]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 | 1 | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] |
2b03887a FG |
2 | #![allow(dead_code)] |
3 | #![allow(clippy::equatable_if_let, clippy::uninlined_format_args)] | |
781aab86 | 4 | //@no-rustfix |
cdc7bbd5 XL |
5 | // This tests the branches_sharing_code lint at the end of blocks |
6 | ||
7 | fn simple_examples() { | |
8 | let x = 1; | |
9 | ||
10 | let _ = if x == 7 { | |
11 | println!("Branch I"); | |
12 | let start_value = 0; | |
13 | println!("=^.^="); | |
14 | ||
15 | // Same but not moveable due to `start_value` | |
16 | let _ = start_value; | |
17 | ||
18 | // The rest is self contained and moveable => Only lint the rest | |
19 | let result = false; | |
20 | println!("Block end!"); | |
21 | result | |
22 | } else { | |
23 | println!("Branch II"); | |
24 | let start_value = 8; | |
25 | println!("xD"); | |
26 | ||
27 | // Same but not moveable due to `start_value` | |
28 | let _ = start_value; | |
29 | ||
30 | // The rest is self contained and moveable => Only lint the rest | |
31 | let result = false; | |
781aab86 FG |
32 | //~^ ERROR: all if blocks contain the same code at the end |
33 | //~| NOTE: the end suggestion probably needs some adjustments to use the expressio | |
cdc7bbd5 XL |
34 | println!("Block end!"); |
35 | result | |
36 | }; | |
37 | ||
38 | // Else if block | |
39 | if x == 9 { | |
40 | println!("The index is: 6"); | |
41 | ||
42 | println!("Same end of block"); | |
43 | } else if x == 8 { | |
44 | println!("The index is: 4"); | |
45 | ||
46 | // We should only get a lint trigger for the last statement | |
47 | println!("This is also eq with the else block"); | |
48 | println!("Same end of block"); | |
49 | } else { | |
50 | println!("This is also eq with the else block"); | |
51 | println!("Same end of block"); | |
781aab86 | 52 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
53 | } |
54 | ||
55 | // Use of outer scope value | |
56 | let outer_scope_value = "I'm outside the if block"; | |
57 | if x < 99 { | |
58 | let z = "How are you"; | |
59 | println!("I'm a local because I use the value `z`: `{}`", z); | |
60 | ||
61 | println!( | |
62 | "I'm moveable because I know: `outer_scope_value`: '{}'", | |
63 | outer_scope_value | |
64 | ); | |
65 | } else { | |
66 | let z = 45678000; | |
67 | println!("I'm a local because I use the value `z`: `{}`", z); | |
68 | ||
69 | println!( | |
781aab86 | 70 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
71 | "I'm moveable because I know: `outer_scope_value`: '{}'", |
72 | outer_scope_value | |
73 | ); | |
74 | } | |
75 | ||
76 | if x == 9 { | |
77 | if x == 8 { | |
78 | // No parent!! | |
79 | println!("---"); | |
80 | println!("Hello World"); | |
81 | } else { | |
82 | println!("Hello World"); | |
781aab86 | 83 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
84 | } |
85 | } | |
86 | } | |
87 | ||
88 | /// Simple examples where the move can cause some problems due to moved values | |
89 | fn simple_but_suggestion_is_invalid() { | |
90 | let x = 16; | |
91 | ||
92 | // Local value | |
93 | let later_used_value = 17; | |
94 | if x == 9 { | |
95 | let _ = 9; | |
96 | let later_used_value = "A string value"; | |
97 | println!("{}", later_used_value); | |
98 | } else { | |
99 | let later_used_value = "A string value"; | |
781aab86 | 100 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
101 | println!("{}", later_used_value); |
102 | // I'm expecting a note about this | |
103 | } | |
104 | println!("{}", later_used_value); | |
105 | ||
106 | // outer function | |
107 | if x == 78 { | |
108 | let simple_examples = "I now identify as a &str :)"; | |
109 | println!("This is the new simple_example: {}", simple_examples); | |
110 | } else { | |
111 | println!("Separator print statement"); | |
112 | ||
113 | let simple_examples = "I now identify as a &str :)"; | |
781aab86 | 114 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
115 | println!("This is the new simple_example: {}", simple_examples); |
116 | } | |
117 | simple_examples(); | |
118 | } | |
119 | ||
120 | /// Tests where the blocks are not linted due to the used value scope | |
121 | fn not_moveable_due_to_value_scope() { | |
122 | let x = 18; | |
123 | ||
124 | // Using a local value in the moved code | |
125 | if x == 9 { | |
126 | let y = 18; | |
127 | println!("y is: `{}`", y); | |
128 | } else { | |
129 | let y = "A string"; | |
130 | println!("y is: `{}`", y); | |
131 | } | |
132 | ||
133 | // Using a local value in the expression | |
134 | let _ = if x == 0 { | |
135 | let mut result = x + 1; | |
136 | ||
137 | println!("1. Doing some calculations"); | |
138 | println!("2. Some more calculations"); | |
139 | println!("3. Setting result"); | |
140 | ||
141 | result | |
142 | } else { | |
143 | let mut result = x - 1; | |
144 | ||
145 | println!("1. Doing some calculations"); | |
146 | println!("2. Some more calculations"); | |
147 | println!("3. Setting result"); | |
148 | ||
149 | result | |
150 | }; | |
151 | ||
152 | let _ = if x == 7 { | |
153 | let z1 = 100; | |
154 | println!("z1: {}", z1); | |
155 | ||
156 | let z2 = z1; | |
157 | println!("z2: {}", z2); | |
158 | ||
159 | z2 | |
160 | } else { | |
161 | let z1 = 300; | |
162 | println!("z1: {}", z1); | |
163 | ||
164 | let z2 = z1; | |
165 | println!("z2: {}", z2); | |
166 | ||
167 | z2 | |
168 | }; | |
169 | } | |
170 | ||
171 | /// This should add a note to the lint msg since the moved expression is not `()` | |
172 | fn added_note_for_expression_use() -> u32 { | |
173 | let x = 9; | |
174 | ||
175 | let _ = if x == 7 { | |
176 | x << 2 | |
177 | } else { | |
178 | let _ = 6; | |
179 | x << 2 | |
781aab86 FG |
180 | //~^ ERROR: all if blocks contain the same code at the end |
181 | //~| NOTE: the end suggestion probably needs some adjustments to use the expressio | |
cdc7bbd5 XL |
182 | }; |
183 | ||
184 | if x == 9 { | |
185 | x * 4 | |
186 | } else { | |
187 | let _ = 17; | |
188 | x * 4 | |
781aab86 FG |
189 | //~^ ERROR: all if blocks contain the same code at the end |
190 | //~| NOTE: the end suggestion probably needs some adjustments to use the expressio | |
cdc7bbd5 XL |
191 | } |
192 | } | |
193 | ||
194 | #[rustfmt::skip] | |
195 | fn test_suggestion_with_weird_formatting() { | |
196 | let x = 9; | |
197 | let mut a = 0; | |
198 | let mut b = 0; | |
199 | ||
200 | // The error message still looks weird tbh but this is the best I can do | |
201 | // for weird formatting | |
202 | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } | |
781aab86 | 203 | //~^ ERROR: all if blocks contain the same code at the end |
cdc7bbd5 XL |
204 | } |
205 | ||
206 | fn fp_test() { | |
207 | let x = 17; | |
208 | ||
209 | if x == 18 { | |
210 | let y = 19; | |
211 | if y < x { | |
212 | println!("Trigger") | |
213 | } | |
214 | } else { | |
215 | let z = 166; | |
216 | if z < x { | |
217 | println!("Trigger") | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | fn fp_if_let_issue7054() { | |
223 | // This shouldn't trigger the lint | |
224 | let string; | |
225 | let _x = if let true = true { | |
226 | "" | |
227 | } else if true { | |
228 | string = "x".to_owned(); | |
229 | &string | |
230 | } else { | |
231 | string = "y".to_owned(); | |
232 | &string | |
233 | }; | |
234 | } | |
235 | ||
236 | fn main() {} |