diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/repeated_equality_comparison.py b/crates/ruff_linter/resources/test/fixtures/pylint/repeated_equality_comparison.py index 328f38666c87d..d38d372863eda 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/repeated_equality_comparison.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/repeated_equality_comparison.py @@ -73,3 +73,7 @@ foo == False or foo == 0 # Different types, same hashed value foo == 0.0 or foo == 0j # Different types, same hashed value + +foo == "bar" or foo == "bar" # All members identical + +foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical diff --git a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs index 441ea068c6465..c857371c48a00 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs @@ -140,6 +140,18 @@ pub(crate) fn repeated_equality_comparison(checker: &Checker, bool_op: &ast::Exp continue; } + if let Some((&first, rest)) = comparators.split_first() { + let first_comparable = ComparableExpr::from(first); + + if rest + .iter() + .all(|&c| ComparableExpr::from(c) == first_comparable) + { + // Do not flag if all members are identical + continue; + } + } + // if we can determine that all the values are hashable, we can use a set // TODO: improve with type inference let all_hashable = comparators diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1714_repeated_equality_comparison.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1714_repeated_equality_comparison.py.snap index 59b3f5b3ea81f..19b95b170b79f 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1714_repeated_equality_comparison.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR1714_repeated_equality_comparison.py.snap @@ -436,6 +436,7 @@ help: Merge multiple comparisons 73 + foo in {False, 0} # Different types, same hashed value 74 | 75 | foo == 0.0 or foo == 0j # Different types, same hashed value +76 | note: This is an unsafe fix and may change runtime behavior PLR1714 [*] Consider merging multiple comparisons: `foo in {0.0, 0j}`. @@ -445,6 +446,8 @@ PLR1714 [*] Consider merging multiple comparisons: `foo in {0.0, 0j}`. 74 | 75 | foo == 0.0 or foo == 0j # Different types, same hashed value | ^^^^^^^^^^^^^^^^^^^^^^^ +76 | +77 | foo == "bar" or foo == "bar" # All members identical | help: Merge multiple comparisons 72 | @@ -452,4 +455,23 @@ help: Merge multiple comparisons 74 | - foo == 0.0 or foo == 0j # Different types, same hashed value 75 + foo in {0.0, 0j} # Different types, same hashed value +76 | +77 | foo == "bar" or foo == "bar" # All members identical +78 | +note: This is an unsafe fix and may change runtime behavior + +PLR1714 [*] Consider merging multiple comparisons: `foo in {"bar", "bar", "buzz"}`. + --> repeated_equality_comparison.py:79:1 + | +77 | foo == "bar" or foo == "bar" # All members identical +78 | +79 | foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: Merge multiple comparisons +76 | +77 | foo == "bar" or foo == "bar" # All members identical +78 | + - foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical +79 + foo in {"bar", "bar", "buzz"} # All but one members identical note: This is an unsafe fix and may change runtime behavior