]> git.lizzy.rs Git - rust.git/commitdiff
Add run-rustfix to collapsible_if test
authorWilco Kusee <wilcokusee@gmail.com>
Sun, 13 Jan 2019 10:44:45 +0000 (11:44 +0100)
committerWilco Kusee <wilcokusee@gmail.com>
Sun, 13 Jan 2019 10:44:45 +0000 (11:44 +0100)
tests/ui/collapsible_if.fixed [new file with mode: 0644]
tests/ui/collapsible_if.rs
tests/ui/collapsible_if.stderr

diff --git a/tests/ui/collapsible_if.fixed b/tests/ui/collapsible_if.fixed
new file mode 100644 (file)
index 0000000..2c6dd95
--- /dev/null
@@ -0,0 +1,175 @@
+// run-rustfix
+#![allow(clippy::cyclomatic_complexity)]
+
+#[rustfmt::skip]
+#[warn(clippy::collapsible_if)]
+fn main() {
+    let x = "hello";
+    let y = "world";
+    if x == "hello" && y == "world" {
+    println!("Hello world!");
+}
+
+    if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
+    println!("Hello world!");
+}
+
+    if x == "hello" && x == "world" && (y == "world" || y == "hello") {
+    println!("Hello world!");
+}
+
+    if (x == "hello" || x == "world") && y == "world" && y == "hello" {
+    println!("Hello world!");
+}
+
+    if x == "hello" && x == "world" && y == "world" && y == "hello" {
+    println!("Hello world!");
+}
+
+    if 42 == 1337 && 'a' != 'A' {
+    println!("world!")
+}
+
+    // Collapse `else { if .. }` to `else if ..`
+    if x == "hello" {
+        print!("Hello ");
+    } else if y == "world" {
+    println!("world!")
+}
+
+    if x == "hello" {
+        print!("Hello ");
+    } else if let Some(42) = Some(42) {
+    println!("world!")
+}
+
+    if x == "hello" {
+        print!("Hello ");
+    } else if y == "world" {
+    println!("world")
+}
+else {
+    println!("!")
+}
+
+    if x == "hello" {
+        print!("Hello ");
+    } else if let Some(42) = Some(42) {
+    println!("world")
+}
+else {
+    println!("!")
+}
+
+    if let Some(42) = Some(42) {
+        print!("Hello ");
+    } else if let Some(42) = Some(42) {
+    println!("world")
+}
+else {
+    println!("!")
+}
+
+    if let Some(42) = Some(42) {
+        print!("Hello ");
+    } else if x == "hello" {
+    println!("world")
+}
+else {
+    println!("!")
+}
+
+    if let Some(42) = Some(42) {
+        print!("Hello ");
+    } else if let Some(42) = Some(42) {
+    println!("world")
+}
+else {
+    println!("!")
+}
+
+    // Works because any if with an else statement cannot be collapsed.
+    if x == "hello" {
+        if y == "world" {
+            println!("Hello world!");
+        }
+    } else {
+        println!("Not Hello world");
+    }
+
+    if x == "hello" {
+        if y == "world" {
+            println!("Hello world!");
+        } else {
+            println!("Hello something else");
+        }
+    }
+
+    if x == "hello" {
+        print!("Hello ");
+        if y == "world" {
+            println!("world!")
+        }
+    }
+
+    if true {
+    } else {
+        assert!(true); // assert! is just an `if`
+    }
+
+
+    // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
+    if x == "hello" {// Not collapsible
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+
+    if x == "hello" { // Not collapsible
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+
+    if x == "hello" {
+        // Not collapsible
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+
+    if x == "hello" && y == "world" { // Collapsible
+    println!("Hello world!");
+}
+
+    if x == "hello" {
+        print!("Hello ");
+    } else {
+        // Not collapsible
+        if y == "world" {
+            println!("world!")
+        }
+    }
+
+    if x == "hello" {
+        print!("Hello ");
+    } else {
+        // Not collapsible
+        if let Some(42) = Some(42) {
+            println!("world!")
+        }
+    }
+
+    if x == "hello" {
+        /* Not collapsible */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+
+    if x == "hello" { /* Not collapsible */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+}
index e8918ddecb560fba1d55797061c1fd49ed0b8e1f..f482d7704def92b7103bc9dafb6251944feb84c2 100644 (file)
@@ -1,3 +1,6 @@
+// run-rustfix
+#![allow(clippy::cyclomatic_complexity)]
+
 #[rustfmt::skip]
 #[warn(clippy::collapsible_if)]
 fn main() {
index 1b9195563e51c5bcbef20e9336700497b4a9a47a..d6d0b9d5d4eaa276de835167837ef68718173354 100644 (file)
@@ -1,5 +1,5 @@
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:6:5
+  --> $DIR/collapsible_if.rs:9:5
    |
 LL | /     if x == "hello" {
 LL | |         if y == "world" {
@@ -17,7 +17,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:12:5
+  --> $DIR/collapsible_if.rs:15:5
    |
 LL | /     if x == "hello" || x == "world" {
 LL | |         if y == "world" || y == "hello" {
@@ -33,7 +33,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:18:5
+  --> $DIR/collapsible_if.rs:21:5
    |
 LL | /     if x == "hello" && x == "world" {
 LL | |         if y == "world" || y == "hello" {
@@ -49,7 +49,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:24:5
+  --> $DIR/collapsible_if.rs:27:5
    |
 LL | /     if x == "hello" || x == "world" {
 LL | |         if y == "world" && y == "hello" {
@@ -65,7 +65,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:30:5
+  --> $DIR/collapsible_if.rs:33:5
    |
 LL | /     if x == "hello" && x == "world" {
 LL | |         if y == "world" && y == "hello" {
@@ -81,7 +81,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:36:5
+  --> $DIR/collapsible_if.rs:39:5
    |
 LL | /     if 42 == 1337 {
 LL | |         if 'a' != 'A' {
@@ -97,7 +97,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:45:12
+  --> $DIR/collapsible_if.rs:48:12
    |
 LL |       } else {
    |  ____________^
@@ -114,7 +114,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:53:12
+  --> $DIR/collapsible_if.rs:56:12
    |
 LL |       } else {
    |  ____________^
@@ -131,7 +131,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:61:12
+  --> $DIR/collapsible_if.rs:64:12
    |
 LL |       } else {
    |  ____________^
@@ -153,7 +153,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:72:12
+  --> $DIR/collapsible_if.rs:75:12
    |
 LL |       } else {
    |  ____________^
@@ -175,7 +175,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:83:12
+  --> $DIR/collapsible_if.rs:86:12
    |
 LL |       } else {
    |  ____________^
@@ -197,7 +197,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:94:12
+  --> $DIR/collapsible_if.rs:97:12
    |
 LL |       } else {
    |  ____________^
@@ -219,7 +219,7 @@ LL | }
    |
 
 error: this `else { if .. }` block can be collapsed
-  --> $DIR/collapsible_if.rs:105:12
+  --> $DIR/collapsible_if.rs:108:12
    |
 LL |       } else {
    |  ____________^
@@ -241,7 +241,7 @@ LL | }
    |
 
 error: this if statement can be collapsed
-  --> $DIR/collapsible_if.rs:164:5
+  --> $DIR/collapsible_if.rs:167:5
    |
 LL | /     if x == "hello" {
 LL | |         if y == "world" { // Collapsible