Skip to content

Commit 72913a1

Browse files
committed
rest_when_destructuring_struct: check if inside of proc macro
1 parent 58c1420 commit 72913a1

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

clippy_lints/src/rest_when_destructuring_struct.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::rustc_lint::LintContext as _;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::is_from_proc_macro;
34
use itertools::Itertools;
45
use rustc_abi::VariantIdx;
56
use rustc_lint::LateLintPass;
@@ -50,6 +51,10 @@ impl<'tcx> LateLintPass<'tcx> for RestWhenDestructuringStruct {
5051
return;
5152
}
5253

54+
if is_from_proc_macro(cx, pat) {
55+
return;
56+
}
57+
5358
if let rustc_hir::PatKind::Struct(path, fields, true) = pat.kind {
5459
let qty = cx.typeck_results().qpath_res(&path, pat.hir_id);
5560
let ty = cx.typeck_results().pat_ty(pat);

tests/ui/rest_when_destructuring_struct.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::rest_when_destructuring_struct)]
23
#![allow(dead_code)]
34
#![allow(unused_variables)]
45

6+
extern crate proc_macros;
7+
58
struct S {
69
a: u8,
710
b: u8,
@@ -20,6 +23,9 @@ fn main() {
2023
let S { a, b, c: _ } = s;
2124
//~^ rest_when_destructuring_struct
2225

26+
let S { a, b, c, } = s;
27+
//~^ rest_when_destructuring_struct
28+
2329
let e = E::A { a1: 1, a2: 2 };
2430

2531
match e {
@@ -36,4 +42,15 @@ fn main() {
3642
//~^ rest_when_destructuring_struct
3743
E::C {} => (),
3844
}
45+
46+
proc_macros::external! {
47+
let s1 = S { a: 1, b: 2, c: 3 };
48+
let S { a, b, .. } = s1;
49+
}
50+
51+
proc_macros::with_span! {
52+
span
53+
let s2 = S { a: 1, b: 2, c: 3 };
54+
let S { a, b, .. } = s2;
55+
}
3956
}

tests/ui/rest_when_destructuring_struct.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::rest_when_destructuring_struct)]
23
#![allow(dead_code)]
34
#![allow(unused_variables)]
45

6+
extern crate proc_macros;
7+
58
struct S {
69
a: u8,
710
b: u8,
@@ -20,6 +23,9 @@ fn main() {
2023
let S { a, b, .. } = s;
2124
//~^ rest_when_destructuring_struct
2225

26+
let S { a, b, c, .. } = s;
27+
//~^ rest_when_destructuring_struct
28+
2329
let e = E::A { a1: 1, a2: 2 };
2430

2531
match e {
@@ -36,4 +42,15 @@ fn main() {
3642
//~^ rest_when_destructuring_struct
3743
E::C {} => (),
3844
}
45+
46+
proc_macros::external! {
47+
let s1 = S { a: 1, b: 2, c: 3 };
48+
let S { a, b, .. } = s1;
49+
}
50+
51+
proc_macros::with_span! {
52+
span
53+
let s2 = S { a: 1, b: 2, c: 3 };
54+
let S { a, b, .. } = s2;
55+
}
3956
}

tests/ui/rest_when_destructuring_struct.stderr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: struct destructuring with rest (..)
2-
--> tests/ui/rest_when_destructuring_struct.rs:20:9
2+
--> tests/ui/rest_when_destructuring_struct.rs:23:9
33
|
44
LL | let S { a, b, .. } = s;
55
| ^^^^^^^^^^^^^^
@@ -13,7 +13,19 @@ LL + let S { a, b, c: _ } = s;
1313
|
1414

1515
error: struct destructuring with rest (..)
16-
--> tests/ui/rest_when_destructuring_struct.rs:27:9
16+
--> tests/ui/rest_when_destructuring_struct.rs:26:9
17+
|
18+
LL | let S { a, b, c, .. } = s;
19+
| ^^^^^^^^^^^^^^^^^
20+
|
21+
help: consider explicitly ignoring remaining fields with wildcard patterns (x: _)
22+
|
23+
LL - let S { a, b, c, .. } = s;
24+
LL + let S { a, b, c, } = s;
25+
|
26+
27+
error: struct destructuring with rest (..)
28+
--> tests/ui/rest_when_destructuring_struct.rs:33:9
1729
|
1830
LL | E::B { .. } => (),
1931
| ^^^^^^^^^^^
@@ -25,7 +37,7 @@ LL + E::B { b1: _, b2: _ } => (),
2537
|
2638

2739
error: struct destructuring with rest (..)
28-
--> tests/ui/rest_when_destructuring_struct.rs:29:9
40+
--> tests/ui/rest_when_destructuring_struct.rs:35:9
2941
|
3042
LL | E::C { .. } => (),
3143
| ^^^^^^^^^^^
@@ -37,7 +49,7 @@ LL + E::C { } => (),
3749
|
3850

3951
error: struct destructuring with rest (..)
40-
--> tests/ui/rest_when_destructuring_struct.rs:35:9
52+
--> tests/ui/rest_when_destructuring_struct.rs:41:9
4153
|
4254
LL | E::B { b1: _, .. } => (),
4355
| ^^^^^^^^^^^^^^^^^^
@@ -48,5 +60,5 @@ LL - E::B { b1: _, .. } => (),
4860
LL + E::B { b1: _, b2: _ } => (),
4961
|
5062

51-
error: aborting due to 4 previous errors
63+
error: aborting due to 5 previous errors
5264

0 commit comments

Comments
 (0)