1 // run-rustfix 2 #![allow(clippy::assertions_on_constants, clippy::equatable_if_let)] 3 4 #[rustfmt::skip] 5 #[warn(clippy::collapsible_if)] main()6fn main() { 7 let x = "hello"; 8 let y = "world"; 9 if x == "hello" { 10 if y == "world" { 11 println!("Hello world!"); 12 } 13 } 14 15 if x == "hello" || x == "world" { 16 if y == "world" || y == "hello" { 17 println!("Hello world!"); 18 } 19 } 20 21 if x == "hello" && x == "world" { 22 if y == "world" || y == "hello" { 23 println!("Hello world!"); 24 } 25 } 26 27 if x == "hello" || x == "world" { 28 if y == "world" && y == "hello" { 29 println!("Hello world!"); 30 } 31 } 32 33 if x == "hello" && x == "world" { 34 if y == "world" && y == "hello" { 35 println!("Hello world!"); 36 } 37 } 38 39 if 42 == 1337 { 40 if 'a' != 'A' { 41 println!("world!") 42 } 43 } 44 45 // Works because any if with an else statement cannot be collapsed. 46 if x == "hello" { 47 if y == "world" { 48 println!("Hello world!"); 49 } 50 } else { 51 println!("Not Hello world"); 52 } 53 54 if x == "hello" { 55 if y == "world" { 56 println!("Hello world!"); 57 } else { 58 println!("Hello something else"); 59 } 60 } 61 62 if x == "hello" { 63 print!("Hello "); 64 if y == "world" { 65 println!("world!") 66 } 67 } 68 69 if true { 70 } else { 71 assert!(true); // assert! is just an `if` 72 } 73 74 75 // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798 76 if x == "hello" {// Not collapsible 77 if y == "world" { 78 println!("Hello world!"); 79 } 80 } 81 82 if x == "hello" { // Not collapsible 83 if y == "world" { 84 println!("Hello world!"); 85 } 86 } 87 88 if x == "hello" { 89 // Not collapsible 90 if y == "world" { 91 println!("Hello world!"); 92 } 93 } 94 95 if x == "hello" { 96 if y == "world" { // Collapsible 97 println!("Hello world!"); 98 } 99 } 100 101 if x == "hello" { 102 print!("Hello "); 103 } else { 104 // Not collapsible 105 if y == "world" { 106 println!("world!") 107 } 108 } 109 110 if x == "hello" { 111 print!("Hello "); 112 } else { 113 // Not collapsible 114 if let Some(42) = Some(42) { 115 println!("world!") 116 } 117 } 118 119 if x == "hello" { 120 /* Not collapsible */ 121 if y == "world" { 122 println!("Hello world!"); 123 } 124 } 125 126 if x == "hello" { /* Not collapsible */ 127 if y == "world" { 128 println!("Hello world!"); 129 } 130 } 131 132 // Test behavior wrt. `let_chains`. 133 // None of the cases below should be collapsed. 134 fn truth() -> bool { true } 135 136 // Prefix: 137 if let 0 = 1 { 138 if truth() {} 139 } 140 141 // Suffix: 142 if truth() { 143 if let 0 = 1 {} 144 } 145 146 // Midfix: 147 if truth() { 148 if let 0 = 1 { 149 if truth() {} 150 } 151 } 152 153 // Fix #5962 154 if matches!(true, true) { 155 if matches!(true, true) {} 156 } 157 158 if true { 159 #[cfg(not(teehee))] 160 if true { 161 println!("Hello world!"); 162 } 163 } 164 } 165