[flake8-simplify] Infer "unknown" truthiness for literal iterables whose items are all unpacks (SIM222) (#14263)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz (push) Blocked by required conditions
CI / Fuzz the parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

## Summary

Resolves #14237.

## Test Plan

`cargo nextest run` and `cargo insta test`.

---------

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
InSync 2024-11-12 03:23:34 +07:00 committed by GitHub
parent f1f3bd1cd3
commit be69f61b3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 6 deletions

View file

@ -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

View file

@ -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 |

View file

@ -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
}