2019-02-10 09:19:24 +00:00
|
|
|
#![warn(clippy::if_same_then_else)]
|
|
|
|
#![allow(
|
2022-08-11 17:42:16 +00:00
|
|
|
clippy::disallowed_names,
|
2019-02-10 09:19:24 +00:00
|
|
|
clippy::eq_op,
|
|
|
|
clippy::never_loop,
|
|
|
|
clippy::no_effect,
|
2020-01-19 06:04:41 +00:00
|
|
|
clippy::unused_unit,
|
2021-04-08 15:50:13 +00:00
|
|
|
clippy::zero_divided_by_zero,
|
2022-06-30 08:50:09 +00:00
|
|
|
clippy::branches_sharing_code,
|
|
|
|
dead_code,
|
|
|
|
unreachable_code
|
2019-02-10 09:19:24 +00:00
|
|
|
)]
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
bar: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:04:41 +00:00
|
|
|
fn if_same_then_else() {
|
2019-02-10 09:19:24 +00:00
|
|
|
if true {
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2019-02-10 09:19:24 +00:00
|
|
|
Foo { bar: 42 };
|
|
|
|
0..10;
|
|
|
|
..;
|
|
|
|
0..;
|
|
|
|
..10;
|
|
|
|
0..=10;
|
|
|
|
foo();
|
|
|
|
} else {
|
|
|
|
Foo { bar: 42 };
|
|
|
|
0..10;
|
|
|
|
..;
|
|
|
|
0..;
|
|
|
|
..10;
|
|
|
|
0..=10;
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
Foo { bar: 42 };
|
|
|
|
} else {
|
|
|
|
Foo { bar: 43 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
();
|
|
|
|
} else {
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
0..10;
|
|
|
|
} else {
|
|
|
|
0..=10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
} else {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = if true {
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2019-02-10 09:19:24 +00:00
|
|
|
0.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = if true {
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2019-02-10 09:19:24 +00:00
|
|
|
-0.0
|
|
|
|
} else {
|
|
|
|
-0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = if true { 0.0 } else { -0.0 };
|
|
|
|
|
|
|
|
// Different NaNs
|
2020-04-07 22:04:33 +00:00
|
|
|
let _ = if true { 0.0 / 0.0 } else { f32::NAN };
|
2019-02-10 09:19:24 +00:00
|
|
|
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = if true {
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2019-02-10 09:19:24 +00:00
|
|
|
42
|
|
|
|
} else {
|
|
|
|
42
|
|
|
|
};
|
|
|
|
|
|
|
|
if true {
|
2023-07-02 12:35:19 +00:00
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2019-02-10 09:19:24 +00:00
|
|
|
let bar = if true { 42 } else { 43 };
|
|
|
|
|
|
|
|
while foo() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bar + 1;
|
|
|
|
} else {
|
|
|
|
let bar = if true { 42 } else { 43 };
|
|
|
|
|
|
|
|
while foo() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bar + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
let _ = match 42 {
|
|
|
|
42 => 1,
|
|
|
|
a if a > 0 => 2,
|
|
|
|
10..=15 => 3,
|
|
|
|
_ => 4,
|
|
|
|
};
|
|
|
|
} else if false {
|
|
|
|
foo();
|
|
|
|
} else if foo() {
|
|
|
|
let _ = match 42 {
|
|
|
|
42 => 1,
|
|
|
|
a if a > 0 => 2,
|
|
|
|
10..=15 => 3,
|
|
|
|
_ => 4,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
// Issue #2423. This was causing an ICE.
|
2019-02-10 09:19:24 +00:00
|
|
|
fn func() {
|
|
|
|
if true {
|
|
|
|
f(&[0; 62]);
|
|
|
|
f(&[0; 4]);
|
|
|
|
f(&[0; 3]);
|
|
|
|
} else {
|
|
|
|
f(&[0; 62]);
|
|
|
|
f(&[0; 6]);
|
|
|
|
f(&[0; 6]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f(val: &[u8]) {}
|
|
|
|
|
2020-06-23 15:05:22 +00:00
|
|
|
mod issue_5698 {
|
|
|
|
fn mul_not_always_commutative(x: i32, y: i32) -> i32 {
|
|
|
|
if x == 42 {
|
|
|
|
x * y
|
|
|
|
} else if x == 21 {
|
|
|
|
y * x
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 08:50:09 +00:00
|
|
|
mod issue_8836 {
|
|
|
|
fn do_not_lint() {
|
|
|
|
if true {
|
|
|
|
todo!()
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
todo!();
|
|
|
|
} else {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
unimplemented!()
|
|
|
|
} else {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
unimplemented!();
|
|
|
|
} else {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
println!("FOO");
|
|
|
|
todo!();
|
|
|
|
} else {
|
|
|
|
println!("FOO");
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
println!("FOO");
|
|
|
|
unimplemented!();
|
|
|
|
} else {
|
|
|
|
println!("FOO");
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
println!("FOO");
|
|
|
|
todo!()
|
|
|
|
} else {
|
|
|
|
println!("FOO");
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
println!("FOO");
|
|
|
|
unimplemented!()
|
|
|
|
} else {
|
|
|
|
println!("FOO");
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:53:53 +00:00
|
|
|
mod issue_11213 {
|
|
|
|
fn reproducer(x: bool) -> bool {
|
|
|
|
if x {
|
|
|
|
0_u8.is_power_of_two()
|
|
|
|
} else {
|
|
|
|
0_u16.is_power_of_two()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a more obvious reproducer that shows
|
|
|
|
// why the code above is problematic:
|
|
|
|
fn v2(x: bool) -> bool {
|
|
|
|
trait Helper {
|
|
|
|
fn is_u8(&self) -> bool;
|
|
|
|
}
|
|
|
|
impl Helper for u8 {
|
|
|
|
fn is_u8(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Helper for u16 {
|
|
|
|
fn is_u8(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is certainly not the same code in both branches
|
|
|
|
// it returns a different bool depending on the branch.
|
|
|
|
if x { 0_u8.is_u8() } else { 0_u16.is_u8() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_lint(x: bool) -> bool {
|
|
|
|
// but do lint if the type of the literal is the same
|
|
|
|
if x {
|
|
|
|
0_u8.is_power_of_two()
|
|
|
|
} else {
|
|
|
|
0_u8.is_power_of_two()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 09:19:24 +00:00
|
|
|
fn main() {}
|