diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py index bd1282ce0f..8252420f6a 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM222.py @@ -167,3 +167,29 @@ print(f"{a}{b}" or "bar") print(f"{a}{''}" or "bar") print(f"{''}{''}" or "bar") print(f"{1}{''}" or "bar") + + +# Regression test for: https://github.com/astral-sh/ruff/issues/14237 +for x in [*a] or [None]: + pass + +for x in {*a} or [None]: + pass + +for x in (*a,) or [None]: + pass + +for x in {**a} or [None]: + pass + +for x in [*a, *b] or [None]: + pass + +for x in {*a, *b} or [None]: + pass + +for x in (*a, *b) or [None]: + pass + +for x in {**a, **b} or [None]: + pass diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM222_SIM222.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM222_SIM222.py.snap index ddf4598e13..bf78eede11 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM222_SIM222.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM222_SIM222.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs +snapshot_kind: text --- SIM222.py:1:4: SIM222 [*] Use `True` instead of `... or True` | @@ -1060,5 +1061,5 @@ SIM222.py:168:7: SIM222 [*] Use `"bar"` instead of `... or "bar"` 168 |-print(f"{''}{''}" or "bar") 168 |+print("bar") 169 169 | print(f"{1}{''}" or "bar") - - +170 170 | +171 171 | diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 3a95065cbf..13c0b89d57 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -12,8 +12,8 @@ use crate::parenthesize::parenthesized_range; use crate::statement_visitor::StatementVisitor; use crate::visitor::Visitor; use crate::{ - self as ast, Arguments, CmpOp, ExceptHandler, Expr, FStringElement, MatchCase, Operator, - Pattern, Stmt, TypeParam, + self as ast, Arguments, CmpOp, DictItem, ExceptHandler, Expr, FStringElement, MatchCase, + Operator, Pattern, Stmt, TypeParam, }; use crate::{AnyNodeRef, ExprContext}; @@ -1188,14 +1188,32 @@ impl Truthiness { | Expr::Set(ast::ExprSet { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }) => { if elts.is_empty() { - Self::Falsey + return Self::Falsey; + } + + if elts.iter().all(Expr::is_starred_expr) { + // [*foo] / [*foo, *bar] + Self::Unknown } else { Self::Truthy } } Expr::Dict(dict) => { if dict.is_empty() { - Self::Falsey + return Self::Falsey; + } + + if dict.items.iter().all(|item| { + matches!( + item, + DictItem { + key: None, + value: Expr::Name(..) + } + ) + }) { + // {**foo} / {**foo, **bar} + Self::Unknown } else { Self::Truthy }