]> git.lizzy.rs Git - rust.git/commitdiff
Use `span_suggestion` for `WHILE_LET_ON_ITERATOR`
authormcarton <cartonmartin+git@gmail.com>
Tue, 7 Jun 2016 16:32:26 +0000 (18:32 +0200)
committermcarton <cartonmartin+git@gmail.com>
Tue, 7 Jun 2016 16:32:40 +0000 (18:32 +0200)
clippy_lints/src/loops.rs
tests/compile-fail/absurd-extreme-comparisons.rs
tests/compile-fail/booleans.rs
tests/compile-fail/collapsible_if.rs
tests/compile-fail/for_loop.rs
tests/compile-fail/matches.rs
tests/compile-fail/mut_mut.rs

index e635b34e1de6bc87ba606f2517a848637e61e671..5dcea35e5a711580724ac6eebf6e53fb9da543ee 100644 (file)
@@ -290,11 +290,15 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
                        !is_iterator_used_after_while_let(cx, iter_expr) {
                         let iterator = snippet(cx, method_args[0].span, "_");
                         let loop_var = snippet(cx, pat_args[0].span, "_");
-                        span_help_and_lint(cx,
+                        span_lint_and_then(cx,
                                            WHILE_LET_ON_ITERATOR,
                                            expr.span,
                                            "this loop could be written as a `for` loop",
-                                           &format!("try\nfor {} in {} {{...}}", loop_var, iterator));
+                                           |db| {
+                        db.span_suggestion(expr.span,
+                                           "try",
+                                           format!("for {} in {} {{ .. }}", loop_var, iterator));
+                        });
                     }
                 }
             }
@@ -598,7 +602,7 @@ fn check_for_loop_over_map_kv(cx: &LateContext, pat: &Pat, arg: &Expr, body: &Ex
                                    |db| {
                                        db.span_suggestion(expr.span,
                                                           "use the corresponding method",
-                                                          format!("for {} in {}.{}() {{...}}",
+                                                          format!("for {} in {}.{}() {{ .. }}",
                                                                   snippet(cx, *pat_span, ".."),
                                                                   snippet(cx, arg_span, ".."),
                                                                   kind));
index f1e4a692800c083f5d353637b3025862d6316a79..627cd888aac1a2880a7097dbc663acda9549fc39 100644 (file)
@@ -8,15 +8,33 @@ fn main() {
 
     let u: u32 = 42;
 
-    u <= 0; //~ERROR this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
-    u <= Z; //~ERROR this comparison involving
-    u < Z; //~ERROR this comparison involving
-    Z >= u; //~ERROR this comparison involving
-    Z > u; //~ERROR this comparison involving
-    u > std::u32::MAX; //~ERROR this comparison involving
-    u >= std::u32::MAX; //~ERROR this comparison involving
-    std::u32::MAX < u; //~ERROR this comparison involving
-    std::u32::MAX <= u; //~ERROR this comparison involving
+    u <= 0;
+    //~^ ERROR this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
+    //~| HELP using u == 0 instead
+    u <= Z;
+    //~^ ERROR this comparison involving
+    //~| HELP using u == Z instead
+    u < Z;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
+    Z >= u;
+    //~^ ERROR this comparison involving
+    //~| HELP using Z == u instead
+    Z > u;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
+    u > std::u32::MAX;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
+    u >= std::u32::MAX;
+    //~^ ERROR this comparison involving
+    //~| HELP using u == std::u32::MAX instead
+    std::u32::MAX < u;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
+    std::u32::MAX <= u;
+    //~^ ERROR this comparison involving
+    //~| HELP using std::u32::MAX == u instead
 
     1-1 > u;
         //~^ ERROR this comparison involving
@@ -29,13 +47,23 @@ fn main() {
         //~| 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
 
     let i: i8 = 0;
-    i < -127 - 1; //~ERROR this comparison involving
-    std::i8::MAX >= i; //~ERROR this comparison involving
-    3-7 < std::i32::MIN; //~ERROR this comparison involving
+    i < -127 - 1;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
+    std::i8::MAX >= i;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always true
+    3-7 < std::i32::MIN;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
 
     let b = false;
-    b >= true; //~ERROR this comparison involving
-    false > b; //~ERROR this comparison involving
+    b >= true;
+    //~^ ERROR this comparison involving
+    //~| HELP using b == true instead
+    false > b;
+    //~^ ERROR this comparison involving
+    //~| HELP comparison is always false
 
     u > 0; // ok
 
index fc220d1ac22279dba26b796c2f7747d9d3d760a3..193edebf3c4ffb8f37f5067a01120404bd43d9d1 100644 (file)
@@ -52,29 +52,37 @@ fn equality_stuff() {
     let c: i32 = unimplemented!();
     let d: i32 = unimplemented!();
     let e: i32 = unimplemented!();
-    let _ = a == b && a != b; //~ ERROR this boolean expression contains a logic bug
+    let _ = a == b && a != b;
+    //~^ ERROR this boolean expression contains a logic bug
     //~| HELP this expression can be optimized out
     //~| HELP it would look like the following
     //~| SUGGESTION let _ = false;
-    let _ = a == b && c == 5 && a == b; //~ ERROR this boolean expression can be simplified
+    let _ = a == b && c == 5 && a == b;
+    //~^ ERROR this boolean expression can be simplified
     //~| HELP try
     //~| SUGGESTION let _ = a == b && c == 5;
-    let _ = a == b && c == 5 && b == a; //~ ERROR this boolean expression can be simplified
+    //~| HELP try
+    //~| SUGGESTION let _ = !(c != 5 || a != b);
+    let _ = a == b && c == 5 && b == a;
+    //~^ ERROR this boolean expression can be simplified
     //~| HELP try
     //~| SUGGESTION let _ = a == b && c == 5;
     //~| HELP try
     //~| SUGGESTION let _ = !(c != 5 || a != b);
-    let _ = a < b && a >= b; //~ ERROR this boolean expression contains a logic bug
+    let _ = a < b && a >= b;
+    //~^ ERROR this boolean expression contains a logic bug
     //~| HELP this expression can be optimized out
     //~| HELP it would look like the following
     //~| SUGGESTION let _ = false;
-    let _ = a > b && a <= b; //~ ERROR this boolean expression contains a logic bug
+    let _ = a > b && a <= b;
+    //~^ ERROR this boolean expression contains a logic bug
     //~| HELP this expression can be optimized out
     //~| HELP it would look like the following
     //~| SUGGESTION let _ = false;
     let _ = a > b && a == b;
 
-    let _ = a != b || !(a != b || c == d); //~ ERROR this boolean expression can be simplified
+    let _ = a != b || !(a != b || c == d);
+    //~^ ERROR this boolean expression can be simplified
     //~| HELP try
     //~| SUGGESTION let _ = c != d || a != b;
     //~| HELP try
index 3bf4128347a365f2d634663929f7035fd73cd74c..34c55499612007cfcf7c2076fadd065a8502cc77 100644 (file)
@@ -5,13 +5,19 @@
 fn main() {
     let x = "hello";
     let y = "world";
-    if x == "hello" { //~ERROR this if statement can be collapsed
+    if x == "hello" {
+    //~^ ERROR this if statement can be collapsed
+    //~| HELP try
+    //~| SUGGESTION if x == "hello" && y == "world" {
         if y == "world" {
             println!("Hello world!");
         }
     }
 
-    if x == "hello" || x == "world" { //~ERROR this if statement can be collapsed
+    if x == "hello" || x == "world" {
+    //~^ ERROR this if statement can be collapsed
+    //~| HELP try
+    //~| SUGGESTION if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
         if y == "world" || y == "hello" {
             println!("Hello world!");
         }
index 2f164d1e5698dab93dbd5b212e4aa9caef2cd171..d35beb617e05cf0fb0e21e06e47f0104f7387617 100644 (file)
@@ -200,11 +200,17 @@ fn main() {
     }
 
     // testing that the empty range lint folds constants
-    for i in 10..5+4 { //~ERROR this range is empty so this for loop will never run
+    for i in 10..5+4 {
+    //~^ ERROR this range is empty so this for loop will never run
+    //~| HELP if you are attempting to iterate over this range in reverse
+    //~| SUGGESTION for i in (5+4..10).rev() {
         println!("{}", i);
     }
 
-    for i in (5+2)..(3-1) { //~ERROR this range is empty so this for loop will never run
+    for i in (5+2)..(3-1) {
+    //~^ ERROR this range is empty so this for loop will never run
+    //~| HELP if you are attempting to iterate over this range in reverse
+    //~| SUGGESTION for i in ((3-1)..(5+2)).rev() {
         println!("{}", i);
     }
 
index affa7e4e86e6e76ab7a24e77eb920d4016451865..650b5917fdcb82581dccb4872500e7a91c9c26f0 100644 (file)
@@ -100,28 +100,43 @@ fn single_match_know_enum() {
 fn match_bool() {
     let test: bool = true;
 
-    match test {  //~ ERROR you seem to be trying to match on a boolean expression
+    match test {
+    //~^ ERROR you seem to be trying to match on a boolean expression
+    //~| HELP try
+    //~| SUGGESTION if test { 0 } else { 42 };
         true => 0,
         false => 42,
     };
 
     let option = 1;
-    match option == 1 {  //~ ERROR you seem to be trying to match on a boolean expression
+    match option == 1 {
+    //~^ ERROR you seem to be trying to match on a boolean expression
+    //~| HELP try
+    //~| SUGGESTION if option == 1 { 1 } else { 0 };
         true => 1,
         false => 0,
     };
 
-    match test { //~ ERROR you seem to be trying to match on a boolean expression
+    match test {
+    //~^ ERROR you seem to be trying to match on a boolean expression
+    //~| HELP try
+    //~^^ SUGGESTION if !test { println!("Noooo!"); };
         true => (),
         false => { println!("Noooo!"); }
     };
 
-    match test { //~ ERROR you seem to be trying to match on a boolean expression
+    match test {
+    //~^ ERROR you seem to be trying to match on a boolean expression
+    //~| HELP try
+    //~^^ SUGGESTION if !test { println!("Noooo!"); };
         false => { println!("Noooo!"); }
         _ => (),
     };
 
-    match test { //~ ERROR you seem to be trying to match on a boolean expression
+    match test {
+    //~^ ERROR you seem to be trying to match on a boolean expression
+    //~| HELP try
+    //~| SUGGESTION if test { println!("Yes!"); } else { println!("Noooo!"); };
         false => { println!("Noooo!"); }
         true => { println!("Yes!"); }
     };
@@ -216,7 +231,7 @@ fn overlapping() {
         11 ... 50 => println!("0 ... 10"),
         _ => (),
     }
-    
+
     if let None = Some(42) {
         // nothing
     } else if let None = Some(42) {
index 92344110d2ccdcbf237b11ba59115fb8d4ca3db1..21c0dcee5115d939aaac47829a9309503db0e571 100644 (file)
@@ -40,5 +40,4 @@ fn main() {
 
     let mut z = mut_ptr!(&mut 3u32);
     //~^ NOTE in this expansion of mut_ptr!
-    //~| NOTE in this expansion of mut_ptr!
 }