summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.rs
blob: a26141be23733b86c444d0bef0435e8330e01ac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![allow(dead_code, clippy::mixed_read_write_in_expression)]
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]

// This tests valid if blocks that shouldn't trigger the lint

// Tests with value references are includes in "shared_code_at_bottom.rs"

fn valid_examples() {
    let x = 2;

    // The edge statements are different
    if x == 9 {
        let y = 1 << 5;

        println!("This is the same: vvv");
        let _z = y;
        println!("The block expression is different");

        println!("Different end 1");
    } else {
        let y = 1 << 7;

        println!("This is the same: vvv");
        let _z = y;
        println!("The block expression is different");

        println!("Different end 2");
    }

    // No else
    if x == 2 {
        println!("Hello world!");
        println!("Hello back, how are you?");

        // This is different vvvv
        println!("Howdy stranger =^.^=");

        println!("Bye Bye World");
    } else if x == 9 {
        println!("Hello world!");
        println!("Hello back, how are you?");

        // This is different vvvv
        println!("Hello reviewer :D");

        println!("Bye Bye World");
    }

    // Overlapping statements only in else if blocks -> Don't lint
    if x == 0 {
        println!("I'm important!")
    } else if x == 17 {
        println!("I share code in else if");

        println!("x is 17");
    } else {
        println!("I share code in else if");

        println!("x is nether x nor 17");
    }

    // Mutability is different
    if x == 13 {
        let mut y = 9;
        println!("Value y is: {}", y);
        y += 16;
        let _z1 = y;
    } else {
        let y = 9;
        println!("Value y is: {}", y);
        let _z2 = y;
    }

    // Same blocks but at start and bottom so no `if_same_then_else` lint
    if x == 418 {
        let y = 9;
        let z = 8;
        let _ = (x, y, z);
        // Don't tell the programmer, my code is also in the else block
    } else if x == 419 {
        println!("+-----------+");
        println!("|           |");
        println!("|  O     O  |");
        println!("|     °     |");
        println!("|  \\_____/  |");
        println!("|           |");
        println!("+-----------+");
    } else {
        let y = 9;
        let z = 8;
        let _ = (x, y, z);
        // I'm so much better than the x == 418 block. Trust me
    }

    let x = 1;
    if true {
        println!("{}", x);
    } else {
        let x = 2;
        println!("{}", x);
    }

    // Let's test empty blocks
    if false {
    } else {
    }
}

/// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint
fn trigger_other_lint() {
    let x = 0;
    let y = 1;

    // Same block
    if x == 0 {
        let u = 19;
        println!("How are u today?");
        let _ = "This is a string";
    } else {
        let u = 19;
        println!("How are u today?");
        let _ = "This is a string";
    }

    // Only same expression
    let _ = if x == 6 { 7 } else { 7 };

    // Same in else if block
    let _ = if x == 67 {
        println!("Well I'm the most important block");
        "I'm a pretty string"
    } else if x == 68 {
        println!("I'm a doppelgänger");
        // Don't listen to my clone below

        if y == 90 { "=^.^=" } else { ":D" }
    } else {
        // Don't listen to my clone above
        println!("I'm a doppelgänger");

        if y == 90 { "=^.^=" } else { ":D" }
    };

    if x == 0 {
        println!("I'm single");
    } else if x == 68 {
        println!("I'm a doppelgänger");
        // Don't listen to my clone below
    } else {
        // Don't listen to my clone above
        println!("I'm a doppelgänger");
    }
}

fn main() {}