2023-04-20 09:37:15 -05:00
|
|
|
//@run-rustfix
|
2023-03-10 09:43:04 -06:00
|
|
|
#![allow(
|
|
|
|
clippy::assertions_on_constants,
|
|
|
|
clippy::equatable_if_let,
|
|
|
|
clippy::nonminimal_bool,
|
|
|
|
clippy::eq_op
|
|
|
|
)]
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
#[warn(clippy::collapsible_if)]
|
|
|
|
fn main() {
|
|
|
|
let x = "hello";
|
|
|
|
let y = "world";
|
|
|
|
if x == "hello" && y == "world" {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
if x == "hello" && x == "world" && (y == "world" || y == "hello") {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
if (x == "hello" || x == "world") && y == "world" && y == "hello" {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
if x == "hello" && x == "world" && y == "world" && y == "hello" {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
if 42 == 1337 && 'a' != 'A' {
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("world!")
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
// 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
|
2020-02-04 09:13:06 -06:00
|
|
|
println!("Hello world!");
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 01:17:47 -05:00
|
|
|
|
|
|
|
// Test behavior wrt. `let_chains`.
|
|
|
|
// None of the cases below should be collapsed.
|
|
|
|
fn truth() -> bool { true }
|
|
|
|
|
|
|
|
// Prefix:
|
|
|
|
if let 0 = 1 {
|
|
|
|
if truth() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suffix:
|
|
|
|
if truth() {
|
|
|
|
if let 0 = 1 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Midfix:
|
|
|
|
if truth() {
|
|
|
|
if let 0 = 1 {
|
|
|
|
if truth() {}
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 10:05:53 -05:00
|
|
|
|
|
|
|
// Fix #5962
|
|
|
|
if matches!(true, true) && matches!(true, true) {}
|
2021-02-08 11:00:30 -06:00
|
|
|
|
2022-08-31 11:47:56 -05:00
|
|
|
// Issue #9375
|
|
|
|
if matches!(true, true) && truth() && matches!(true, true) {}
|
|
|
|
|
2021-02-08 11:00:30 -06:00
|
|
|
if true {
|
|
|
|
#[cfg(not(teehee))]
|
|
|
|
if true {
|
|
|
|
println!("Hello world!");
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 04:44:45 -06:00
|
|
|
}
|