about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/let_if_seq.rs
blob: 2db206212aa5ddc05d1496a6fb39f2a6c5560d57 (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
#![allow(
    unused_variables,
    unused_assignments,
    clippy::similar_names,
    clippy::disallowed_names,
    clippy::branches_sharing_code,
    clippy::needless_late_init
)]
#![warn(clippy::useless_let_if_seq)]
//@no-rustfix
fn f() -> bool {
    true
}
fn g(x: i32) -> i32 {
    x + 1
}

fn issue985() -> i32 {
    let mut x = 42;
    if f() {
        x = g(x);
    }

    x
}

fn issue985_alt() -> i32 {
    let mut x = 42;
    if f() {
        f();
    } else {
        x = g(x);
    }

    x
}

#[allow(clippy::manual_strip)]
fn issue975() -> String {
    let mut udn = "dummy".to_string();
    if udn.starts_with("uuid:") {
        udn = String::from(&udn[5..]);
    }
    udn
}

fn early_return() -> u8 {
    // FIXME: we could extend the lint to include such cases:
    let foo;

    if f() {
        return 42;
    } else {
        foo = 0;
    }

    foo
}

fn allow_works() -> i32 {
    #[allow(clippy::useless_let_if_seq)]
    let x;
    if true {
        x = 1;
    } else {
        x = 2;
    }
    x
}

fn main() {
    early_return();
    issue975();
    issue985();
    issue985_alt();

    let mut foo = 0;
    //~^ useless_let_if_seq

    if f() {
        foo = 42;
    }

    let mut bar = 0;
    //~^ useless_let_if_seq

    if f() {
        f();
        bar = 42;
    } else {
        f();
    }

    let quz;
    //~^ useless_let_if_seq

    if f() {
        quz = 42;
    } else {
        quz = 0;
    }

    // `toto` is used several times
    let mut toto;
    if f() {
        toto = 42;
    } else {
        for i in &[1, 2] {
            toto = *i;
        }

        toto = 2;
    }

    // found in libcore, the inner if is not a statement but the block's expr
    let mut ch = b'x';
    if f() {
        ch = b'*';
        if f() {
            ch = b'?';
        }
    }

    // baz needs to be mut
    let mut baz = 0;
    //~^ useless_let_if_seq

    if f() {
        baz = 42;
    }

    baz = 1337;

    // issue 3043 - types with interior mutability should not trigger this lint
    use std::cell::Cell;
    let mut val = Cell::new(1);
    if true {
        val = Cell::new(2);
    }
    println!("{}", val.get());
}