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