2020-01-19 00:04:41 -06:00
|
|
|
#![warn(clippy::if_same_then_else)]
|
|
|
|
#![allow(
|
2022-08-11 12:42:16 -05:00
|
|
|
clippy::disallowed_names,
|
2021-01-15 03:56:44 -06:00
|
|
|
clippy::collapsible_else_if,
|
2021-10-07 04:21:30 -05:00
|
|
|
clippy::equatable_if_let,
|
2020-01-19 00:04:41 -06:00
|
|
|
clippy::collapsible_if,
|
|
|
|
clippy::ifs_same_cond,
|
2023-07-02 07:35:19 -05:00
|
|
|
clippy::needless_if,
|
2020-10-28 17:36:07 -05:00
|
|
|
clippy::needless_return,
|
2021-04-08 10:50:13 -05:00
|
|
|
clippy::single_element_loop,
|
|
|
|
clippy::branches_sharing_code
|
2020-01-19 00:04:41 -06:00
|
|
|
)]
|
|
|
|
|
|
|
|
fn if_same_then_else2() -> Result<&'static str, ()> {
|
|
|
|
if true {
|
|
|
|
for _ in &[42] {
|
|
|
|
let foo: &Option<_> = &Some::<u8>(42);
|
2021-02-25 04:25:22 -06:00
|
|
|
if foo.is_some() {
|
2020-01-19 00:04:41 -06:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _ in &[42] {
|
2021-02-25 04:25:22 -06:00
|
|
|
let bar: &Option<_> = &Some::<u8>(42);
|
|
|
|
if bar.is_some() {
|
2020-01-19 00:04:41 -06:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 12:17:36 -06:00
|
|
|
//~^^^^^^^^^^^^^^^^^^^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
if true {
|
|
|
|
if let Some(a) = Some(42) {}
|
|
|
|
} else {
|
|
|
|
if let Some(a) = Some(42) {}
|
|
|
|
}
|
2024-01-25 12:17:36 -06:00
|
|
|
//~^^^^^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
if true {
|
|
|
|
if let (1, .., 3) = (1, 2, 3) {}
|
|
|
|
} else {
|
|
|
|
if let (1, .., 3) = (1, 2, 3) {}
|
|
|
|
}
|
2024-01-25 12:17:36 -06:00
|
|
|
//~^^^^^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
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::<u8> {}
|
|
|
|
} else {
|
|
|
|
if let Some(42) = None {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
if let Some(42) = None::<u8> {}
|
|
|
|
} else {
|
|
|
|
if let Some(42) = None::<u32> {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if true {
|
|
|
|
if let Some(a) = Some(42) {}
|
|
|
|
} else {
|
|
|
|
if let Some(a) = Some(43) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same NaNs
|
2024-01-25 12:17:36 -06:00
|
|
|
let _ = if true { f32::NAN } else { f32::NAN };
|
|
|
|
//~^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
if true {
|
|
|
|
Ok("foo")?;
|
|
|
|
} else {
|
|
|
|
Ok("foo")?;
|
|
|
|
}
|
2024-01-25 12:17:36 -06:00
|
|
|
//~^^^^^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
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..]);
|
|
|
|
}
|
2024-01-25 12:17:36 -06:00
|
|
|
//~^^^^^^^ ERROR: this `if` has identical blocks
|
2020-01-19 00:04:41 -06:00
|
|
|
|
|
|
|
// 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]);
|
|
|
|
}
|
2022-01-17 06:29:07 -06:00
|
|
|
|
|
|
|
// Issue #7579
|
|
|
|
let _ = if let Some(0) = None { 0 } else { 0 };
|
|
|
|
|
|
|
|
if true {
|
|
|
|
return Err(());
|
|
|
|
} else if let Some(0) = None {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = if let Some(0) = None {
|
|
|
|
0
|
|
|
|
} else if let Some(1) = None {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2020-01-19 00:04:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|