@@ -5,11 +5,10 @@ use ast::ExprContext;
55use ruff_diagnostics:: { AlwaysFixableViolation , Diagnostic , Edit , Fix } ;
66use ruff_macros:: { derive_message_formats, violation} ;
77use ruff_python_ast:: comparable:: ComparableExpr ;
8- use ruff_python_ast:: hashable:: HashableExpr ;
98use ruff_python_ast:: helpers:: { any_over_expr, contains_effect} ;
109use ruff_python_ast:: { self as ast, BoolOp , CmpOp , Expr } ;
1110use ruff_python_semantic:: SemanticModel ;
12- use ruff_text_size:: { Ranged , TextRange , TextSize } ;
11+ use ruff_text_size:: { Ranged , TextRange } ;
1312
1413use crate :: checkers:: ast:: Checker ;
1514use crate :: fix:: snippet:: SourceCodeSnippet ;
@@ -71,7 +70,7 @@ impl AlwaysFixableViolation for RepeatedEqualityComparison {
7170/// PLR1714
7271pub ( crate ) fn repeated_equality_comparison ( checker : & mut Checker , bool_op : & ast:: ExprBoolOp ) {
7372 // Map from expression hash to (starting offset, number of comparisons, list
74- let mut value_to_comparators: FxHashMap < HashableExpr , ( TextSize , Vec < & Expr > , Vec < usize > ) > =
73+ let mut value_to_comparators: FxHashMap < ComparableExpr , ( & Expr , Vec < & Expr > , Vec < usize > ) > =
7574 FxHashMap :: with_capacity_and_hasher ( bool_op. values . len ( ) * 2 , FxBuildHasher ) ;
7675
7776 for ( i, value) in bool_op. values . iter ( ) . enumerate ( ) {
@@ -82,23 +81,23 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
8281 if matches ! ( left, Expr :: Name ( _) | Expr :: Attribute ( _) ) {
8382 let ( _, left_matches, index_matches) = value_to_comparators
8483 . entry ( left. into ( ) )
85- . or_insert_with ( || ( left. start ( ) , Vec :: new ( ) , Vec :: new ( ) ) ) ;
84+ . or_insert_with ( || ( left, Vec :: new ( ) , Vec :: new ( ) ) ) ;
8685 left_matches. push ( right) ;
8786 index_matches. push ( i) ;
8887 }
8988
9089 if matches ! ( right, Expr :: Name ( _) | Expr :: Attribute ( _) ) {
9190 let ( _, right_matches, index_matches) = value_to_comparators
9291 . entry ( right. into ( ) )
93- . or_insert_with ( || ( right. start ( ) , Vec :: new ( ) , Vec :: new ( ) ) ) ;
92+ . or_insert_with ( || ( right, Vec :: new ( ) , Vec :: new ( ) ) ) ;
9493 right_matches. push ( left) ;
9594 index_matches. push ( i) ;
9695 }
9796 }
9897
99- for ( value , ( _ , comparators, indices) ) in value_to_comparators
100- . iter ( )
101- . sorted_by_key ( |( _ , ( start , _, _) ) | * start)
98+ for ( expr , comparators, indices) in value_to_comparators
99+ . into_values ( )
100+ . sorted_by_key ( |( expr , _, _) | expr . start ( ) )
102101 {
103102 // If there's only one comparison, there's nothing to merge.
104103 if comparators. len ( ) == 1 {
@@ -112,9 +111,9 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
112111 if last. is_some_and ( |last| last + 1 == * index) {
113112 let ( indices, comparators) = sequences. last_mut ( ) . unwrap ( ) ;
114113 indices. push ( * index) ;
115- comparators. push ( * comparator) ;
114+ comparators. push ( comparator) ;
116115 } else {
117- sequences. push ( ( vec ! [ * index] , vec ! [ * comparator] ) ) ;
116+ sequences. push ( ( vec ! [ * index] , vec ! [ comparator] ) ) ;
118117 }
119118 last = Some ( * index) ;
120119 }
@@ -127,7 +126,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
127126 let mut diagnostic = Diagnostic :: new (
128127 RepeatedEqualityComparison {
129128 expression : SourceCodeSnippet :: new ( merged_membership_test (
130- value . as_expr ( ) ,
129+ expr ,
131130 bool_op. op ,
132131 & comparators,
133132 checker. locator ( ) ,
@@ -148,7 +147,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
148147 op : bool_op. op ,
149148 values : before
150149 . chain ( std:: iter:: once ( Expr :: Compare ( ast:: ExprCompare {
151- left : Box :: new ( value . as_expr ( ) . clone ( ) ) ,
150+ left : Box :: new ( expr . clone ( ) ) ,
152151 ops : match bool_op. op {
153152 BoolOp :: Or => Box :: from ( [ CmpOp :: In ] ) ,
154153 BoolOp :: And => Box :: from ( [ CmpOp :: NotIn ] ) ,
0 commit comments