]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/if_same_then_else.rs
Rollup merge of #98944 - pierwill:flatset-docs, r=Dylan-DPC
[rust.git] / src / tools / clippy / tests / ui / if_same_then_else.rs
1 #![warn(clippy::if_same_then_else)]
2 #![allow(
3     clippy::blacklisted_name,
4     clippy::eq_op,
5     clippy::never_loop,
6     clippy::no_effect,
7     clippy::unused_unit,
8     clippy::zero_divided_by_zero,
9     clippy::branches_sharing_code,
10     dead_code,
11     unreachable_code
12 )]
13
14 struct Foo {
15     bar: u8,
16 }
17
18 fn foo() -> bool {
19     unimplemented!()
20 }
21
22 fn if_same_then_else() {
23     if true {
24         Foo { bar: 42 };
25         0..10;
26         ..;
27         0..;
28         ..10;
29         0..=10;
30         foo();
31     } else {
32         //~ ERROR same body as `if` block
33         Foo { bar: 42 };
34         0..10;
35         ..;
36         0..;
37         ..10;
38         0..=10;
39         foo();
40     }
41
42     if true {
43         Foo { bar: 42 };
44     } else {
45         Foo { bar: 43 };
46     }
47
48     if true {
49         ();
50     } else {
51         ()
52     }
53
54     if true {
55         0..10;
56     } else {
57         0..=10;
58     }
59
60     if true {
61         foo();
62         foo();
63     } else {
64         foo();
65     }
66
67     let _ = if true {
68         0.0
69     } else {
70         //~ ERROR same body as `if` block
71         0.0
72     };
73
74     let _ = if true {
75         -0.0
76     } else {
77         //~ ERROR same body as `if` block
78         -0.0
79     };
80
81     let _ = if true { 0.0 } else { -0.0 };
82
83     // Different NaNs
84     let _ = if true { 0.0 / 0.0 } else { f32::NAN };
85
86     if true {
87         foo();
88     }
89
90     let _ = if true {
91         42
92     } else {
93         //~ ERROR same body as `if` block
94         42
95     };
96
97     if true {
98         let bar = if true { 42 } else { 43 };
99
100         while foo() {
101             break;
102         }
103         bar + 1;
104     } else {
105         //~ ERROR same body as `if` block
106         let bar = if true { 42 } else { 43 };
107
108         while foo() {
109             break;
110         }
111         bar + 1;
112     }
113
114     if true {
115         let _ = match 42 {
116             42 => 1,
117             a if a > 0 => 2,
118             10..=15 => 3,
119             _ => 4,
120         };
121     } else if false {
122         foo();
123     } else if foo() {
124         let _ = match 42 {
125             42 => 1,
126             a if a > 0 => 2,
127             10..=15 => 3,
128             _ => 4,
129         };
130     }
131 }
132
133 // Issue #2423. This was causing an ICE.
134 fn func() {
135     if true {
136         f(&[0; 62]);
137         f(&[0; 4]);
138         f(&[0; 3]);
139     } else {
140         f(&[0; 62]);
141         f(&[0; 6]);
142         f(&[0; 6]);
143     }
144 }
145
146 fn f(val: &[u8]) {}
147
148 mod issue_5698 {
149     fn mul_not_always_commutative(x: i32, y: i32) -> i32 {
150         if x == 42 {
151             x * y
152         } else if x == 21 {
153             y * x
154         } else {
155             0
156         }
157     }
158 }
159
160 mod issue_8836 {
161     fn do_not_lint() {
162         if true {
163             todo!()
164         } else {
165             todo!()
166         }
167         if true {
168             todo!();
169         } else {
170             todo!();
171         }
172         if true {
173             unimplemented!()
174         } else {
175             unimplemented!()
176         }
177         if true {
178             unimplemented!();
179         } else {
180             unimplemented!();
181         }
182
183         if true {
184             println!("FOO");
185             todo!();
186         } else {
187             println!("FOO");
188             todo!();
189         }
190
191         if true {
192             println!("FOO");
193             unimplemented!();
194         } else {
195             println!("FOO");
196             unimplemented!();
197         }
198
199         if true {
200             println!("FOO");
201             todo!()
202         } else {
203             println!("FOO");
204             todo!()
205         }
206
207         if true {
208             println!("FOO");
209             unimplemented!()
210         } else {
211             println!("FOO");
212             unimplemented!()
213         }
214     }
215 }
216
217 fn main() {}