@@ -133,46 +133,36 @@ impl<'a, 'b, 'c> SimilarNamesNameVisitor<'a, 'b, 'c> {
133
133
} else {
134
134
let mut interned_chars = interned_name. chars ( ) ;
135
135
let mut existing_chars = existing_name. chars ( ) ;
136
+ let first_i = interned_chars. next ( ) . expect ( "we know we have at least one char" ) ;
137
+ let first_e = existing_chars. next ( ) . expect ( "we know we have at least one char" ) ;
138
+ let eq_or_numeric = |a : char , b : char | a == b || a. is_numeric ( ) && b. is_numeric ( ) ;
136
139
137
- if interned_chars. next ( ) != existing_chars. next ( ) {
138
- let i = interned_chars. next ( ) . expect ( "we know we have more than 1 char" ) ;
139
- let e = existing_chars. next ( ) . expect ( "we know we have more than 1 char" ) ;
140
- if i == e {
141
- if i == '_' {
142
- // allowed similarity x_foo, y_foo
143
- // or too many chars differ (x_foo, y_boo)
140
+ if eq_or_numeric ( first_i, first_e) {
141
+ let last_i = interned_chars. next_back ( ) . expect ( "we know we have at least two chars" ) ;
142
+ let last_e = existing_chars. next_back ( ) . expect ( "we know we have at least two chars" ) ;
143
+ if eq_or_numeric ( last_i, last_e) {
144
+ if interned_chars. zip ( existing_chars) . filter ( |& ( i, e) | !eq_or_numeric ( i, e) ) . count ( ) != 1 {
144
145
continue ;
145
- } else if interned_chars. ne ( existing_chars) {
146
- // too many chars differ
147
- continue
148
146
}
149
147
} else {
150
- // too many chars differ
151
- continue ;
152
- }
153
- split_at = interned_name. chars ( ) . next ( ) . map ( |c| c. len_utf8 ( ) ) ;
154
- } else if interned_chars. next_back ( ) == existing_chars. next_back ( ) {
155
- if interned_chars. zip ( existing_chars) . filter ( |& ( i, e) | i != e) . count ( ) != 1 {
156
- // too many chars differ, or none differ (aka shadowing)
157
- continue ;
158
- }
159
- } else {
160
- let i = interned_chars. next_back ( ) . expect ( "we know we have more than 2 chars" ) ;
161
- let e = existing_chars. next_back ( ) . expect ( "we know we have more than 2 chars" ) ;
162
- if i == e {
163
- if i == '_' {
164
- // allowed similarity foo_x, foo_x
165
- // or too many chars differ (foo_x, boo_x)
148
+ let second_last_i = interned_chars. next_back ( ) . expect ( "we know we have at least three chars" ) ;
149
+ let second_last_e = existing_chars. next_back ( ) . expect ( "we know we have at least three chars" ) ;
150
+ if !eq_or_numeric ( second_last_i, second_last_e) || second_last_i == '_' || !interned_chars. zip ( existing_chars) . all ( |( i, e) | eq_or_numeric ( i, e) ) {
151
+ // allowed similarity foo_x, foo_y
152
+ // or too many chars differ (foo_x, boo_y) or (foox, booy)
166
153
continue ;
167
- } else if interned_chars. ne ( existing_chars) {
168
- // too many chars differ
169
- continue
170
154
}
171
- } else {
172
- // too many chars differ
155
+ split_at = interned_name. char_indices ( ) . rev ( ) . next ( ) . map ( |( i, _) | i) ;
156
+ }
157
+ } else {
158
+ let second_i = interned_chars. next ( ) . expect ( "we know we have at least two chars" ) ;
159
+ let second_e = existing_chars. next ( ) . expect ( "we know we have at least two chars" ) ;
160
+ if !eq_or_numeric ( second_i, second_e) || second_i == '_' || !interned_chars. zip ( existing_chars) . all ( |( i, e) | eq_or_numeric ( i, e) ) {
161
+ // allowed similarity x_foo, y_foo
162
+ // or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
173
163
continue ;
174
164
}
175
- split_at = interned_name. char_indices ( ) . rev ( ) . next ( ) . map ( |( i , _ ) | i ) ;
165
+ split_at = interned_name. chars ( ) . next ( ) . map ( |c| c . len_utf8 ( ) ) ;
176
166
}
177
167
}
178
168
span_lint_and_then ( self . 0 . cx ,
0 commit comments