--- /dev/null
+// 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!");
+ }
+ }
+}
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" {
|
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" {
|
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" {
|
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" {
|
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" {
|
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' {
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:45:12
+ --> $DIR/collapsible_if.rs:48:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:53:12
+ --> $DIR/collapsible_if.rs:56:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:61:12
+ --> $DIR/collapsible_if.rs:64:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:72:12
+ --> $DIR/collapsible_if.rs:75:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:83:12
+ --> $DIR/collapsible_if.rs:86:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:94:12
+ --> $DIR/collapsible_if.rs:97:12
|
LL | } else {
| ____________^
|
error: this `else { if .. }` block can be collapsed
- --> $DIR/collapsible_if.rs:105:12
+ --> $DIR/collapsible_if.rs:108:12
|
LL | } else {
| ____________^
|
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