about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-02-16 16:05:02 +0100
committerSamuel Tardieu <sam@rfc1149.net>2025-03-22 15:12:42 +0100
commit82381608c993262b562e56b8b52ff47b42650c8e (patch)
treeb67f34a631f243eefb22bd65ce043acb31817df9 /tests
parente8e0126c1804d6c579ec747c32fe4200aaa49699 (diff)
downloadrust-82381608c993262b562e56b8b52ff47b42650c8e.tar.gz
rust-82381608c993262b562e56b8b52ff47b42650c8e.zip
Lint more cases in `collapsible_if`
Replace the use of `Sugg::ast()` which prevented combining `if`
together when they contained comments by span manipulation.

A new configuration option `lint_commented_code`, which is `true` by
default, opts out from this behavior.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui-toml/collapsible_if/clippy.toml1
-rw-r--r--tests/ui-toml/collapsible_if/collapsible_if.fixed34
-rw-r--r--tests/ui-toml/collapsible_if/collapsible_if.rs38
-rw-r--r--tests/ui-toml/collapsible_if/collapsible_if.stderr80
-rw-r--r--tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr3
-rw-r--r--tests/ui/collapsible_if.fixed102
-rw-r--r--tests/ui/collapsible_if.rs52
-rw-r--r--tests/ui/collapsible_if.stderr91
8 files changed, 281 insertions, 120 deletions
diff --git a/tests/ui-toml/collapsible_if/clippy.toml b/tests/ui-toml/collapsible_if/clippy.toml
new file mode 100644
index 00000000000..592cea90cff
--- /dev/null
+++ b/tests/ui-toml/collapsible_if/clippy.toml
@@ -0,0 +1 @@
+lint-commented-code = true
diff --git a/tests/ui-toml/collapsible_if/collapsible_if.fixed b/tests/ui-toml/collapsible_if/collapsible_if.fixed
new file mode 100644
index 00000000000..f695f9804d5
--- /dev/null
+++ b/tests/ui-toml/collapsible_if/collapsible_if.fixed
@@ -0,0 +1,34 @@
+#![allow(clippy::eq_op, clippy::nonminimal_bool)]
+
+#[rustfmt::skip]
+#[warn(clippy::collapsible_if)]
+fn main() {
+    let (x, y) = ("hello", "world");
+
+    if x == "hello"
+        // Comment must be kept
+        && y == "world" {
+            println!("Hello world!");
+        }
+    //~^^^^^^ collapsible_if
+
+    // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
+    if x == "hello" // Inner comment
+        && y == "world" {
+            println!("Hello world!");
+        }
+    //~^^^^^ collapsible_if
+
+    if x == "hello"
+        /* Inner comment */
+        && y == "world" {
+            println!("Hello world!");
+        }
+    //~^^^^^^ collapsible_if
+
+    if x == "hello" /* Inner comment */
+        && y == "world" {
+            println!("Hello world!");
+        }
+    //~^^^^^ collapsible_if
+}
diff --git a/tests/ui-toml/collapsible_if/collapsible_if.rs b/tests/ui-toml/collapsible_if/collapsible_if.rs
new file mode 100644
index 00000000000..868b4adcde5
--- /dev/null
+++ b/tests/ui-toml/collapsible_if/collapsible_if.rs
@@ -0,0 +1,38 @@
+#![allow(clippy::eq_op, clippy::nonminimal_bool)]
+
+#[rustfmt::skip]
+#[warn(clippy::collapsible_if)]
+fn main() {
+    let (x, y) = ("hello", "world");
+
+    if x == "hello" {
+        // Comment must be kept
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+    //~^^^^^^ collapsible_if
+
+    // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
+    if x == "hello" { // Inner comment
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+    //~^^^^^ collapsible_if
+
+    if x == "hello" {
+        /* Inner comment */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+    //~^^^^^^ collapsible_if
+
+    if x == "hello" { /* Inner comment */
+        if y == "world" {
+            println!("Hello world!");
+        }
+    }
+    //~^^^^^ collapsible_if
+}
diff --git a/tests/ui-toml/collapsible_if/collapsible_if.stderr b/tests/ui-toml/collapsible_if/collapsible_if.stderr
new file mode 100644
index 00000000000..a12c2112f58
--- /dev/null
+++ b/tests/ui-toml/collapsible_if/collapsible_if.stderr
@@ -0,0 +1,80 @@
+error: this `if` statement can be collapsed
+  --> tests/ui-toml/collapsible_if/collapsible_if.rs:8:5
+   |
+LL | /     if x == "hello" {
+LL | |         // Comment must be kept
+LL | |         if y == "world" {
+LL | |             println!("Hello world!");
+LL | |         }
+LL | |     }
+   | |_____^
+   |
+   = note: `-D clippy::collapsible-if` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::collapsible_if)]`
+help: collapse nested if block
+   |
+LL ~     if x == "hello"
+LL |         // Comment must be kept
+LL ~         && y == "world" {
+LL |             println!("Hello world!");
+LL ~         }
+   |
+
+error: this `if` statement can be collapsed
+  --> tests/ui-toml/collapsible_if/collapsible_if.rs:17:5
+   |
+LL | /     if x == "hello" { // Inner comment
+LL | |         if y == "world" {
+LL | |             println!("Hello world!");
+LL | |         }
+LL | |     }
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if x == "hello" // Inner comment
+LL ~         && y == "world" {
+LL |             println!("Hello world!");
+LL ~         }
+   |
+
+error: this `if` statement can be collapsed
+  --> tests/ui-toml/collapsible_if/collapsible_if.rs:24:5
+   |
+LL | /     if x == "hello" {
+LL | |         /* Inner comment */
+LL | |         if y == "world" {
+LL | |             println!("Hello world!");
+LL | |         }
+LL | |     }
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if x == "hello"
+LL |         /* Inner comment */
+LL ~         && y == "world" {
+LL |             println!("Hello world!");
+LL ~         }
+   |
+
+error: this `if` statement can be collapsed
+  --> tests/ui-toml/collapsible_if/collapsible_if.rs:32:5
+   |
+LL | /     if x == "hello" { /* Inner comment */
+LL | |         if y == "world" {
+LL | |             println!("Hello world!");
+LL | |         }
+LL | |     }
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if x == "hello" /* Inner comment */
+LL ~         && y == "world" {
+LL |             println!("Hello world!");
+LL ~         }
+   |
+
+error: aborting due to 4 previous errors
+
diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
index fee5b01b689..de0eefc422e 100644
--- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
+++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
@@ -49,6 +49,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
            future-size-threshold
            ignore-interior-mutability
            large-error-threshold
+           lint-commented-code
            lint-inconsistent-struct-field-initializers
            literal-representation-threshold
            matches-for-let-else
@@ -142,6 +143,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
            future-size-threshold
            ignore-interior-mutability
            large-error-threshold
+           lint-commented-code
            lint-inconsistent-struct-field-initializers
            literal-representation-threshold
            matches-for-let-else
@@ -235,6 +237,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
            future-size-threshold
            ignore-interior-mutability
            large-error-threshold
+           lint-commented-code
            lint-inconsistent-struct-field-initializers
            literal-representation-threshold
            matches-for-let-else
diff --git a/tests/ui/collapsible_if.fixed b/tests/ui/collapsible_if.fixed
index 6e994018aef..df281651ab1 100644
--- a/tests/ui/collapsible_if.fixed
+++ b/tests/ui/collapsible_if.fixed
@@ -12,34 +12,40 @@
 fn main() {
     let x = "hello";
     let y = "world";
-    if x == "hello" && y == "world" {
-        println!("Hello world!");
-    }
+    if x == "hello"
+        && y == "world" {
+            println!("Hello world!");
+        }
     //~^^^^^ collapsible_if
 
-    if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
-        println!("Hello world!");
-    }
+    if (x == "hello" || x == "world")
+        && (y == "world" || y == "hello") {
+            println!("Hello world!");
+        }
     //~^^^^^ collapsible_if
 
-    if x == "hello" && x == "world" && (y == "world" || y == "hello") {
-        println!("Hello world!");
-    }
+    if x == "hello" && x == "world"
+        && (y == "world" || y == "hello") {
+            println!("Hello world!");
+        }
     //~^^^^^ collapsible_if
 
-    if (x == "hello" || x == "world") && y == "world" && y == "hello" {
-        println!("Hello world!");
-    }
+    if (x == "hello" || x == "world")
+        && y == "world" && y == "hello" {
+            println!("Hello world!");
+        }
     //~^^^^^ collapsible_if
 
-    if x == "hello" && x == "world" && y == "world" && y == "hello" {
-        println!("Hello world!");
-    }
+    if x == "hello" && x == "world"
+        && y == "world" && y == "hello" {
+            println!("Hello world!");
+        }
     //~^^^^^ collapsible_if
 
-    if 42 == 1337 && 'a' != 'A' {
-        println!("world!")
-    }
+    if 42 == 1337
+        && 'a' != 'A' {
+            println!("world!")
+        }
     //~^^^^^ collapsible_if
 
     // Works because any if with an else statement cannot be collapsed.
@@ -71,37 +77,17 @@ fn main() {
         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" {
+    if x == "hello"
+        && y == "world" { // Collapsible
             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!");
-    }
     //~^^^^^ collapsible_if
 
     if x == "hello" {
         print!("Hello ");
     } else {
         // Not collapsible
-        if y == "world" {
+        if let Some(42) = Some(42) {
             println!("world!")
         }
     }
@@ -110,21 +96,8 @@ fn main() {
         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!");
+            println!("world!")
         }
     }
 
@@ -150,11 +123,13 @@ fn main() {
     }
 
     // Fix #5962
-    if matches!(true, true) && matches!(true, true) {}
+    if matches!(true, true)
+        && matches!(true, true) {}
     //~^^^ collapsible_if
 
     // Issue #9375
-    if matches!(true, true) && truth() && matches!(true, true) {}
+    if matches!(true, true) && truth()
+        && matches!(true, true) {}
     //~^^^ collapsible_if
 
     if true {
@@ -163,4 +138,17 @@ fn main() {
             println!("Hello world!");
         }
     }
+
+    if true
+        && true {
+            println!("No comment, linted");
+        }
+    //~^^^^^ collapsible_if
+
+    if true {
+        // Do not collapse because of this comment
+        if true {
+            println!("Hello world!");
+        }
+    }
 }
diff --git a/tests/ui/collapsible_if.rs b/tests/ui/collapsible_if.rs
index 5cf591a658c..ce979568cc8 100644
--- a/tests/ui/collapsible_if.rs
+++ b/tests/ui/collapsible_if.rs
@@ -83,27 +83,6 @@ fn main() {
         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!");
@@ -115,7 +94,7 @@ fn main() {
         print!("Hello ");
     } else {
         // Not collapsible
-        if y == "world" {
+        if let Some(42) = Some(42) {
             println!("world!")
         }
     }
@@ -124,21 +103,8 @@ fn main() {
         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!");
+            println!("world!")
         }
     }
 
@@ -181,4 +147,18 @@ fn main() {
             println!("Hello world!");
         }
     }
+
+    if true {
+        if true {
+            println!("No comment, linted");
+        }
+    }
+    //~^^^^^ collapsible_if
+
+    if true {
+        // Do not collapse because of this comment
+        if true {
+            println!("Hello world!");
+        }
+    }
 }
diff --git a/tests/ui/collapsible_if.stderr b/tests/ui/collapsible_if.stderr
index 3cc3fe5534f..559db238cb1 100644
--- a/tests/ui/collapsible_if.stderr
+++ b/tests/ui/collapsible_if.stderr
@@ -12,9 +12,10 @@ LL | |     }
    = help: to override `-D warnings` add `#[allow(clippy::collapsible_if)]`
 help: collapse nested if block
    |
-LL ~     if x == "hello" && y == "world" {
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if x == "hello"
+LL ~         && y == "world" {
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
@@ -29,9 +30,10 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if (x == "hello" || x == "world") {
+LL ~         && (y == "world" || y == "hello") {
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
@@ -46,9 +48,10 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if x == "hello" && x == "world" && (y == "world" || y == "hello") {
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if x == "hello" && x == "world"
+LL ~         && (y == "world" || y == "hello") {
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
@@ -63,9 +66,10 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if (x == "hello" || x == "world") && y == "world" && y == "hello" {
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if (x == "hello" || x == "world") {
+LL ~         && y == "world" && y == "hello" {
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
@@ -80,9 +84,10 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if x == "hello" && x == "world" && y == "world" && y == "hello" {
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if x == "hello" && x == "world"
+LL ~         && y == "world" && y == "hello" {
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
@@ -97,13 +102,14 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if 42 == 1337 && 'a' != 'A' {
-LL +         println!("world!")
-LL +     }
+LL ~     if 42 == 1337
+LL ~         && 'a' != 'A' {
+LL |             println!("world!")
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
-  --> tests/ui/collapsible_if.rs:107:5
+  --> tests/ui/collapsible_if.rs:86:5
    |
 LL | /     if x == "hello" {
 LL | |         if y == "world" { // Collapsible
@@ -114,26 +120,57 @@ LL | |     }
    |
 help: collapse nested if block
    |
-LL ~     if x == "hello" && y == "world" { // Collapsible
-LL +         println!("Hello world!");
-LL +     }
+LL ~     if x == "hello"
+LL ~         && y == "world" { // Collapsible
+LL |             println!("Hello world!");
+LL ~         }
    |
 
 error: this `if` statement can be collapsed
-  --> tests/ui/collapsible_if.rs:167:5
+  --> tests/ui/collapsible_if.rs:133:5
    |
 LL | /     if matches!(true, true) {
 LL | |         if matches!(true, true) {}
 LL | |     }
-   | |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}`
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if matches!(true, true)
+LL ~         && matches!(true, true) {}
+   |
 
 error: this `if` statement can be collapsed
-  --> tests/ui/collapsible_if.rs:173:5
+  --> tests/ui/collapsible_if.rs:139:5
    |
 LL | /     if matches!(true, true) && truth() {
 LL | |         if matches!(true, true) {}
 LL | |     }
-   | |_____^ help: collapse nested if block: `if matches!(true, true) && truth() && matches!(true, true) {}`
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if matches!(true, true) && truth()
+LL ~         && matches!(true, true) {}
+   |
+
+error: this `if` statement can be collapsed
+  --> tests/ui/collapsible_if.rs:151:5
+   |
+LL | /     if true {
+LL | |         if true {
+LL | |             println!("No comment, linted");
+LL | |         }
+LL | |     }
+   | |_____^
+   |
+help: collapse nested if block
+   |
+LL ~     if true
+LL ~         && true {
+LL |             println!("No comment, linted");
+LL ~         }
+   |
 
-error: aborting due to 9 previous errors
+error: aborting due to 10 previous errors