@@ -178,22 +178,26 @@ fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
178
178
179
179
/// Implementation if `MATCH_SAME_ARMS`.
180
180
fn lint_match_arms ( cx : & LateContext , expr : & Expr ) {
181
- let hash = |arm : & Arm | -> u64 {
182
- let mut h = SpanlessHash :: new ( cx) ;
183
- h. hash_expr ( & arm. body ) ;
184
- h. finish ( )
185
- } ;
181
+ if let ExprMatch ( _, ref arms, MatchSource :: Normal ) = expr. node {
182
+ let hash = |& ( _, arm) : & ( usize , & Arm ) | -> u64 {
183
+ let mut h = SpanlessHash :: new ( cx) ;
184
+ h. hash_expr ( & arm. body ) ;
185
+ h. finish ( )
186
+ } ;
186
187
187
- let eq = |lhs : & Arm , rhs : & Arm | -> bool {
188
- // Arms with a guard are ignored, those can’t always be merged together
189
- lhs. guard . is_none ( ) && rhs. guard . is_none ( ) &&
190
- SpanlessEq :: new ( cx) . eq_expr ( & lhs. body , & rhs. body ) &&
191
- // all patterns should have the same bindings
192
- bindings ( cx, & lhs. pats [ 0 ] ) == bindings ( cx, & rhs. pats [ 0 ] )
193
- } ;
188
+ let eq = |& ( lindex, lhs) : & ( usize , & Arm ) , & ( rindex, rhs) : & ( usize , & Arm ) | -> bool {
189
+ let min_index = usize:: min ( lindex, rindex) ;
190
+ let max_index = usize:: max ( rindex, rindex) ;
191
+ // Arms with a guard are ignored, those can’t always be merged together
192
+ // This is also the case for arms in-between each there is an arm with a guard
193
+ ( min_index..=max_index) . all ( |index| arms[ index] . guard . is_none ( ) ) &&
194
+ SpanlessEq :: new ( cx) . eq_expr ( & lhs. body , & rhs. body ) &&
195
+ // all patterns should have the same bindings
196
+ bindings ( cx, & lhs. pats [ 0 ] ) == bindings ( cx, & rhs. pats [ 0 ] )
197
+ } ;
194
198
195
- if let ExprMatch ( _ , ref arms , MatchSource :: Normal ) = expr . node {
196
- if let Some ( ( i , j) ) = search_same ( arms , hash, eq) {
199
+ let indexed_arms : Vec < ( usize , & Arm ) > = arms . iter ( ) . enumerate ( ) . collect ( ) ;
200
+ if let Some ( ( & ( _ , i ) , & ( _ , j) ) ) = search_same ( & indexed_arms , hash, eq) {
197
201
span_lint_and_then (
198
202
cx,
199
203
MATCH_SAME_ARMS ,
0 commit comments