|
1 | 1 | use crate::utils::{span_lint, span_lint_and_then};
|
2 | 2 | use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
3 | 3 | use rustc::{declare_tool_lint, impl_lint_pass};
|
| 4 | +use std::cmp::Ordering; |
4 | 5 | use syntax::ast::*;
|
5 | 6 | use syntax::attr;
|
6 | 7 | use syntax::source_map::Span;
|
@@ -206,63 +207,67 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
|
206 | 207 | continue;
|
207 | 208 | }
|
208 | 209 | let mut split_at = None;
|
209 |
| - if existing_name.len > count { |
210 |
| - if existing_name.len - count != 1 || levenstein_not_1(&interned_name, &existing_name.interned) { |
211 |
| - continue; |
212 |
| - } |
213 |
| - } else if existing_name.len < count { |
214 |
| - if count - existing_name.len != 1 || levenstein_not_1(&existing_name.interned, &interned_name) { |
215 |
| - continue; |
216 |
| - } |
217 |
| - } else { |
218 |
| - let mut interned_chars = interned_name.chars(); |
219 |
| - let mut existing_chars = existing_name.interned.chars(); |
220 |
| - let first_i = interned_chars.next().expect("we know we have at least one char"); |
221 |
| - let first_e = existing_chars.next().expect("we know we have at least one char"); |
222 |
| - let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric(); |
| 210 | + match existing_name.len.cmp(&count) { |
| 211 | + Ordering::Greater => { |
| 212 | + if existing_name.len - count != 1 || levenstein_not_1(&interned_name, &existing_name.interned) { |
| 213 | + continue; |
| 214 | + } |
| 215 | + }, |
| 216 | + Ordering::Less => { |
| 217 | + if count - existing_name.len != 1 || levenstein_not_1(&existing_name.interned, &interned_name) { |
| 218 | + continue; |
| 219 | + } |
| 220 | + }, |
| 221 | + Ordering::Equal => { |
| 222 | + let mut interned_chars = interned_name.chars(); |
| 223 | + let mut existing_chars = existing_name.interned.chars(); |
| 224 | + let first_i = interned_chars.next().expect("we know we have at least one char"); |
| 225 | + let first_e = existing_chars.next().expect("we know we have at least one char"); |
| 226 | + let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric(); |
223 | 227 |
|
224 |
| - if eq_or_numeric((first_i, first_e)) { |
225 |
| - let last_i = interned_chars.next_back().expect("we know we have at least two chars"); |
226 |
| - let last_e = existing_chars.next_back().expect("we know we have at least two chars"); |
227 |
| - if eq_or_numeric((last_i, last_e)) { |
228 |
| - if interned_chars |
229 |
| - .zip(existing_chars) |
230 |
| - .filter(|&ie| !eq_or_numeric(ie)) |
231 |
| - .count() |
232 |
| - != 1 |
233 |
| - { |
234 |
| - continue; |
| 228 | + if eq_or_numeric((first_i, first_e)) { |
| 229 | + let last_i = interned_chars.next_back().expect("we know we have at least two chars"); |
| 230 | + let last_e = existing_chars.next_back().expect("we know we have at least two chars"); |
| 231 | + if eq_or_numeric((last_i, last_e)) { |
| 232 | + if interned_chars |
| 233 | + .zip(existing_chars) |
| 234 | + .filter(|&ie| !eq_or_numeric(ie)) |
| 235 | + .count() |
| 236 | + != 1 |
| 237 | + { |
| 238 | + continue; |
| 239 | + } |
| 240 | + } else { |
| 241 | + let second_last_i = interned_chars |
| 242 | + .next_back() |
| 243 | + .expect("we know we have at least three chars"); |
| 244 | + let second_last_e = existing_chars |
| 245 | + .next_back() |
| 246 | + .expect("we know we have at least three chars"); |
| 247 | + if !eq_or_numeric((second_last_i, second_last_e)) |
| 248 | + || second_last_i == '_' |
| 249 | + || !interned_chars.zip(existing_chars).all(eq_or_numeric) |
| 250 | + { |
| 251 | + // allowed similarity foo_x, foo_y |
| 252 | + // or too many chars differ (foo_x, boo_y) or (foox, booy) |
| 253 | + continue; |
| 254 | + } |
| 255 | + split_at = interned_name.char_indices().rev().next().map(|(i, _)| i); |
235 | 256 | }
|
236 | 257 | } else {
|
237 |
| - let second_last_i = interned_chars |
238 |
| - .next_back() |
239 |
| - .expect("we know we have at least three chars"); |
240 |
| - let second_last_e = existing_chars |
241 |
| - .next_back() |
242 |
| - .expect("we know we have at least three chars"); |
243 |
| - if !eq_or_numeric((second_last_i, second_last_e)) |
244 |
| - || second_last_i == '_' |
| 258 | + let second_i = interned_chars.next().expect("we know we have at least two chars"); |
| 259 | + let second_e = existing_chars.next().expect("we know we have at least two chars"); |
| 260 | + if !eq_or_numeric((second_i, second_e)) |
| 261 | + || second_i == '_' |
245 | 262 | || !interned_chars.zip(existing_chars).all(eq_or_numeric)
|
246 | 263 | {
|
247 |
| - // allowed similarity foo_x, foo_y |
248 |
| - // or too many chars differ (foo_x, boo_y) or (foox, booy) |
| 264 | + // allowed similarity x_foo, y_foo |
| 265 | + // or too many chars differ (x_foo, y_boo) or (xfoo, yboo) |
249 | 266 | continue;
|
250 | 267 | }
|
251 |
| - split_at = interned_name.char_indices().rev().next().map(|(i, _)| i); |
| 268 | + split_at = interned_name.chars().next().map(char::len_utf8); |
252 | 269 | }
|
253 |
| - } else { |
254 |
| - let second_i = interned_chars.next().expect("we know we have at least two chars"); |
255 |
| - let second_e = existing_chars.next().expect("we know we have at least two chars"); |
256 |
| - if !eq_or_numeric((second_i, second_e)) |
257 |
| - || second_i == '_' |
258 |
| - || !interned_chars.zip(existing_chars).all(eq_or_numeric) |
259 |
| - { |
260 |
| - // allowed similarity x_foo, y_foo |
261 |
| - // or too many chars differ (x_foo, y_boo) or (xfoo, yboo) |
262 |
| - continue; |
263 |
| - } |
264 |
| - split_at = interned_name.chars().next().map(char::len_utf8); |
265 |
| - } |
| 270 | + }, |
266 | 271 | }
|
267 | 272 | span_lint_and_then(
|
268 | 273 | self.0.cx,
|
|
0 commit comments