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