mirror of https://github.com/astral-sh/ruff
Avoid triggering single-element test for starred expressions (#8433)
See: https://github.com/astral-sh/ruff/issues/8402#issuecomment-1788784721
This commit is contained in:
parent
ab6bf50a2d
commit
f8f507cfc8
|
|
@ -43,3 +43,6 @@ if 1 is {1}:
|
|||
|
||||
if "a" == "a":
|
||||
pass
|
||||
|
||||
if 1 in {*[1]}:
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::helpers::generate_comparison;
|
||||
use ruff_python_ast::ExprStringLiteral;
|
||||
use ruff_python_ast::{CmpOp, Expr};
|
||||
use ruff_python_ast::{self as ast, CmpOp, Expr, ExprStringLiteral};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
|
@ -95,13 +94,17 @@ pub(crate) fn single_item_membership_test(
|
|||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
||||
/// Return the single item wrapped in Some if the expression contains a single
|
||||
/// item, otherwise return None.
|
||||
/// Return the single item wrapped in `Some` if the expression contains a single
|
||||
/// item, otherwise return `None`.
|
||||
fn single_item(expr: &Expr) -> Option<&Expr> {
|
||||
match expr {
|
||||
Expr::List(list) if list.elts.len() == 1 => Some(&list.elts[0]),
|
||||
Expr::Tuple(tuple) if tuple.elts.len() == 1 => Some(&tuple.elts[0]),
|
||||
Expr::Set(set) if set.elts.len() == 1 => Some(&set.elts[0]),
|
||||
Expr::List(ast::ExprList { elts, .. })
|
||||
| Expr::Tuple(ast::ExprTuple { elts, .. })
|
||||
| Expr::Set(ast::ExprSet { elts, .. }) => match elts.as_slice() {
|
||||
[Expr::Starred(_)] => None,
|
||||
[item] => Some(item),
|
||||
_ => None,
|
||||
},
|
||||
string_expr @ Expr::StringLiteral(ExprStringLiteral { value: string, .. })
|
||||
if string.chars().count() == 1 =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue