]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_else.rs
Rollup merge of #91133 - terrarier2111:unsafe-diagnostic, r=jackh726
[rust.git] / src / tools / clippy / tests / ui / redundant_else.rs
1 #![warn(clippy::redundant_else)]
2 #![allow(clippy::needless_return, clippy::if_same_then_else, clippy::needless_late_init)]
3
4 fn main() {
5     loop {
6         // break
7         if foo() {
8             println!("Love your neighbor;");
9             break;
10         } else {
11             println!("yet don't pull down your hedge.");
12         }
13         // continue
14         if foo() {
15             println!("He that lies down with Dogs,");
16             continue;
17         } else {
18             println!("shall rise up with fleas.");
19         }
20         // match block
21         if foo() {
22             match foo() {
23                 1 => break,
24                 _ => return,
25             }
26         } else {
27             println!("You may delay, but time will not.");
28         }
29     }
30     // else if
31     if foo() {
32         return;
33     } else if foo() {
34         return;
35     } else {
36         println!("A fat kitchen makes a lean will.");
37     }
38     // let binding outside of block
39     let _ = {
40         if foo() {
41             return;
42         } else {
43             1
44         }
45     };
46     // else if with let binding outside of block
47     let _ = {
48         if foo() {
49             return;
50         } else if foo() {
51             return;
52         } else {
53             2
54         }
55     };
56     // inside if let
57     let _ = if let Some(1) = foo() {
58         let _ = 1;
59         if foo() {
60             return;
61         } else {
62             1
63         }
64     } else {
65         1
66     };
67
68     //
69     // non-lint cases
70     //
71
72     // sanity check
73     if foo() {
74         let _ = 1;
75     } else {
76         println!("Who is wise? He that learns from every one.");
77     }
78     // else if without else
79     if foo() {
80         return;
81     } else if foo() {
82         foo()
83     };
84     // nested if return
85     if foo() {
86         if foo() {
87             return;
88         }
89     } else {
90         foo()
91     };
92     // match with non-breaking branch
93     if foo() {
94         match foo() {
95             1 => foo(),
96             _ => return,
97         }
98     } else {
99         println!("Three may keep a secret, if two of them are dead.");
100     }
101     // let binding
102     let _ = if foo() {
103         return;
104     } else {
105         1
106     };
107     // assign
108     let mut a;
109     a = if foo() {
110         return;
111     } else {
112         1
113     };
114     // assign-op
115     a += if foo() {
116         return;
117     } else {
118         1
119     };
120     // if return else if else
121     if foo() {
122         return;
123     } else if foo() {
124         1
125     } else {
126         2
127     };
128     // if else if return else
129     if foo() {
130         1
131     } else if foo() {
132         return;
133     } else {
134         2
135     };
136     // else if with let binding
137     let _ = if foo() {
138         return;
139     } else if foo() {
140         return;
141     } else {
142         2
143     };
144     // inside function call
145     Box::new(if foo() {
146         return;
147     } else {
148         1
149     });
150 }
151
152 fn foo<T>() -> T {
153     unimplemented!("I'm not Santa Claus")
154 }