diff --git a/tests/ui/if_same_then_else.rs b/tests/ui/if_same_then_else.rs index ecdc5623c..67b4c3110 100644 --- a/tests/ui/if_same_then_else.rs +++ b/tests/ui/if_same_then_else.rs @@ -1,14 +1,11 @@ #![warn(clippy::if_same_then_else)] #![allow( clippy::blacklisted_name, - clippy::collapsible_if, - clippy::cognitive_complexity, clippy::eq_op, - clippy::needless_return, clippy::never_loop, clippy::no_effect, - clippy::zero_divided_by_zero, - clippy::unused_unit + clippy::unused_unit, + clippy::zero_divided_by_zero )] struct Foo { @@ -19,7 +16,7 @@ fn foo() -> bool { unimplemented!() } -fn if_same_then_else() -> Result<&'static str, ()> { +fn if_same_then_else() { if true { Foo { bar: 42 }; 0..10; @@ -94,27 +91,6 @@ fn if_same_then_else() -> Result<&'static str, ()> { 42 }; - if true { - for _ in &[42] { - let foo: &Option<_> = &Some::(42); - if true { - break; - } else { - continue; - } - } - } else { - //~ ERROR same body as `if` block - for _ in &[42] { - let foo: &Option<_> = &Some::(42); - if true { - break; - } else { - continue; - } - } - } - if true { let bar = if true { 42 } else { 43 }; @@ -149,113 +125,6 @@ fn if_same_then_else() -> Result<&'static str, ()> { _ => 4, }; } - - if true { - if let Some(a) = Some(42) {} - } else { - //~ ERROR same body as `if` block - if let Some(a) = Some(42) {} - } - - if true { - if let (1, .., 3) = (1, 2, 3) {} - } else { - //~ ERROR same body as `if` block - if let (1, .., 3) = (1, 2, 3) {} - } - - if true { - if let (1, .., 3) = (1, 2, 3) {} - } else { - if let (.., 3) = (1, 2, 3) {} - } - - if true { - if let (1, .., 3) = (1, 2, 3) {} - } else { - if let (.., 4) = (1, 2, 3) {} - } - - if true { - if let (1, .., 3) = (1, 2, 3) {} - } else { - if let (.., 1, 3) = (1, 2, 3) {} - } - - if true { - if let Some(42) = None {} - } else { - if let Option::Some(42) = None {} - } - - if true { - if let Some(42) = None:: {} - } else { - if let Some(42) = None {} - } - - if true { - if let Some(42) = None:: {} - } else { - if let Some(42) = None:: {} - } - - if true { - if let Some(a) = Some(42) {} - } else { - if let Some(a) = Some(43) {} - } - - // Same NaNs - let _ = if true { - std::f32::NAN - } else { - //~ ERROR same body as `if` block - std::f32::NAN - }; - - if true { - Ok("foo")?; - } else { - //~ ERROR same body as `if` block - Ok("foo")?; - } - - if true { - let foo = ""; - return Ok(&foo[0..]); - } else if false { - let foo = "bar"; - return Ok(&foo[0..]); - } else { - let foo = ""; - return Ok(&foo[0..]); - } - - if true { - let foo = ""; - return Ok(&foo[0..]); - } else if false { - let foo = "bar"; - return Ok(&foo[0..]); - } else if true { - let foo = ""; - return Ok(&foo[0..]); - } else { - let foo = ""; - return Ok(&foo[0..]); - } - - // False positive `if_same_then_else`: `let (x, y)` vs. `let (y, x)`; see issue #3559. - if true { - let foo = ""; - let (x, y) = (1, 2); - return Ok(&foo[x..y]); - } else { - let foo = ""; - let (y, x) = (1, 2); - return Ok(&foo[x..y]); - } } // Issue #2423. This was causing an ICE. diff --git a/tests/ui/if_same_then_else.stderr b/tests/ui/if_same_then_else.stderr index 0c2a1fdb7..d9fdf06fa 100644 --- a/tests/ui/if_same_then_else.stderr +++ b/tests/ui/if_same_then_else.stderr @@ -1,5 +1,5 @@ error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:31:12 + --> $DIR/if_same_then_else.rs:28:12 | LL | } else { | ____________^ @@ -13,7 +13,7 @@ LL | | } | = note: `-D clippy::if-same-then-else` implied by `-D warnings` note: same as this - --> $DIR/if_same_then_else.rs:23:13 + --> $DIR/if_same_then_else.rs:20:13 | LL | if true { | _____________^ @@ -26,7 +26,7 @@ LL | | } else { | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:69:12 + --> $DIR/if_same_then_else.rs:66:12 | LL | } else { | ____________^ @@ -36,7 +36,7 @@ LL | | }; | |_____^ | note: same as this - --> $DIR/if_same_then_else.rs:67:21 + --> $DIR/if_same_then_else.rs:64:21 | LL | let _ = if true { | _____________________^ @@ -45,7 +45,7 @@ LL | | } else { | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:76:12 + --> $DIR/if_same_then_else.rs:73:12 | LL | } else { | ____________^ @@ -55,7 +55,7 @@ LL | | }; | |_____^ | note: same as this - --> $DIR/if_same_then_else.rs:74:21 + --> $DIR/if_same_then_else.rs:71:21 | LL | let _ = if true { | _____________________^ @@ -64,7 +64,7 @@ LL | | } else { | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:92:12 + --> $DIR/if_same_then_else.rs:89:12 | LL | } else { | ____________^ @@ -74,7 +74,7 @@ LL | | }; | |_____^ | note: same as this - --> $DIR/if_same_then_else.rs:90:21 + --> $DIR/if_same_then_else.rs:87:21 | LL | let _ = if true { | _____________________^ @@ -83,33 +83,7 @@ LL | | } else { | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:106:12 - | -LL | } else { - | ____________^ -LL | | //~ ERROR same body as `if` block -LL | | for _ in &[42] { -LL | | let foo: &Option<_> = &Some::(42); -... | -LL | | } -LL | | } - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:97:13 - | -LL | if true { - | _____________^ -LL | | for _ in &[42] { -LL | | let foo: &Option<_> = &Some::(42); -LL | | if true { -... | -LL | | } -LL | | } else { - | |_____^ - -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:125:12 + --> $DIR/if_same_then_else.rs:101:12 | LL | } else { | ____________^ @@ -122,7 +96,7 @@ LL | | } | |_____^ | note: same as this - --> $DIR/if_same_then_else.rs:118:13 + --> $DIR/if_same_then_else.rs:94:13 | LL | if true { | _____________^ @@ -134,114 +108,5 @@ LL | | bar + 1; LL | | } else { | |_____^ -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:155:12 - | -LL | } else { - | ____________^ -LL | | //~ ERROR same body as `if` block -LL | | if let Some(a) = Some(42) {} -LL | | } - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:153:13 - | -LL | if true { - | _____________^ -LL | | if let Some(a) = Some(42) {} -LL | | } else { - | |_____^ - -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:162:12 - | -LL | } else { - | ____________^ -LL | | //~ ERROR same body as `if` block -LL | | if let (1, .., 3) = (1, 2, 3) {} -LL | | } - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:160:13 - | -LL | if true { - | _____________^ -LL | | if let (1, .., 3) = (1, 2, 3) {} -LL | | } else { - | |_____^ - -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:212:12 - | -LL | } else { - | ____________^ -LL | | //~ ERROR same body as `if` block -LL | | std::f32::NAN -LL | | }; - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:210:21 - | -LL | let _ = if true { - | _____________________^ -LL | | std::f32::NAN -LL | | } else { - | |_____^ - -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:219:12 - | -LL | } else { - | ____________^ -LL | | //~ ERROR same body as `if` block -LL | | Ok("foo")?; -LL | | } - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:217:13 - | -LL | if true { - | _____________^ -LL | | Ok("foo")?; -LL | | } else { - | |_____^ - -error: this `if` has identical blocks - --> $DIR/if_same_then_else.rs:244:12 - | -LL | } else { - | ____________^ -LL | | let foo = ""; -LL | | return Ok(&foo[0..]); -LL | | } - | |_____^ - | -note: same as this - --> $DIR/if_same_then_else.rs:241:20 - | -LL | } else if true { - | ____________________^ -LL | | let foo = ""; -LL | | return Ok(&foo[0..]); -LL | | } else { - | |_____^ - -error: this `if` has the same condition as a previous `if` - --> $DIR/if_same_then_else.rs:241:15 - | -LL | } else if true { - | ^^^^ - | - = note: `#[deny(clippy::ifs_same_cond)]` on by default -note: same as this - --> $DIR/if_same_then_else.rs:235:8 - | -LL | if true { - | ^^^^ - -error: aborting due to 12 previous errors +error: aborting due to 5 previous errors diff --git a/tests/ui/if_same_then_else2.rs b/tests/ui/if_same_then_else2.rs new file mode 100644 index 000000000..8e61bf383 --- /dev/null +++ b/tests/ui/if_same_then_else2.rs @@ -0,0 +1,140 @@ +#![warn(clippy::if_same_then_else)] +#![allow( + clippy::blacklisted_name, + clippy::cognitive_complexity, + clippy::collapsible_if, + clippy::ifs_same_cond, + clippy::needless_return +)] + +fn if_same_then_else2() -> Result<&'static str, ()> { + if true { + for _ in &[42] { + let foo: &Option<_> = &Some::(42); + if true { + break; + } else { + continue; + } + } + } else { + //~ ERROR same body as `if` block + for _ in &[42] { + let foo: &Option<_> = &Some::(42); + if true { + break; + } else { + continue; + } + } + } + + if true { + if let Some(a) = Some(42) {} + } else { + //~ ERROR same body as `if` block + if let Some(a) = Some(42) {} + } + + if true { + if let (1, .., 3) = (1, 2, 3) {} + } else { + //~ ERROR same body as `if` block + if let (1, .., 3) = (1, 2, 3) {} + } + + if true { + if let (1, .., 3) = (1, 2, 3) {} + } else { + if let (.., 3) = (1, 2, 3) {} + } + + if true { + if let (1, .., 3) = (1, 2, 3) {} + } else { + if let (.., 4) = (1, 2, 3) {} + } + + if true { + if let (1, .., 3) = (1, 2, 3) {} + } else { + if let (.., 1, 3) = (1, 2, 3) {} + } + + if true { + if let Some(42) = None {} + } else { + if let Option::Some(42) = None {} + } + + if true { + if let Some(42) = None:: {} + } else { + if let Some(42) = None {} + } + + if true { + if let Some(42) = None:: {} + } else { + if let Some(42) = None:: {} + } + + if true { + if let Some(a) = Some(42) {} + } else { + if let Some(a) = Some(43) {} + } + + // Same NaNs + let _ = if true { + std::f32::NAN + } else { + //~ ERROR same body as `if` block + std::f32::NAN + }; + + if true { + Ok("foo")?; + } else { + //~ ERROR same body as `if` block + Ok("foo")?; + } + + if true { + let foo = ""; + return Ok(&foo[0..]); + } else if false { + let foo = "bar"; + return Ok(&foo[0..]); + } else { + let foo = ""; + return Ok(&foo[0..]); + } + + if true { + let foo = ""; + return Ok(&foo[0..]); + } else if false { + let foo = "bar"; + return Ok(&foo[0..]); + } else if true { + let foo = ""; + return Ok(&foo[0..]); + } else { + let foo = ""; + return Ok(&foo[0..]); + } + + // False positive `if_same_then_else`: `let (x, y)` vs. `let (y, x)`; see issue #3559. + if true { + let foo = ""; + let (x, y) = (1, 2); + return Ok(&foo[x..y]); + } else { + let foo = ""; + let (y, x) = (1, 2); + return Ok(&foo[x..y]); + } +} + +fn main() {} diff --git a/tests/ui/if_same_then_else2.stderr b/tests/ui/if_same_then_else2.stderr new file mode 100644 index 000000000..c6da3c6be --- /dev/null +++ b/tests/ui/if_same_then_else2.stderr @@ -0,0 +1,125 @@ +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:20:12 + | +LL | } else { + | ____________^ +LL | | //~ ERROR same body as `if` block +LL | | for _ in &[42] { +LL | | let foo: &Option<_> = &Some::(42); +... | +LL | | } +LL | | } + | |_____^ + | + = note: `-D clippy::if-same-then-else` implied by `-D warnings` +note: same as this + --> $DIR/if_same_then_else2.rs:11:13 + | +LL | if true { + | _____________^ +LL | | for _ in &[42] { +LL | | let foo: &Option<_> = &Some::(42); +LL | | if true { +... | +LL | | } +LL | | } else { + | |_____^ + +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:34:12 + | +LL | } else { + | ____________^ +LL | | //~ ERROR same body as `if` block +LL | | if let Some(a) = Some(42) {} +LL | | } + | |_____^ + | +note: same as this + --> $DIR/if_same_then_else2.rs:32:13 + | +LL | if true { + | _____________^ +LL | | if let Some(a) = Some(42) {} +LL | | } else { + | |_____^ + +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:41:12 + | +LL | } else { + | ____________^ +LL | | //~ ERROR same body as `if` block +LL | | if let (1, .., 3) = (1, 2, 3) {} +LL | | } + | |_____^ + | +note: same as this + --> $DIR/if_same_then_else2.rs:39:13 + | +LL | if true { + | _____________^ +LL | | if let (1, .., 3) = (1, 2, 3) {} +LL | | } else { + | |_____^ + +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:91:12 + | +LL | } else { + | ____________^ +LL | | //~ ERROR same body as `if` block +LL | | std::f32::NAN +LL | | }; + | |_____^ + | +note: same as this + --> $DIR/if_same_then_else2.rs:89:21 + | +LL | let _ = if true { + | _____________________^ +LL | | std::f32::NAN +LL | | } else { + | |_____^ + +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:98:12 + | +LL | } else { + | ____________^ +LL | | //~ ERROR same body as `if` block +LL | | Ok("foo")?; +LL | | } + | |_____^ + | +note: same as this + --> $DIR/if_same_then_else2.rs:96:13 + | +LL | if true { + | _____________^ +LL | | Ok("foo")?; +LL | | } else { + | |_____^ + +error: this `if` has identical blocks + --> $DIR/if_same_then_else2.rs:123:12 + | +LL | } else { + | ____________^ +LL | | let foo = ""; +LL | | return Ok(&foo[0..]); +LL | | } + | |_____^ + | +note: same as this + --> $DIR/if_same_then_else2.rs:120:20 + | +LL | } else if true { + | ____________________^ +LL | | let foo = ""; +LL | | return Ok(&foo[0..]); +LL | | } else { + | |_____^ + +error: aborting due to 6 previous errors +