diff --git a/tests/ui/collapsible_if.fixed b/tests/ui/collapsible_if.fixed new file mode 100644 index 00000000000..2c6dd95a637 --- /dev/null +++ b/tests/ui/collapsible_if.fixed @@ -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!"); + } + } +} diff --git a/tests/ui/collapsible_if.rs b/tests/ui/collapsible_if.rs index e8918ddecb5..f482d7704de 100644 --- a/tests/ui/collapsible_if.rs +++ b/tests/ui/collapsible_if.rs @@ -1,3 +1,6 @@ +// run-rustfix +#![allow(clippy::cyclomatic_complexity)] + #[rustfmt::skip] #[warn(clippy::collapsible_if)] fn main() { diff --git a/tests/ui/collapsible_if.stderr b/tests/ui/collapsible_if.stderr index 1b9195563e5..d6d0b9d5d4e 100644 --- a/tests/ui/collapsible_if.stderr +++ b/tests/ui/collapsible_if.stderr @@ -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