]> git.lizzy.rs Git - rust.git/blob - tests/ui/shared_code_in_if_blocks/shared_at_top.rs
A new lint for shared code in if blocks
[rust.git] / tests / ui / shared_code_in_if_blocks / shared_at_top.rs
1 #![allow(dead_code, clippy::eval_order_dependence)]
2 #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
3
4 // This tests the shared_code_in_if_blocks lint at the start of blocks
5
6 fn simple_examples() {
7     let x = 0;
8
9     // Simple
10     if true {
11         println!("Hello World!");
12         println!("I'm branch nr: 1");
13     } else {
14         println!("Hello World!");
15         println!("I'm branch nr: 2");
16     }
17
18     // Else if
19     if x == 0 {
20         let y = 9;
21         println!("The value y was set to: `{}`", y);
22         let _z = y;
23
24         println!("I'm the true start index of arrays");
25     } else if x == 1 {
26         let y = 9;
27         println!("The value y was set to: `{}`", y);
28         let _z = y;
29
30         println!("I start counting from 1 so my array starts from `1`");
31     } else {
32         let y = 9;
33         println!("The value y was set to: `{}`", y);
34         let _z = y;
35
36         println!("Ha, Pascal allows you to start the array where you want")
37     }
38
39     // Return a value
40     let _ = if x == 7 {
41         let y = 16;
42         println!("What can I say except: \"you're welcome?\"");
43         let _ = y;
44         x
45     } else {
46         let y = 16;
47         println!("Thank you");
48         y
49     };
50 }
51
52 /// Simple examples where the move can cause some problems due to moved values
53 fn simple_but_suggestion_is_invalid() {
54     let x = 10;
55
56     // Can't be automatically moved because used_value_name is getting used again
57     let used_value_name = 19;
58     if x == 10 {
59         let used_value_name = "Different type";
60         println!("Str: {}", used_value_name);
61         let _ = 1;
62     } else {
63         let used_value_name = "Different type";
64         println!("Str: {}", used_value_name);
65         let _ = 2;
66     }
67     let _ = used_value_name;
68
69     // This can be automatically moved as `can_be_overridden` is not used again
70     let can_be_overridden = 8;
71     let _ = can_be_overridden;
72     if x == 11 {
73         let can_be_overridden = "Move me";
74         println!("I'm also moveable");
75         let _ = 111;
76     } else {
77         let can_be_overridden = "Move me";
78         println!("I'm also moveable");
79         let _ = 222;
80     }
81 }
82
83 fn main() {}