Skip to content

Commit f4726c0

Browse files
committed
Merge pull request #983 from Manishearth/fix-tests
Fix wrong tests and improve some other
2 parents 1aab0e6 + 35a22bc commit f4726c0

17 files changed

+176
-64
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ clippy_lints = { version = "0.0.74", path = "clippy_lints" }
3535
rustc-serialize = "0.3"
3636

3737
[dev-dependencies]
38-
compiletest_rs = "0.1.0"
38+
compiletest_rs = "0.2.0"
3939
lazy_static = "0.1.15"
4040
regex = "0.1.56"
4141

clippy_lints/src/loops.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,15 @@ impl LateLintPass for LoopsPass {
290290
!is_iterator_used_after_while_let(cx, iter_expr) {
291291
let iterator = snippet(cx, method_args[0].span, "_");
292292
let loop_var = snippet(cx, pat_args[0].span, "_");
293-
span_help_and_lint(cx,
293+
span_lint_and_then(cx,
294294
WHILE_LET_ON_ITERATOR,
295295
expr.span,
296296
"this loop could be written as a `for` loop",
297-
&format!("try\nfor {} in {} {{...}}", loop_var, iterator));
297+
|db| {
298+
db.span_suggestion(expr.span,
299+
"try",
300+
format!("for {} in {} {{ .. }}", loop_var, iterator));
301+
});
298302
}
299303
}
300304
}
@@ -446,11 +450,11 @@ fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
446450
expr.span,
447451
"this range is empty so this for loop will never run",
448452
|db| {
449-
db.span_suggestion(expr.span,
453+
db.span_suggestion(arg.span,
450454
"consider using the following if \
451455
you are attempting to iterate \
452456
over this range in reverse",
453-
format!("({}..{}).rev()` ", end_snippet, start_snippet));
457+
format!("({}..{}).rev()", end_snippet, start_snippet));
454458
});
455459
} else if eq && limits != ast::RangeLimits::Closed {
456460
// if they are equal, it's also problematic - this loop
@@ -598,7 +602,7 @@ fn check_for_loop_over_map_kv(cx: &LateContext, pat: &Pat, arg: &Expr, body: &Ex
598602
|db| {
599603
db.span_suggestion(expr.span,
600604
"use the corresponding method",
601-
format!("for {} in {}.{}() {{...}}",
605+
format!("for {} in {}.{}() {{ .. }}",
602606
snippet(cx, *pat_span, ".."),
603607
snippet(cx, arg_span, ".."),
604608
kind));

tests/compile-fail/absurd-extreme-comparisons.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@ fn main() {
88

99
let u: u32 = 42;
1010

11-
u <= 0; //~ERROR this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
12-
u <= Z; //~ERROR this comparison involving
13-
u < Z; //~ERROR this comparison involving
14-
Z >= u; //~ERROR this comparison involving
15-
Z > u; //~ERROR this comparison involving
16-
u > std::u32::MAX; //~ERROR this comparison involving
17-
u >= std::u32::MAX; //~ERROR this comparison involving
18-
std::u32::MAX < u; //~ERROR this comparison involving
19-
std::u32::MAX <= u; //~ERROR this comparison involving
11+
u <= 0;
12+
//~^ ERROR this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
13+
//~| HELP using u == 0 instead
14+
u <= Z;
15+
//~^ ERROR this comparison involving
16+
//~| HELP using u == Z instead
17+
u < Z;
18+
//~^ ERROR this comparison involving
19+
//~| HELP comparison is always false
20+
Z >= u;
21+
//~^ ERROR this comparison involving
22+
//~| HELP using Z == u instead
23+
Z > u;
24+
//~^ ERROR this comparison involving
25+
//~| HELP comparison is always false
26+
u > std::u32::MAX;
27+
//~^ ERROR this comparison involving
28+
//~| HELP comparison is always false
29+
u >= std::u32::MAX;
30+
//~^ ERROR this comparison involving
31+
//~| HELP using u == std::u32::MAX instead
32+
std::u32::MAX < u;
33+
//~^ ERROR this comparison involving
34+
//~| HELP comparison is always false
35+
std::u32::MAX <= u;
36+
//~^ ERROR this comparison involving
37+
//~| HELP using std::u32::MAX == u instead
2038

2139
1-1 > u;
2240
//~^ ERROR this comparison involving
@@ -29,13 +47,23 @@ fn main() {
2947
//~| HELP because 12 - 2*6 is the minimum value for this type, the case where the two sides are not equal never occurs, consider using u == 12 - 2*6 instead
3048

3149
let i: i8 = 0;
32-
i < -127 - 1; //~ERROR this comparison involving
33-
std::i8::MAX >= i; //~ERROR this comparison involving
34-
3-7 < std::i32::MIN; //~ERROR this comparison involving
50+
i < -127 - 1;
51+
//~^ ERROR this comparison involving
52+
//~| HELP comparison is always false
53+
std::i8::MAX >= i;
54+
//~^ ERROR this comparison involving
55+
//~| HELP comparison is always true
56+
3-7 < std::i32::MIN;
57+
//~^ ERROR this comparison involving
58+
//~| HELP comparison is always false
3559

3660
let b = false;
37-
b >= true; //~ERROR this comparison involving
38-
false > b; //~ERROR this comparison involving
61+
b >= true;
62+
//~^ ERROR this comparison involving
63+
//~| HELP using b == true instead
64+
false > b;
65+
//~^ ERROR this comparison involving
66+
//~| HELP comparison is always false
3967

4068
u > 0; // ok
4169

tests/compile-fail/booleans.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,37 @@ fn equality_stuff() {
5252
let c: i32 = unimplemented!();
5353
let d: i32 = unimplemented!();
5454
let e: i32 = unimplemented!();
55-
let _ = a == b && a != b; //~ ERROR this boolean expression contains a logic bug
55+
let _ = a == b && a != b;
56+
//~^ ERROR this boolean expression contains a logic bug
5657
//~| HELP this expression can be optimized out
5758
//~| HELP it would look like the following
5859
//~| SUGGESTION let _ = false;
59-
let _ = a == b && c == 5 && a == b; //~ ERROR this boolean expression can be simplified
60+
let _ = a == b && c == 5 && a == b;
61+
//~^ ERROR this boolean expression can be simplified
6062
//~| HELP try
6163
//~| SUGGESTION let _ = a == b && c == 5;
62-
let _ = a == b && c == 5 && b == a; //~ ERROR this boolean expression can be simplified
64+
//~| HELP try
65+
//~| SUGGESTION let _ = !(c != 5 || a != b);
66+
let _ = a == b && c == 5 && b == a;
67+
//~^ ERROR this boolean expression can be simplified
6368
//~| HELP try
6469
//~| SUGGESTION let _ = a == b && c == 5;
6570
//~| HELP try
6671
//~| SUGGESTION let _ = !(c != 5 || a != b);
67-
let _ = a < b && a >= b; //~ ERROR this boolean expression contains a logic bug
72+
let _ = a < b && a >= b;
73+
//~^ ERROR this boolean expression contains a logic bug
6874
//~| HELP this expression can be optimized out
6975
//~| HELP it would look like the following
7076
//~| SUGGESTION let _ = false;
71-
let _ = a > b && a <= b; //~ ERROR this boolean expression contains a logic bug
77+
let _ = a > b && a <= b;
78+
//~^ ERROR this boolean expression contains a logic bug
7279
//~| HELP this expression can be optimized out
7380
//~| HELP it would look like the following
7481
//~| SUGGESTION let _ = false;
7582
let _ = a > b && a == b;
7683

77-
let _ = a != b || !(a != b || c == d); //~ ERROR this boolean expression can be simplified
84+
let _ = a != b || !(a != b || c == d);
85+
//~^ ERROR this boolean expression can be simplified
7886
//~| HELP try
7987
//~| SUGGESTION let _ = c != d || a != b;
8088
//~| HELP try

tests/compile-fail/collapsible_if.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
fn main() {
66
let x = "hello";
77
let y = "world";
8-
if x == "hello" { //~ERROR this if statement can be collapsed
8+
if x == "hello" {
9+
//~^ ERROR this if statement can be collapsed
10+
//~| HELP try
11+
//~| SUGGESTION if x == "hello" && y == "world" {
912
if y == "world" {
1013
println!("Hello world!");
1114
}
1215
}
1316

14-
if x == "hello" || x == "world" { //~ERROR this if statement can be collapsed
17+
if x == "hello" || x == "world" {
18+
//~^ ERROR this if statement can be collapsed
19+
//~| HELP try
20+
//~| SUGGESTION if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
1521
if y == "world" || y == "hello" {
1622
println!("Hello world!");
1723
}

tests/compile-fail/for_loop.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ fn main() {
200200
}
201201

202202
// testing that the empty range lint folds constants
203-
for i in 10..5+4 { //~ERROR this range is empty so this for loop will never run
203+
for i in 10..5+4 {
204+
//~^ ERROR this range is empty so this for loop will never run
205+
//~| HELP if you are attempting to iterate over this range in reverse
206+
//~| SUGGESTION for i in (5+4..10).rev() {
204207
println!("{}", i);
205208
}
206209

207-
for i in (5+2)..(3-1) { //~ERROR this range is empty so this for loop will never run
210+
for i in (5+2)..(3-1) {
211+
//~^ ERROR this range is empty so this for loop will never run
212+
//~| HELP if you are attempting to iterate over this range in reverse
213+
//~| SUGGESTION for i in ((3-1)..(5+2)).rev() {
208214
println!("{}", i);
209215
}
210216

tests/compile-fail/formatting.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,32 @@ fn foo() -> bool { true }
1111
fn main() {
1212
// weird `else if` formatting:
1313
if foo() {
14-
} if foo() { //~ERROR this looks like an `else if` but the `else` is missing
14+
} if foo() {
15+
//~^ ERROR this looks like an `else if` but the `else` is missing
16+
//~| NOTE add the missing `else` or
1517
}
1618

1719
let _ = {
1820
if foo() {
19-
} if foo() { //~ERROR this looks like an `else if` but the `else` is missing
21+
} if foo() {
22+
//~^ ERROR this looks like an `else if` but the `else` is missing
23+
//~| NOTE add the missing `else` or
2024
}
2125
else {
2226
}
2327
};
2428

2529
if foo() {
26-
} else //~ERROR this is an `else if` but the formatting might hide it
30+
} else
31+
//~^ ERROR this is an `else if` but the formatting might hide it
32+
//~| NOTE remove the `else` or
2733
if foo() { // the span of the above error should continue here
2834
}
2935

3036
if foo() {
31-
} //~ERROR this is an `else if` but the formatting might hide it
37+
}
38+
//~^ ERROR this is an `else if` but the formatting might hide it
39+
//~| NOTE remove the `else` or
3240
else
3341
if foo() { // the span of the above error should continue here
3442
}

tests/compile-fail/let_return.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
fn test() -> i32 {
88
let _y = 0; // no warning
9-
let x = 5; //~NOTE
9+
let x = 5; //~NOTE this expression can be directly returned
1010
x //~ERROR returning the result of a let binding
1111
}
1212

1313
fn test_inner() -> i32 {
1414
if true {
15-
let x = 5;
15+
let x = 5; //~NOTE this expression can be directly returned
1616
x //~ERROR returning the result of a let binding
1717
} else {
1818
0

tests/compile-fail/matches.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,43 @@ fn single_match_know_enum() {
100100
fn match_bool() {
101101
let test: bool = true;
102102

103-
match test { //~ ERROR you seem to be trying to match on a boolean expression
103+
match test {
104+
//~^ ERROR you seem to be trying to match on a boolean expression
105+
//~| HELP try
106+
//~| SUGGESTION if test { 0 } else { 42 };
104107
true => 0,
105108
false => 42,
106109
};
107110

108111
let option = 1;
109-
match option == 1 { //~ ERROR you seem to be trying to match on a boolean expression
112+
match option == 1 {
113+
//~^ ERROR you seem to be trying to match on a boolean expression
114+
//~| HELP try
115+
//~| SUGGESTION if option == 1 { 1 } else { 0 };
110116
true => 1,
111117
false => 0,
112118
};
113119

114-
match test { //~ ERROR you seem to be trying to match on a boolean expression
120+
match test {
121+
//~^ ERROR you seem to be trying to match on a boolean expression
122+
//~| HELP try
123+
//~^^ SUGGESTION if !test { println!("Noooo!"); };
115124
true => (),
116125
false => { println!("Noooo!"); }
117126
};
118127

119-
match test { //~ ERROR you seem to be trying to match on a boolean expression
128+
match test {
129+
//~^ ERROR you seem to be trying to match on a boolean expression
130+
//~| HELP try
131+
//~^^ SUGGESTION if !test { println!("Noooo!"); };
120132
false => { println!("Noooo!"); }
121133
_ => (),
122134
};
123135

124-
match test { //~ ERROR you seem to be trying to match on a boolean expression
136+
match test {
137+
//~^ ERROR you seem to be trying to match on a boolean expression
138+
//~| HELP try
139+
//~| SUGGESTION if test { println!("Yes!"); } else { println!("Noooo!"); };
125140
false => { println!("Noooo!"); }
126141
true => { println!("Yes!"); }
127142
};
@@ -216,7 +231,7 @@ fn overlapping() {
216231
11 ... 50 => println!("0 ... 10"),
217232
_ => (),
218233
}
219-
234+
220235
if let None = Some(42) {
221236
// nothing
222237
} else if let None = Some(42) {

tests/compile-fail/methods.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,19 @@ fn starts_with() {
356356

357357
fn use_extend_from_slice() {
358358
let mut v : Vec<&'static str> = vec![];
359-
v.extend(&["Hello", "World"]); //~ERROR use of `extend`
359+
v.extend(&["Hello", "World"]);
360+
//~^ ERROR use of `extend`
361+
//~| HELP try this
362+
//~| SUGGESTION v.extend_from_slice(&["Hello", "World"]);
360363
v.extend(&vec!["Some", "more"]);
361-
//~^ERROR use of `extend`
364+
//~^ ERROR use of `extend`
362365
//~| HELP try this
363366
//~| SUGGESTION v.extend_from_slice(&vec!["Some", "more"]);
364367

365-
v.extend(vec!["And", "even", "more"].iter()); //~ERROR use of `extend`
368+
v.extend(vec!["And", "even", "more"].iter());
369+
//~^ ERROR use of `extend`
370+
//~| HELP try this
371+
//FIXME: the suggestion if broken because of the macro
366372
let o : Option<&'static str> = None;
367373
v.extend(o);
368374
v.extend(Some("Bye"));

tests/compile-fail/mut_mut.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ fn main() {
3838
***y + **x;
3939
}
4040

41-
let mut z = mut_ptr!(&mut 3u32); //~ NOTE in this expansion of mut_ptr!
41+
let mut z = mut_ptr!(&mut 3u32);
42+
//~^ NOTE in this expansion of mut_ptr!
4243
}

tests/compile-fail/needless_borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn x(y: &i32) -> i32 {
1010
fn main() {
1111
let a = 5;
1212
let b = x(&a);
13-
let c = x(&&a); //~ ERROR: needless_borrow
13+
let c = x(&&a); //~ ERROR: this expression borrows a reference that is immediately dereferenced by the compiler
1414
let s = &String::from("hi");
1515
let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
1616
let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`

0 commit comments

Comments
 (0)