]> git.lizzy.rs Git - rust.git/blob - tests/ui/shared_code_in_if_blocks/valid_if_blocks.rs
The shared_code_in_if_blocks lint only operats on entire if expr not else ifs
[rust.git] / tests / ui / shared_code_in_if_blocks / valid_if_blocks.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 // Tests with value references are includes in "shared_code_at_bot.rs"
7
8 fn valid_examples() {
9     let x = 2;
10
11     // The edge statements are different
12     if x == 9 {
13         let y = 1 << 5;
14
15         println!("This is the same: vvv");
16         let _z = y;
17         println!("The block expression is different");
18
19         println!("Different end 1");
20     } else {
21         let y = 1 << 7;
22
23         println!("This is the same: vvv");
24         let _z = y;
25         println!("The block expression is different");
26
27         println!("Different end 2");
28     }
29
30     // No else
31     if x == 2 {
32         println!("Hello world!");
33         println!("Hello back, how are you?");
34
35         // This is different vvvv
36         println!("Howdy stranger =^.^=");
37
38         println!("Bye Bye World");
39     } else if x == 9 {
40         println!("Hello world!");
41         println!("Hello back, how are you?");
42
43         // This is different vvvv
44         println!("Hello reviewer :D");
45
46         println!("Bye Bye World");
47     }
48
49     // Overlapping statements only in else if blocks -> Don't lint
50     if x == 0 {
51         println!("I'm important!")
52     } else if x == 17 {
53         println!("I share code in else if");
54
55         println!("x is 17");
56     } else {
57         println!("I share code in else if");
58
59         println!("x is nether x nor 17");
60     }
61
62     // Mutability is different
63     if x == 13 {
64         let mut y = 9;
65         println!("Value y is: {}", y);
66         y += 16;
67         let _z1 = y;
68     } else {
69         let y = 9;
70         println!("Value y is: {}", y);
71         let _z2 = y;
72     }
73
74     // Same blocks but at start and bottom so no `if_same_then_else` lint
75     if x == 418 {
76         let y = 9;
77         let z = 8;
78         let _ = (x, y, z);
79         // Don't tell the programmer, my code is also in the else block
80     } else if x == 419 {
81         println!("+-----------+");
82         println!("|           |");
83         println!("|  O     O  |");
84         println!("|     °     |");
85         println!("|  \\_____/  |");
86         println!("|           |");
87         println!("+-----------+");
88     } else {
89         let y = 9;
90         let z = 8;
91         let _ = (x, y, z);
92         // I'm so much better than the x == 418 block. Trust me
93     }
94 }
95
96 /// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint
97 fn trigger_other_lint() {
98     let x = 0;
99     let y = 1;
100
101     // Same block
102     if x == 0 {
103         let u = 19;
104         println!("How are u today?");
105         let _ = "This is a string";
106     } else {
107         let u = 19;
108         println!("How are u today?");
109         let _ = "This is a string";
110     }
111
112     // More complex same blocks
113     if x == 17 {
114         #[derive(Debug)]
115         struct Duck {
116             num: u64,
117         };
118         let pet = Duck { num: 18 };
119         println!("{:?}", pet);
120     } else {
121         #[derive(Debug)]
122         struct Duck {
123             num: u64,
124         };
125         let pet = Duck { num: 18 };
126         println!("{:?}", pet);
127     }
128
129     // Only same expression
130     let _ = if x == 6 { 7 } else { 7 };
131
132     // Same in else if block
133     let _ = if x == 67 {
134         println!("Well I'm the most important block");
135         "I'm a pretty string"
136     } else if x == 68 {
137         println!("I'm a doppelgänger");
138         // Don't listen to my clone below
139
140         if y == 90 {
141             "=^.^="
142         } else {
143             ":D"
144         }
145     } else {
146         // Don't listen to my clone above
147         println!("I'm a doppelgänger");
148
149         if y == 90 {
150             "=^.^="
151         } else {
152             ":D"
153         }
154     };
155
156     if x == 0 {
157         println!("I'm single");
158     } else if x == 68 {
159         println!("I'm a doppelgänger");
160         // Don't listen to my clone below
161     } else {
162         // Don't listen to my clone above
163         println!("I'm a doppelgänger");
164     }
165 }
166
167 fn main() {}