|
| 1 | +#![allow(dead_code)] |
| 2 | +#![warn(clippy::if_same_then_else)] |
| 3 | + |
| 4 | +macro_rules! foo { |
| 5 | + () => { |
| 6 | + println!("I'm a macro, yay"); |
| 7 | + }; |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | +macro_rules! bar { |
| 12 | + () => { |
| 13 | + println!("I'm a macro, yay"); |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +/// This should be caught by the `if_same_then_else` lint and be ignored by the |
| 18 | +/// `shared_code_in_if_blocks` lint |
| 19 | +fn other_lint() { |
| 20 | + let x = 10; |
| 21 | + if x == 0 { |
| 22 | + // I'm the real start of arrays |
| 23 | + println!("Hello World"); |
| 24 | + } else { |
| 25 | + // I'm an awesome number |
| 26 | + // Hello @reviewer |
| 27 | + println!("Hello World"); |
| 28 | + } |
| 29 | + |
| 30 | + let _me = if x == 9 { |
| 31 | + 42 |
| 32 | + } else { |
| 33 | + 42 |
| 34 | + }; |
| 35 | + |
| 36 | + let _duck = if x == 8 { |
| 37 | + 7 |
| 38 | + } else if x == 5 { |
| 39 | + 7 |
| 40 | + } else { |
| 41 | + 7 |
| 42 | + }; |
| 43 | + |
| 44 | + // Only two blocks are the same |
| 45 | + let _ = if x == 9 { |
| 46 | + 9 |
| 47 | + } else if x == 7 { |
| 48 | + 7 |
| 49 | + } else { |
| 50 | + 7 |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +fn everything_is_okay() { |
| 55 | + let x = 10; |
| 56 | + // This is currently a false positive |
| 57 | + if x == 0 { |
| 58 | + foo!(); |
| 59 | + } else { |
| 60 | + bar!(); |
| 61 | + } |
| 62 | + |
| 63 | + // Single branch |
| 64 | + if x == 1 { |
| 65 | + println!("I'm an awesome clippy test") |
| 66 | + } |
| 67 | + |
| 68 | + // Same start in two blocks |
| 69 | + if x == 10 { |
| 70 | + println!("x is a value"); |
| 71 | + println!("x is 10"); |
| 72 | + } else if x == 1 { |
| 73 | + println!("x is 1"); |
| 74 | + } else { |
| 75 | + println!("x is a value"); |
| 76 | + } |
| 77 | + |
| 78 | + // Shared code in the middle |
| 79 | + let _ = if x == 7 { |
| 80 | + let z = 3; |
| 81 | + println!("x is a value"); |
| 82 | + z |
| 83 | + } else if x == 100 { |
| 84 | + let y = 1; |
| 85 | + println!("x is a value"); |
| 86 | + y |
| 87 | + } else { |
| 88 | + let x = 2; |
| 89 | + println!("x is a value"); |
| 90 | + x |
| 91 | + }; |
| 92 | + |
| 93 | + // Different macros and just a return should be simple |
| 94 | + let _z = if x == 8 { |
| 95 | + println!("Branch 1"); |
| 96 | + let z = 10; |
| 97 | + foo!(); |
| 98 | + z |
| 99 | + } else { |
| 100 | + println!("Branch 2"); |
| 101 | + let z = 10; |
| 102 | + bar!(); |
| 103 | + z |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +fn shared_code_at_start() { |
| 109 | + let x = 17; |
| 110 | + |
| 111 | + // First statement is the same |
| 112 | + let _ = if x == 1 { |
| 113 | + // I'm a comment that shouldn't matter for the lint |
| 114 | + println!("Hello World"); |
| 115 | + |
| 116 | + 4 |
| 117 | + } else { |
| 118 | + // I'm another comment, |
| 119 | + // Nice to see you here in the tests |
| 120 | + println!("Hello World"); |
| 121 | + |
| 122 | + 16 |
| 123 | + }; |
| 124 | + |
| 125 | + // First statement is the same |
| 126 | + if x == 19 { |
| 127 | + println!("Hello World"); |
| 128 | + let mut y = 9; |
| 129 | + y += 1; |
| 130 | + let _z = y; |
| 131 | + } else { |
| 132 | + println!("Hello World"); |
| 133 | + }; |
| 134 | + |
| 135 | + if x == 8 { |
| 136 | + println!("Hello World"); |
| 137 | + let _ = true; |
| 138 | + } else if x != 2 { |
| 139 | + println!("Hello World"); |
| 140 | + let _ = false; |
| 141 | + } else { |
| 142 | + println!("Hello World"); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +fn shared_code_at_end() { |
| 147 | + let x = 10; |
| 148 | + |
| 149 | + // Same code at the end |
| 150 | + if x == 8 { |
| 151 | + let _ = true; |
| 152 | + println!("Hello World"); |
| 153 | + } else if x != 2 { |
| 154 | + let _ = false; |
| 155 | + println!("Hello World"); |
| 156 | + } else { |
| 157 | + println!("Hello World"); |
| 158 | + } |
| 159 | + |
| 160 | + let _z = if x == 8 { |
| 161 | + println!("Branch 1"); |
| 162 | + let z = 10; |
| 163 | + bar!(); |
| 164 | + z |
| 165 | + } else { |
| 166 | + println!("Branch 2"); |
| 167 | + let z = 10; |
| 168 | + bar!(); |
| 169 | + z |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +fn main () {} |
| 174 | + |
0 commit comments