Skip to content

Commit e82f527

Browse files
committed
Issue rust-lang#16110: similar_names not triggered by example
1 parent 615ac5d commit e82f527

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

clippy_lints/src/non_expressive_names.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
442442
debug_assert!(a_name.chars().count() < b_name.chars().count());
443443
let mut a_chars = a_name.chars();
444444
let mut b_chars = b_name.chars();
445-
while let (Some(a), Some(b)) = (a_chars.next(), b_chars.next()) {
445+
while let Some(a) = a_chars.next() {
446+
let b = b_chars.next().unwrap();
446447
if a == b {
447448
continue;
448449
}
@@ -454,6 +455,7 @@ fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
454455
// ntuple
455456
return true;
456457
}
457-
// for item in items
458-
true
458+
// If we matched all characters of `a` but `b` is longer,
459+
// it's a single insertion only if `b` has exactly one remaining char.
460+
b_chars.count() != 1
459461
}

tests/ui/similar_names.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn main() {
4949

5050
let cake: i32;
5151
let cakes: i32;
52+
//~^ similar_names
5253
let coke: i32;
5354
//~^ similar_names
5455

@@ -59,7 +60,9 @@ fn main() {
5960
let cheese: i32;
6061
match (42, 43) {
6162
(cheese1, 1) => {},
63+
//~^ similar_names
6264
(cheese2, 2) => panic!(),
65+
//~^ similar_names
6366
_ => println!(""),
6467
}
6568
let ipv4: i32;
@@ -93,6 +96,10 @@ fn main() {
9396
// 3 letter names are allowed to be similar
9497
let kta: i32;
9598
let ktv: i32;
99+
100+
let checked_exp: i32;
101+
let checked_expr: i32;
102+
//~^ similar_names
96103
}
97104

98105
fn foo() {

tests/ui/similar_names.stderr

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ LL | let blubx: i32;
1313
= help: to override `-D warnings` add `#[allow(clippy::similar_names)]`
1414

1515
error: binding's name is too similar to existing binding
16-
--> tests/ui/similar_names.rs:52:9
16+
--> tests/ui/similar_names.rs:51:9
17+
|
18+
LL | let cakes: i32;
19+
| ^^^^^
20+
|
21+
note: existing binding defined here
22+
--> tests/ui/similar_names.rs:50:9
23+
|
24+
LL | let cake: i32;
25+
| ^^^^
26+
27+
error: binding's name is too similar to existing binding
28+
--> tests/ui/similar_names.rs:53:9
1729
|
1830
LL | let coke: i32;
1931
| ^^^^
@@ -25,40 +37,76 @@ LL | let cake: i32;
2537
| ^^^^
2638

2739
error: binding's name is too similar to existing binding
28-
--> tests/ui/similar_names.rs:71:9
40+
--> tests/ui/similar_names.rs:62:10
41+
|
42+
LL | (cheese1, 1) => {},
43+
| ^^^^^^^
44+
|
45+
note: existing binding defined here
46+
--> tests/ui/similar_names.rs:60:9
47+
|
48+
LL | let cheese: i32;
49+
| ^^^^^^
50+
51+
error: binding's name is too similar to existing binding
52+
--> tests/ui/similar_names.rs:64:10
53+
|
54+
LL | (cheese2, 2) => panic!(),
55+
| ^^^^^^^
56+
|
57+
note: existing binding defined here
58+
--> tests/ui/similar_names.rs:60:9
59+
|
60+
LL | let cheese: i32;
61+
| ^^^^^^
62+
63+
error: binding's name is too similar to existing binding
64+
--> tests/ui/similar_names.rs:74:9
2965
|
3066
LL | let xyzeabc: i32;
3167
| ^^^^^^^
3268
|
3369
note: existing binding defined here
34-
--> tests/ui/similar_names.rs:69:9
70+
--> tests/ui/similar_names.rs:72:9
3571
|
3672
LL | let xyz1abc: i32;
3773
| ^^^^^^^
3874

3975
error: binding's name is too similar to existing binding
40-
--> tests/ui/similar_names.rs:76:9
76+
--> tests/ui/similar_names.rs:79:9
4177
|
4278
LL | let parsee: i32;
4379
| ^^^^^^
4480
|
4581
note: existing binding defined here
46-
--> tests/ui/similar_names.rs:74:9
82+
--> tests/ui/similar_names.rs:77:9
4783
|
4884
LL | let parser: i32;
4985
| ^^^^^^
5086

5187
error: binding's name is too similar to existing binding
52-
--> tests/ui/similar_names.rs:102:16
88+
--> tests/ui/similar_names.rs:101:9
89+
|
90+
LL | let checked_expr: i32;
91+
| ^^^^^^^^^^^^
92+
|
93+
note: existing binding defined here
94+
--> tests/ui/similar_names.rs:100:9
95+
|
96+
LL | let checked_exp: i32;
97+
| ^^^^^^^^^^^
98+
99+
error: binding's name is too similar to existing binding
100+
--> tests/ui/similar_names.rs:109:16
53101
|
54102
LL | bpple: sprang,
55103
| ^^^^^^
56104
|
57105
note: existing binding defined here
58-
--> tests/ui/similar_names.rs:101:16
106+
--> tests/ui/similar_names.rs:108:16
59107
|
60108
LL | apple: spring,
61109
| ^^^^^^
62110

63-
error: aborting due to 5 previous errors
111+
error: aborting due to 9 previous errors
64112

0 commit comments

Comments
 (0)