about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/if_same_then_else.rs
blob: 6d2e63e7299a82944ba9428e9260106f6576e414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#![warn(clippy::if_same_then_else)]
#![allow(
    clippy::disallowed_names,
    clippy::eq_op,
    clippy::never_loop,
    clippy::no_effect,
    clippy::unused_unit,
    clippy::zero_divided_by_zero,
    clippy::branches_sharing_code,
    dead_code,
    unreachable_code
)]

struct Foo {
    bar: u8,
}

fn foo() -> bool {
    unimplemented!()
}

fn if_same_then_else() {
    if true {
        Foo { bar: 42 };
        0..10;
        ..;
        0..;
        ..10;
        0..=10;
        foo();
    } else {
        Foo { bar: 42 };
        0..10;
        ..;
        0..;
        ..10;
        0..=10;
        foo();
    }
    //~^^^^^^^^^^^^^^^^^ if_same_then_else

    if true {
        Foo { bar: 42 };
    } else {
        Foo { bar: 43 };
    }

    if true {
        ();
    } else {
        ()
    }

    if true {
        0..10;
    } else {
        0..=10;
    }

    if true {
        foo();
        foo();
    } else {
        foo();
    }

    let _ = if true { 0.0 } else { 0.0 };
    //~^ if_same_then_else

    let _ = if true { -0.0 } else { -0.0 };
    //~^ if_same_then_else

    let _ = if true { 0.0 } else { -0.0 };

    // Different NaNs
    let _ = if true { 0.0 / 0.0 } else { f32::NAN };

    if true {
        foo();
    }

    let _ = if true { 42 } else { 42 };
    //~^ if_same_then_else

    if true {
        let bar = if true { 42 } else { 43 };

        while foo() {
            break;
        }
        bar + 1;
    } else {
        let bar = if true { 42 } else { 43 };

        while foo() {
            break;
        }
        bar + 1;
    }
    //~^^^^^^^^^^^^^^^ if_same_then_else

    if true {
        let _ = match 42 {
            42 => 1,
            a if a > 0 => 2,
            10..=15 => 3,
            _ => 4,
        };
    } else if false {
        foo();
    } else if foo() {
        let _ = match 42 {
            42 => 1,
            a if a > 0 => 2,
            10..=15 => 3,
            _ => 4,
        };
    }
}

// Issue #2423. This was causing an ICE.
fn func() {
    if true {
        f(&[0; 62]);
        f(&[0; 4]);
        f(&[0; 3]);
    } else {
        f(&[0; 62]);
        f(&[0; 6]);
        f(&[0; 6]);
    }
}

fn f(val: &[u8]) {}

mod issue_5698 {
    fn mul_not_always_commutative(x: i32, y: i32) -> i32 {
        if x == 42 {
            x * y
        } else if x == 21 {
            y * x
        } else {
            0
        }
    }
}

mod issue_8836 {
    fn do_not_lint() {
        if true {
            todo!()
        } else {
            todo!()
        }
        if true {
            todo!();
        } else {
            todo!();
        }
        if true {
            unimplemented!()
        } else {
            unimplemented!()
        }
        if true {
            unimplemented!();
        } else {
            unimplemented!();
        }

        if true {
            println!("FOO");
            todo!();
        } else {
            println!("FOO");
            todo!();
        }

        if true {
            println!("FOO");
            unimplemented!();
        } else {
            println!("FOO");
            unimplemented!();
        }

        if true {
            println!("FOO");
            todo!()
        } else {
            println!("FOO");
            todo!()
        }

        if true {
            println!("FOO");
            unimplemented!()
        } else {
            println!("FOO");
            unimplemented!()
        }
    }
}

mod issue_11213 {
    fn reproducer(x: bool) -> bool {
        if x {
            0_u8.is_power_of_two()
        } else {
            0_u16.is_power_of_two()
        }
    }

    // a more obvious reproducer that shows
    // why the code above is problematic:
    fn v2(x: bool) -> bool {
        trait Helper {
            fn is_u8(&self) -> bool;
        }
        impl Helper for u8 {
            fn is_u8(&self) -> bool {
                true
            }
        }
        impl Helper for u16 {
            fn is_u8(&self) -> bool {
                false
            }
        }

        // this is certainly not the same code in both branches
        // it returns a different bool depending on the branch.
        if x { 0_u8.is_u8() } else { 0_u16.is_u8() }
    }

    fn do_lint(x: bool) -> bool {
        // but do lint if the type of the literal is the same
        if x {
            0_u8.is_power_of_two()
        } else {
            0_u8.is_power_of_two()
        }
        //~^^^^^ if_same_then_else
    }
}

fn main() {}