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