]> git.lizzy.rs Git - rust.git/blob - tests/ui/branches_sharing_code/valid_if_blocks.rs
Rollup merge of #92849 - flip1995:clippyup, r=Manishearth
[rust.git] / tests / ui / branches_sharing_code / valid_if_blocks.rs
1 #![allow(dead_code, clippy::eval_order_dependence)]
2 #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
3
4 // This tests valid if blocks that shouldn't trigger the lint
5
6 // Tests with value references are includes in "shared_code_at_bottom.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     let x = 1;
96     if true {
97         println!("{}", x);
98     } else {
99         let x = 2;
100         println!("{}", x);
101     }
102
103     // Let's test empty blocks
104     if false {
105     } else {
106     }
107 }
108
109 /// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint
110 fn trigger_other_lint() {
111     let x = 0;
112     let y = 1;
113
114     // Same block
115     if x == 0 {
116         let u = 19;
117         println!("How are u today?");
118         let _ = "This is a string";
119     } else {
120         let u = 19;
121         println!("How are u today?");
122         let _ = "This is a string";
123     }
124
125     // Only same expression
126     let _ = if x == 6 { 7 } else { 7 };
127
128     // Same in else if block
129     let _ = if x == 67 {
130         println!("Well I'm the most important block");
131         "I'm a pretty string"
132     } else if x == 68 {
133         println!("I'm a doppelgänger");
134         // Don't listen to my clone below
135
136         if y == 90 { "=^.^=" } else { ":D" }
137     } else {
138         // Don't listen to my clone above
139         println!("I'm a doppelgänger");
140
141         if y == 90 { "=^.^=" } else { ":D" }
142     };
143
144     if x == 0 {
145         println!("I'm single");
146     } else if x == 68 {
147         println!("I'm a doppelgänger");
148         // Don't listen to my clone below
149     } else {
150         // Don't listen to my clone above
151         println!("I'm a doppelgänger");
152     }
153 }
154
155 fn main() {}