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