]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/collapsible_if.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / collapsible_if.rs
index d40be6319337fed40dd300dbf1613880de23cd18..7b322acae263d75eed3482dd120150677faf7b47 100644 (file)
@@ -1,6 +1,7 @@
-#![feature(tool_lints)]
-
+// run-rustfix
+#![allow(clippy::cognitive_complexity, clippy::assertions_on_constants)]
 
+#[rustfmt::skip]
 #[warn(clippy::collapsible_if)]
 fn main() {
     let x = "hello";
@@ -141,4 +142,83 @@ fn main() {
     } 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" {
+        if 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!");
+        }
+    }
+
+    // Test behavior wrt. `let_chains`.
+    // None of the cases below should be collapsed.
+    fn truth() -> bool { true }
+
+    // Prefix:
+    if let 0 = 1 {
+        if truth() {}
+    }
+
+    // Suffix:
+    if truth() {
+        if let 0 = 1 {}
+    }
+
+    // Midfix:
+    if truth() {
+        if let 0 = 1 {
+            if truth() {}
+        }
+    }
 }