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