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