1 #![warn(clippy::same_functions_in_if_condition)] 2 #![allow(clippy::ifs_same_cond)] // This warning is different from `ifs_same_cond`. 3 #![allow(clippy::if_same_then_else, clippy::comparison_chain)] // all empty blocks 4 function() -> bool5fn function() -> bool { 6 true 7 } 8 fn_arg(_arg: u8) -> bool9fn fn_arg(_arg: u8) -> bool { 10 true 11 } 12 13 struct Struct; 14 15 impl Struct { method(&self) -> bool16 fn method(&self) -> bool { 17 true 18 } method_arg(&self, _arg: u8) -> bool19 fn method_arg(&self, _arg: u8) -> bool { 20 true 21 } 22 } 23 ifs_same_cond_fn()24fn ifs_same_cond_fn() { 25 let a = 0; 26 let obj = Struct; 27 28 if function() { 29 } else if function() { 30 //~ ERROR ifs same condition 31 } 32 33 if fn_arg(a) { 34 } else if fn_arg(a) { 35 //~ ERROR ifs same condition 36 } 37 38 if obj.method() { 39 } else if obj.method() { 40 //~ ERROR ifs same condition 41 } 42 43 if obj.method_arg(a) { 44 } else if obj.method_arg(a) { 45 //~ ERROR ifs same condition 46 } 47 48 let mut v = vec![1]; 49 if v.pop() == None { 50 //~ ERROR ifs same condition 51 } else if v.pop() == None { 52 } 53 54 if v.len() == 42 { 55 //~ ERROR ifs same condition 56 } else if v.len() == 42 { 57 } 58 59 if v.len() == 1 { 60 // ok, different conditions 61 } else if v.len() == 2 { 62 } 63 64 if fn_arg(0) { 65 // ok, different arguments. 66 } else if fn_arg(1) { 67 } 68 69 if obj.method_arg(0) { 70 // ok, different arguments. 71 } else if obj.method_arg(1) { 72 } 73 74 if a == 1 { 75 // ok, warning is on `ifs_same_cond` behalf. 76 } else if a == 1 { 77 } 78 } 79 main()80fn main() { 81 // macro as condition (see #6168) 82 let os = if cfg!(target_os = "macos") { 83 "macos" 84 } else if cfg!(target_os = "windows") { 85 "windows" 86 } else { 87 "linux" 88 }; 89 println!("{}", os); 90 } 91