| #![allow(clippy::eq_op, clippy::needless_ifs)] | |
| #![expect(clippy::assertions_on_constants, clippy::redundant_pattern_matching)] | |
| #[rustfmt::skip] | |
| #[warn(clippy::collapsible_if)] | |
| fn main() { | |
| let x = "hello"; | |
| let y = "world"; | |
| if x == "hello" | |
| && y == "world" { | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if (x == "hello" || x == "world") | |
| && (y == "world" || y == "hello") { | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if x == "hello" && x == "world" | |
| && (y == "world" || y == "hello") { | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if (x == "hello" || x == "world") | |
| && y == "world" && y == "hello" { | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if x == "hello" && x == "world" | |
| && y == "world" && y == "hello" { | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if 42 == 1337 | |
| && 'a' != 'A' { | |
| println!("world!") | |
| } | |
| //~^^^^^ collapsible_if | |
| // 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` | |
| } | |
| if x == "hello" | |
| && y == "world" { // Collapsible | |
| println!("Hello world!"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if x == "hello" { | |
| print!("Hello "); | |
| } else { | |
| // Not collapsible | |
| if let Some(42) = Some(42) { | |
| println!("world!") | |
| } | |
| } | |
| if x == "hello" { | |
| print!("Hello "); | |
| } else { | |
| // Not collapsible | |
| if y == "world" { | |
| println!("world!") | |
| } | |
| } | |
| fn truth() -> bool { true } | |
| // Fix #5962 | |
| if matches!(true, true) | |
| && matches!(true, true) {} | |
| //~^^^ collapsible_if | |
| // Issue #9375 | |
| if matches!(true, true) && truth() | |
| && matches!(true, true) {} | |
| //~^^^ collapsible_if | |
| if true { | |
| #[cfg(not(teehee))] | |
| if true { | |
| println!("Hello world!"); | |
| } | |
| } | |
| if true | |
| && true { | |
| println!("No comment, linted"); | |
| } | |
| //~^^^^^ collapsible_if | |
| if true { | |
| // Do not collapse because of this comment | |
| if true { | |
| println!("Hello world!"); | |
| } | |
| } | |
| } | |
| #[rustfmt::skip] | |
| fn layout_check() -> u32 { | |
| if true | |
| && true { | |
| } | |
| // This is a comment, do not collapse code to it | |
| ; 3 | |
| //~^^^^^ collapsible_if | |
| } | |
| fn issue13365() { | |
| // all the `expect`s that we should fulfill | |
| if true { | |
| #[expect(clippy::collapsible_if)] | |
| if true {} | |
| } | |
| if true { | |
| #[expect(clippy::style)] | |
| if true {} | |
| } | |
| if true { | |
| #[expect(clippy::all)] | |
| if true {} | |
| } | |
| } | |
| fn issue14722() { | |
| let x = if true { | |
| Some(1) | |
| } else { | |
| if true { | |
| println!("Some debug information"); | |
| }; | |
| None | |
| }; | |
| } | |
| fn issue14799() { | |
| if true { | |
| #[cfg(target_os = "freebsd")] | |
| todo!(); | |
| if true {} | |
| }; | |
| } | |
| fn in_parens() { | |
| if true | |
| && true { | |
| println!("In parens, linted"); | |
| } | |
| //~^^^^^ collapsible_if | |
| } | |
| fn in_brackets() { | |
| if true { | |
| { | |
| if true { | |
| println!("In brackets, not linted"); | |
| } | |
| } | |
| } | |
| } |