]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/if_same_then_else2.rs
Rollup merge of #102072 - scottmcm:ptr-alignment-type, r=thomcc
[rust.git] / src / tools / clippy / tests / ui / if_same_then_else2.rs
1 #![warn(clippy::if_same_then_else)]
2 #![allow(
3     clippy::disallowed_names,
4     clippy::collapsible_else_if,
5     clippy::equatable_if_let,
6     clippy::collapsible_if,
7     clippy::ifs_same_cond,
8     clippy::needless_return,
9     clippy::single_element_loop,
10     clippy::branches_sharing_code
11 )]
12
13 fn if_same_then_else2() -> Result<&'static str, ()> {
14     if true {
15         for _ in &[42] {
16             let foo: &Option<_> = &Some::<u8>(42);
17             if foo.is_some() {
18                 break;
19             } else {
20                 continue;
21             }
22         }
23     } else {
24         //~ ERROR same body as `if` block
25         for _ in &[42] {
26             let bar: &Option<_> = &Some::<u8>(42);
27             if bar.is_some() {
28                 break;
29             } else {
30                 continue;
31             }
32         }
33     }
34
35     if true {
36         if let Some(a) = Some(42) {}
37     } else {
38         //~ ERROR same body as `if` block
39         if let Some(a) = Some(42) {}
40     }
41
42     if true {
43         if let (1, .., 3) = (1, 2, 3) {}
44     } else {
45         //~ ERROR same body as `if` block
46         if let (1, .., 3) = (1, 2, 3) {}
47     }
48
49     if true {
50         if let (1, .., 3) = (1, 2, 3) {}
51     } else {
52         if let (.., 3) = (1, 2, 3) {}
53     }
54
55     if true {
56         if let (1, .., 3) = (1, 2, 3) {}
57     } else {
58         if let (.., 4) = (1, 2, 3) {}
59     }
60
61     if true {
62         if let (1, .., 3) = (1, 2, 3) {}
63     } else {
64         if let (.., 1, 3) = (1, 2, 3) {}
65     }
66
67     if true {
68         if let Some(42) = None {}
69     } else {
70         if let Option::Some(42) = None {}
71     }
72
73     if true {
74         if let Some(42) = None::<u8> {}
75     } else {
76         if let Some(42) = None {}
77     }
78
79     if true {
80         if let Some(42) = None::<u8> {}
81     } else {
82         if let Some(42) = None::<u32> {}
83     }
84
85     if true {
86         if let Some(a) = Some(42) {}
87     } else {
88         if let Some(a) = Some(43) {}
89     }
90
91     // Same NaNs
92     let _ = if true {
93         f32::NAN
94     } else {
95         //~ ERROR same body as `if` block
96         f32::NAN
97     };
98
99     if true {
100         Ok("foo")?;
101     } else {
102         //~ ERROR same body as `if` block
103         Ok("foo")?;
104     }
105
106     if true {
107         let foo = "";
108         return Ok(&foo[0..]);
109     } else if false {
110         let foo = "bar";
111         return Ok(&foo[0..]);
112     } else {
113         let foo = "";
114         return Ok(&foo[0..]);
115     }
116
117     if true {
118         let foo = "";
119         return Ok(&foo[0..]);
120     } else if false {
121         let foo = "bar";
122         return Ok(&foo[0..]);
123     } else if true {
124         let foo = "";
125         return Ok(&foo[0..]);
126     } else {
127         let foo = "";
128         return Ok(&foo[0..]);
129     }
130
131     // False positive `if_same_then_else`: `let (x, y)` vs. `let (y, x)`; see issue #3559.
132     if true {
133         let foo = "";
134         let (x, y) = (1, 2);
135         return Ok(&foo[x..y]);
136     } else {
137         let foo = "";
138         let (y, x) = (1, 2);
139         return Ok(&foo[x..y]);
140     }
141
142     // Issue #7579
143     let _ = if let Some(0) = None { 0 } else { 0 };
144
145     if true {
146         return Err(());
147     } else if let Some(0) = None {
148         return Err(());
149     }
150
151     let _ = if let Some(0) = None {
152         0
153     } else if let Some(1) = None {
154         0
155     } else {
156         0
157     };
158 }
159
160 fn main() {}