Skip to content

Commit 5161c4c

Browse files
committed
Auto merge of #10697 - lochetti:fix_9757, r=dswij
Ignore `shadow` warns in code from macro expansions This PR fixes #9757 I am in doubt if just looking for `pat.span.from_expansion()` would be sufficient instead of looking for both `pat.span.desugaring_kind().is_some()` or `pat.span.from_expansion()`. The tests (including the new one) passes if I leave the only `if pat.span.from_expansion()`. Any feedbacks? Also, this is my first PR here, sorry for anything and thanks for the patience! changelog: [`shadow_same`, `shadow_reuse`, `shadow_unrelated`]: avoiding warns in macro-generated code
2 parents 96f8471 + 628605e commit 5161c4c

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

clippy_lints/src/shadow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
108108
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
109109
let PatKind::Binding(_, id, ident, _) = pat.kind else { return };
110110

111-
if pat.span.desugaring_kind().is_some() {
111+
if pat.span.desugaring_kind().is_some() || pat.span.from_expansion() {
112112
return;
113113
}
114114

tests/ui/shadow.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ extern crate proc_macro_derive;
88
#[derive(proc_macro_derive::ShadowDerive)]
99
pub struct Nothing;
1010

11+
macro_rules! reuse {
12+
($v:ident) => {
13+
let $v = $v + 1;
14+
};
15+
}
16+
1117
fn shadow_same() {
1218
let x = 1;
1319
let x = x;
@@ -33,6 +39,12 @@ fn shadow_reuse() -> Option<()> {
3339
None
3440
}
3541

42+
fn shadow_reuse_macro() {
43+
let x = 1;
44+
// this should not warn
45+
reuse!(x);
46+
}
47+
3648
fn shadow_unrelated() {
3749
let x = 1;
3850
let x = 2;

tests/ui/shadow.stderr

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,278 +1,278 @@
11
error: `x` is shadowed by itself in `x`
2-
--> $DIR/shadow.rs:13:9
2+
--> $DIR/shadow.rs:19:9
33
|
44
LL | let x = x;
55
| ^
66
|
77
note: previous binding is here
8-
--> $DIR/shadow.rs:12:9
8+
--> $DIR/shadow.rs:18:9
99
|
1010
LL | let x = 1;
1111
| ^
1212
= note: `-D clippy::shadow-same` implied by `-D warnings`
1313

1414
error: `mut x` is shadowed by itself in `&x`
15-
--> $DIR/shadow.rs:14:13
15+
--> $DIR/shadow.rs:20:13
1616
|
1717
LL | let mut x = &x;
1818
| ^
1919
|
2020
note: previous binding is here
21-
--> $DIR/shadow.rs:13:9
21+
--> $DIR/shadow.rs:19:9
2222
|
2323
LL | let x = x;
2424
| ^
2525

2626
error: `x` is shadowed by itself in `&mut x`
27-
--> $DIR/shadow.rs:15:9
27+
--> $DIR/shadow.rs:21:9
2828
|
2929
LL | let x = &mut x;
3030
| ^
3131
|
3232
note: previous binding is here
33-
--> $DIR/shadow.rs:14:9
33+
--> $DIR/shadow.rs:20:9
3434
|
3535
LL | let mut x = &x;
3636
| ^^^^^
3737

3838
error: `x` is shadowed by itself in `*x`
39-
--> $DIR/shadow.rs:16:9
39+
--> $DIR/shadow.rs:22:9
4040
|
4141
LL | let x = *x;
4242
| ^
4343
|
4444
note: previous binding is here
45-
--> $DIR/shadow.rs:15:9
45+
--> $DIR/shadow.rs:21:9
4646
|
4747
LL | let x = &mut x;
4848
| ^
4949

5050
error: `x` is shadowed
51-
--> $DIR/shadow.rs:21:9
51+
--> $DIR/shadow.rs:27:9
5252
|
5353
LL | let x = x.0;
5454
| ^
5555
|
5656
note: previous binding is here
57-
--> $DIR/shadow.rs:20:9
57+
--> $DIR/shadow.rs:26:9
5858
|
5959
LL | let x = ([[0]], ());
6060
| ^
6161
= note: `-D clippy::shadow-reuse` implied by `-D warnings`
6262

6363
error: `x` is shadowed
64-
--> $DIR/shadow.rs:22:9
64+
--> $DIR/shadow.rs:28:9
6565
|
6666
LL | let x = x[0];
6767
| ^
6868
|
6969
note: previous binding is here
70-
--> $DIR/shadow.rs:21:9
70+
--> $DIR/shadow.rs:27:9
7171
|
7272
LL | let x = x.0;
7373
| ^
7474

7575
error: `x` is shadowed
76-
--> $DIR/shadow.rs:23:10
76+
--> $DIR/shadow.rs:29:10
7777
|
7878
LL | let [x] = x;
7979
| ^
8080
|
8181
note: previous binding is here
82-
--> $DIR/shadow.rs:22:9
82+
--> $DIR/shadow.rs:28:9
8383
|
8484
LL | let x = x[0];
8585
| ^
8686

8787
error: `x` is shadowed
88-
--> $DIR/shadow.rs:24:9
88+
--> $DIR/shadow.rs:30:9
8989
|
9090
LL | let x = Some(x);
9191
| ^
9292
|
9393
note: previous binding is here
94-
--> $DIR/shadow.rs:23:10
94+
--> $DIR/shadow.rs:29:10
9595
|
9696
LL | let [x] = x;
9797
| ^
9898

9999
error: `x` is shadowed
100-
--> $DIR/shadow.rs:25:9
100+
--> $DIR/shadow.rs:31:9
101101
|
102102
LL | let x = foo(x);
103103
| ^
104104
|
105105
note: previous binding is here
106-
--> $DIR/shadow.rs:24:9
106+
--> $DIR/shadow.rs:30:9
107107
|
108108
LL | let x = Some(x);
109109
| ^
110110

111111
error: `x` is shadowed
112-
--> $DIR/shadow.rs:26:9
112+
--> $DIR/shadow.rs:32:9
113113
|
114114
LL | let x = || x;
115115
| ^
116116
|
117117
note: previous binding is here
118-
--> $DIR/shadow.rs:25:9
118+
--> $DIR/shadow.rs:31:9
119119
|
120120
LL | let x = foo(x);
121121
| ^
122122

123123
error: `x` is shadowed
124-
--> $DIR/shadow.rs:27:9
124+
--> $DIR/shadow.rs:33:9
125125
|
126126
LL | let x = Some(1).map(|_| x)?;
127127
| ^
128128
|
129129
note: previous binding is here
130-
--> $DIR/shadow.rs:26:9
130+
--> $DIR/shadow.rs:32:9
131131
|
132132
LL | let x = || x;
133133
| ^
134134

135135
error: `y` is shadowed
136-
--> $DIR/shadow.rs:29:9
136+
--> $DIR/shadow.rs:35:9
137137
|
138138
LL | let y = match y {
139139
| ^
140140
|
141141
note: previous binding is here
142-
--> $DIR/shadow.rs:28:9
142+
--> $DIR/shadow.rs:34:9
143143
|
144144
LL | let y = 1;
145145
| ^
146146

147147
error: `x` shadows a previous, unrelated binding
148-
--> $DIR/shadow.rs:38:9
148+
--> $DIR/shadow.rs:50:9
149149
|
150150
LL | let x = 2;
151151
| ^
152152
|
153153
note: previous binding is here
154-
--> $DIR/shadow.rs:37:9
154+
--> $DIR/shadow.rs:49:9
155155
|
156156
LL | let x = 1;
157157
| ^
158158
= note: `-D clippy::shadow-unrelated` implied by `-D warnings`
159159

160160
error: `x` shadows a previous, unrelated binding
161-
--> $DIR/shadow.rs:43:13
161+
--> $DIR/shadow.rs:55:13
162162
|
163163
LL | let x = 1;
164164
| ^
165165
|
166166
note: previous binding is here
167-
--> $DIR/shadow.rs:42:10
167+
--> $DIR/shadow.rs:54:10
168168
|
169169
LL | fn f(x: u32) {
170170
| ^
171171

172172
error: `x` shadows a previous, unrelated binding
173-
--> $DIR/shadow.rs:48:14
173+
--> $DIR/shadow.rs:60:14
174174
|
175175
LL | Some(x) => {
176176
| ^
177177
|
178178
note: previous binding is here
179-
--> $DIR/shadow.rs:45:9
179+
--> $DIR/shadow.rs:57:9
180180
|
181181
LL | let x = 1;
182182
| ^
183183

184184
error: `x` shadows a previous, unrelated binding
185-
--> $DIR/shadow.rs:49:17
185+
--> $DIR/shadow.rs:61:17
186186
|
187187
LL | let x = 1;
188188
| ^
189189
|
190190
note: previous binding is here
191-
--> $DIR/shadow.rs:48:14
191+
--> $DIR/shadow.rs:60:14
192192
|
193193
LL | Some(x) => {
194194
| ^
195195

196196
error: `x` shadows a previous, unrelated binding
197-
--> $DIR/shadow.rs:53:17
197+
--> $DIR/shadow.rs:65:17
198198
|
199199
LL | if let Some(x) = Some(1) {}
200200
| ^
201201
|
202202
note: previous binding is here
203-
--> $DIR/shadow.rs:45:9
203+
--> $DIR/shadow.rs:57:9
204204
|
205205
LL | let x = 1;
206206
| ^
207207

208208
error: `x` shadows a previous, unrelated binding
209-
--> $DIR/shadow.rs:54:20
209+
--> $DIR/shadow.rs:66:20
210210
|
211211
LL | while let Some(x) = Some(1) {}
212212
| ^
213213
|
214214
note: previous binding is here
215-
--> $DIR/shadow.rs:45:9
215+
--> $DIR/shadow.rs:57:9
216216
|
217217
LL | let x = 1;
218218
| ^
219219

220220
error: `x` shadows a previous, unrelated binding
221-
--> $DIR/shadow.rs:55:15
221+
--> $DIR/shadow.rs:67:15
222222
|
223223
LL | let _ = |[x]: [u32; 1]| {
224224
| ^
225225
|
226226
note: previous binding is here
227-
--> $DIR/shadow.rs:45:9
227+
--> $DIR/shadow.rs:57:9
228228
|
229229
LL | let x = 1;
230230
| ^
231231

232232
error: `x` shadows a previous, unrelated binding
233-
--> $DIR/shadow.rs:56:13
233+
--> $DIR/shadow.rs:68:13
234234
|
235235
LL | let x = 1;
236236
| ^
237237
|
238238
note: previous binding is here
239-
--> $DIR/shadow.rs:55:15
239+
--> $DIR/shadow.rs:67:15
240240
|
241241
LL | let _ = |[x]: [u32; 1]| {
242242
| ^
243243

244244
error: `y` is shadowed
245-
--> $DIR/shadow.rs:59:17
245+
--> $DIR/shadow.rs:71:17
246246
|
247247
LL | if let Some(y) = y {}
248248
| ^
249249
|
250250
note: previous binding is here
251-
--> $DIR/shadow.rs:58:9
251+
--> $DIR/shadow.rs:70:9
252252
|
253253
LL | let y = Some(1);
254254
| ^
255255

256256
error: `_b` shadows a previous, unrelated binding
257-
--> $DIR/shadow.rs:95:9
257+
--> $DIR/shadow.rs:107:9
258258
|
259259
LL | let _b = _a;
260260
| ^^
261261
|
262262
note: previous binding is here
263-
--> $DIR/shadow.rs:94:28
263+
--> $DIR/shadow.rs:106:28
264264
|
265265
LL | pub async fn foo2(_a: i32, _b: i64) {
266266
| ^^
267267

268268
error: `x` shadows a previous, unrelated binding
269-
--> $DIR/shadow.rs:101:21
269+
--> $DIR/shadow.rs:113:21
270270
|
271271
LL | if let Some(x) = Some(1) { x } else { 1 }
272272
| ^
273273
|
274274
note: previous binding is here
275-
--> $DIR/shadow.rs:100:13
275+
--> $DIR/shadow.rs:112:13
276276
|
277277
LL | let x = 1;
278278
| ^

0 commit comments

Comments
 (0)