From 3b16c5f07bebe11a91fb6c643ce941c1cb2c2bdd Mon Sep 17 00:00:00 2001 From: njhearp Date: Fri, 26 Dec 2025 19:46:42 -0500 Subject: [PATCH 1/2] Ignore identical members (`PLR1714`) --- .../pylint/repeated_equality_comparison.py | 4 ++++ .../rules/repeated_equality_comparison.rs | 9 ++++++++ ...R1714_repeated_equality_comparison.py.snap | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+) 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..1c7df0af52d05 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,15 @@ pub(crate) fn repeated_equality_comparison(checker: &Checker, bool_op: &ast::Exp continue; } + let first_comparable = ComparableExpr::from(comparators[0]); + if comparators + .iter() + .skip(1) + .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 From 0739b2ab6a30f02efe02b19b014d2c27df52fe5b Mon Sep 17 00:00:00 2001 From: njhearp Date: Fri, 2 Jan 2026 12:13:00 -0500 Subject: [PATCH 2/2] Wrap check --- .../rules/repeated_equality_comparison.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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 1c7df0af52d05..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,15 +140,18 @@ pub(crate) fn repeated_equality_comparison(checker: &Checker, bool_op: &ast::Exp continue; } - let first_comparable = ComparableExpr::from(comparators[0]); - if comparators - .iter() - .skip(1) - .all(|&c| ComparableExpr::from(c) == first_comparable) - { - // Do not flag if all members are identical - 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