Skip to content

Commit

Permalink
Avoid triggering single-element test for starred expressions (#8433)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 2, 2023
1 parent ab6bf50 commit f8f507c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB171.py
Expand Up @@ -43,3 +43,6 @@

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;
Expand Down Expand Up @@ -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 =>
{
Expand Down

0 comments on commit f8f507c

Please sign in to comment.