about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/shadow.rs
blob: 05009b2ddd4167dcae155a90fbb26da74482e89d (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//@aux-build:proc_macro_derive.rs

#![warn(clippy::shadow_same, clippy::shadow_reuse, clippy::shadow_unrelated)]
#![allow(
    clippy::let_unit_value,
    clippy::needless_if,
    clippy::redundant_guards,
    clippy::redundant_locals
)]

extern crate proc_macro_derive;

#[derive(proc_macro_derive::ShadowDerive)]
pub struct Nothing;

macro_rules! reuse {
    ($v:ident) => {
        let $v = $v + 1;
    };
}

fn shadow_same() {
    let x = 1;
    let x = x;
    //~^ shadow_same
    let mut x = &x;
    //~^ shadow_same
    let x = &mut x;
    //~^ shadow_same
    let x = *x;
    //~^ shadow_same
}

fn shadow_reuse() -> Option<()> {
    let x = ([[0]], ());
    let x = x.0;
    //~^ shadow_reuse
    let x = x[0];
    //~^ shadow_reuse
    let [x] = x;
    //~^ shadow_reuse
    let x = Some(x);
    //~^ shadow_reuse
    let x = foo(x);
    //~^ shadow_reuse
    let x = || x;
    //~^ shadow_reuse
    let x = Some(1).map(|_| x)?;
    //~^ shadow_reuse
    let y = 1;
    let y = match y {
        //~^ shadow_reuse
        1 => 2,
        _ => 3,
    };
    None
}

fn shadow_reuse_macro() {
    let x = 1;
    // this should not warn
    reuse!(x);
}

fn shadow_unrelated() {
    let x = 1;
    let x = 2;
    //~^ shadow_unrelated
}

fn syntax() {
    fn f(x: u32) {
        let x = 1;
        //~^ shadow_unrelated
    }
    let x = 1;
    match Some(1) {
        Some(1) => {},
        Some(x) => {
            //~^ shadow_unrelated
            let x = 1;
            //~^ shadow_unrelated
        },
        _ => {},
    }
    if let Some(x) = Some(1) {}
    //~^ shadow_unrelated
    while let Some(x) = Some(1) {}
    //~^ shadow_unrelated
    let _ = |[x]: [u32; 1]| {
        //~^ shadow_unrelated
        let x = 1;
        //~^ shadow_unrelated
    };
    let y = Some(1);
    if let Some(y) = y {}
    //~^ shadow_reuse
}

fn negative() {
    match Some(1) {
        Some(x) if x == 1 => {},
        Some(x) => {},
        None => {},
    }
    match [None, Some(1)] {
        [Some(x), None] | [None, Some(x)] => {},
        _ => {},
    }
    if let Some(x) = Some(1) {
        let y = 1;
    } else {
        let x = 1;
        let y = 1;
    }
    let x = 1;
    #[allow(clippy::shadow_unrelated)]
    let x = 1;
}

fn foo<T>(_: T) {}

fn question_mark() -> Option<()> {
    let val = 1;
    // `?` expands with a `val` binding
    None?;
    None
}

pub async fn foo1(_a: i32) {}

pub async fn foo2(_a: i32, _b: i64) {
    let _b = _a;
    //~^ shadow_unrelated
}

fn ice_8748() {
    let _ = [0; {
        let x = 1;
        if let Some(x) = Some(1) { x } else { 1 }
        //~^ shadow_unrelated
    }];
}

// https://github.com/rust-lang/rust-clippy/issues/10780
fn shadow_closure() {
    // These are not shadow_unrelated; but they are correctly shadow_reuse
    let x = Some(1);
    #[allow(clippy::shadow_reuse)]
    let y = x.map(|x| x + 1);
    let z = x.map(|x| x + 1);
    //~^ shadow_reuse
    let a: Vec<Option<u8>> = [100u8, 120, 140]
        .iter()
        .map(|i| i.checked_mul(2))
        .map(|i| i.map(|i| i - 10))
        //~^ shadow_reuse
        .collect();
}

struct Issue13795 {
    value: i32,
}

fn issue13795(value: Issue13795) {
    let Issue13795 { value, .. } = value;
    //~^ shadow_same
}

fn issue14377() {
    let a;
    let b;
    (a, b) = (0, 1);

    struct S {
        c: i32,
        d: i32,
    }

    let c;
    let d;
    S { c, d } = S { c: 1, d: 2 };
}

fn main() {}