From 0b15f179396239ba7fe6d4aafe714aedb2fd98e6 Mon Sep 17 00:00:00 2001 From: InSync Date: Mon, 30 Dec 2024 17:41:33 +0700 Subject: [PATCH] [`flake8-simplify`] More precise inference for dictionaries (`SIM300`) (#15164) Co-authored-by: Micha Reiser --- .../test/fixtures/flake8_simplify/SIM300.py | 6 +- .../flake8_simplify/rules/yoda_conditions.rs | 14 +-- ...ke8_simplify__tests__SIM300_SIM300.py.snap | 89 ++++++++++++------- 3 files changed, 67 insertions(+), 42 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM300.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM300.py index f44cf7ef41e2f..0745cc610850d 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM300.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM300.py @@ -14,6 +14,7 @@ 0 < (number - 100) # SIM300 B 0 SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8 SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # Error in 0.1.8 -{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG + +# https://github.com/astral-sh/ruff/issues/14761 +{"": print(1)} == print(2) +{0: 1, **print(2)} == print(4) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/yoda_conditions.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/yoda_conditions.rs index 78449f2d2ae15..df0be0a8f978f 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/yoda_conditions.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/yoda_conditions.rs @@ -101,13 +101,13 @@ impl From<&Expr> for ConstantLikelihood { .map(ConstantLikelihood::from) .min() .unwrap_or(ConstantLikelihood::Definitely), - Expr::Dict(dict) => { - if dict.is_empty() { - ConstantLikelihood::Definitely - } else { - ConstantLikelihood::Probably - } - } + Expr::Dict(dict) => dict + .items + .iter() + .flat_map(|item| std::iter::once(&item.value).chain(item.key.as_ref())) + .map(ConstantLikelihood::from) + .min() + .unwrap_or(ConstantLikelihood::Definitely), Expr::BinOp(ast::ExprBinOp { left, right, .. }) => cmp::min( ConstantLikelihood::from(&**left), ConstantLikelihood::from(&**right), diff --git a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM300_SIM300.py.snap b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM300_SIM300.py.snap index 208f9d0f679b5..677e4563ded7f 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM300_SIM300.py.snap +++ b/crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM300_SIM300.py.snap @@ -269,7 +269,7 @@ SIM300.py:14:1: SIM300 [*] Yoda condition detected 14 |+(number - 100) > 0 # SIM300 15 15 | B B` @@ -288,8 +289,8 @@ SIM300.py:15:1: SIM300 [*] Yoda condition detected 15 |-B B or B 16 16 | B or(B) (B)` @@ -308,46 +308,67 @@ SIM300.py:16:5: SIM300 [*] Yoda condition detected 15 15 | B (B) -17 17 | -18 18 | # Errors in preview -19 19 | ['upper'] == UPPER_LIST +17 17 | {"non-empty-dict": "is-ok"} == DummyHandler.CONFIG +18 18 | +19 19 | # Errors in preview -SIM300.py:19:1: SIM300 [*] Yoda condition detected +SIM300.py:17:1: SIM300 [*] Yoda condition detected | -18 | # Errors in preview -19 | ['upper'] == UPPER_LIST - | ^^^^^^^^^^^^^^^^^^^^^^^ SIM300 -20 | {} == DummyHandler.CONFIG +15 | B