]> git.lizzy.rs Git - rust.git/blob - tests/ui/if_same_then_else.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / 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 )]
10
11 struct Foo {
12     bar: u8,
13 }
14
15 fn foo() -> bool {
16     unimplemented!()
17 }
18
19 fn if_same_then_else() {
20     if true {
21         Foo { bar: 42 };
22         0..10;
23         ..;
24         0..;
25         ..10;
26         0..=10;
27         foo();
28     } else {
29         //~ ERROR same body as `if` block
30         Foo { bar: 42 };
31         0..10;
32         ..;
33         0..;
34         ..10;
35         0..=10;
36         foo();
37     }
38
39     if true {
40         Foo { bar: 42 };
41     } else {
42         Foo { bar: 43 };
43     }
44
45     if true {
46         ();
47     } else {
48         ()
49     }
50
51     if true {
52         0..10;
53     } else {
54         0..=10;
55     }
56
57     if true {
58         foo();
59         foo();
60     } else {
61         foo();
62     }
63
64     let _ = if true {
65         0.0
66     } else {
67         //~ ERROR same body as `if` block
68         0.0
69     };
70
71     let _ = if true {
72         -0.0
73     } else {
74         //~ ERROR same body as `if` block
75         -0.0
76     };
77
78     let _ = if true { 0.0 } else { -0.0 };
79
80     // Different NaNs
81     let _ = if true { 0.0 / 0.0 } else { f32::NAN };
82
83     if true {
84         foo();
85     }
86
87     let _ = if true {
88         42
89     } else {
90         //~ ERROR same body as `if` block
91         42
92     };
93
94     if true {
95         let bar = if true { 42 } else { 43 };
96
97         while foo() {
98             break;
99         }
100         bar + 1;
101     } else {
102         //~ ERROR same body as `if` block
103         let bar = if true { 42 } else { 43 };
104
105         while foo() {
106             break;
107         }
108         bar + 1;
109     }
110
111     if true {
112         let _ = match 42 {
113             42 => 1,
114             a if a > 0 => 2,
115             10..=15 => 3,
116             _ => 4,
117         };
118     } else if false {
119         foo();
120     } else if foo() {
121         let _ = match 42 {
122             42 => 1,
123             a if a > 0 => 2,
124             10..=15 => 3,
125             _ => 4,
126         };
127     }
128 }
129
130 // Issue #2423. This was causing an ICE.
131 fn func() {
132     if true {
133         f(&[0; 62]);
134         f(&[0; 4]);
135         f(&[0; 3]);
136     } else {
137         f(&[0; 62]);
138         f(&[0; 6]);
139         f(&[0; 6]);
140     }
141 }
142
143 fn f(val: &[u8]) {}
144
145 fn main() {}