Skip to content

Commit adf7c50

Browse files
committed
Auto merge of rust-lang#5476 - ThibsG:FixMatchesInExternalMacros, r=flip1995
Do not lint in macros for match lints Don't lint in macros for match lints, more precisely in `check_pat` and `check_local` where it was not the case. changelog: none fixes: rust-lang#5362
2 parents 2f6b6ab + 7fb94c2 commit adf7c50

9 files changed

+119
-17
lines changed

clippy_lints/src/matches.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
389389

390390
fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local<'_>) {
391391
if_chain! {
392+
if !in_external_macro(cx.sess(), local.span);
393+
if !in_macro(local.span);
392394
if let Some(ref expr) = local.init;
393395
if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.kind;
394396
if arms.len() == 1 && arms[0].guard.is_none();
@@ -423,6 +425,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
423425

424426
fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat<'_>) {
425427
if_chain! {
428+
if !in_external_macro(cx.sess(), pat.span);
429+
if !in_macro(pat.span);
426430
if let PatKind::Struct(ref qpath, fields, true) = pat.kind;
427431
if let QPath::Resolved(_, ref path) = qpath;
428432
if let Some(def_id) = path.res.opt_def_id();

tests/ui/infallible_destructuring_match.fixed

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ struct TupleStruct(i32);
1111

1212
enum EmptyEnum {}
1313

14+
macro_rules! match_enum {
15+
($param:expr) => {
16+
let data = match $param {
17+
SingleVariantEnum::Variant(i) => i,
18+
};
19+
};
20+
}
21+
1422
fn infallible_destructuring_match_enum() {
1523
let wrapper = SingleVariantEnum::Variant(0);
1624

1725
// This should lint!
1826
let SingleVariantEnum::Variant(data) = wrapper;
1927

28+
// This shouldn't (inside macro)
29+
match_enum!(wrapper);
30+
2031
// This shouldn't!
2132
let data = match wrapper {
2233
SingleVariantEnum::Variant(_) => -1,
@@ -30,12 +41,23 @@ fn infallible_destructuring_match_enum() {
3041
let SingleVariantEnum::Variant(data) = wrapper;
3142
}
3243

44+
macro_rules! match_struct {
45+
($param:expr) => {
46+
let data = match $param {
47+
TupleStruct(i) => i,
48+
};
49+
};
50+
}
51+
3352
fn infallible_destructuring_match_struct() {
3453
let wrapper = TupleStruct(0);
3554

3655
// This should lint!
3756
let TupleStruct(data) = wrapper;
3857

58+
// This shouldn't (inside macro)
59+
match_struct!(wrapper);
60+
3961
// This shouldn't!
4062
let data = match wrapper {
4163
TupleStruct(_) => -1,
@@ -49,12 +71,23 @@ fn infallible_destructuring_match_struct() {
4971
let TupleStruct(data) = wrapper;
5072
}
5173

74+
macro_rules! match_never_enum {
75+
($param:expr) => {
76+
let data = match $param {
77+
Ok(i) => i,
78+
};
79+
};
80+
}
81+
5282
fn never_enum() {
5383
let wrapper: Result<i32, !> = Ok(23);
5484

5585
// This should lint!
5686
let Ok(data) = wrapper;
5787

88+
// This shouldn't (inside macro)
89+
match_never_enum!(wrapper);
90+
5891
// This shouldn't!
5992
let data = match wrapper {
6093
Ok(_) => -1,

tests/ui/infallible_destructuring_match.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ struct TupleStruct(i32);
1111

1212
enum EmptyEnum {}
1313

14+
macro_rules! match_enum {
15+
($param:expr) => {
16+
let data = match $param {
17+
SingleVariantEnum::Variant(i) => i,
18+
};
19+
};
20+
}
21+
1422
fn infallible_destructuring_match_enum() {
1523
let wrapper = SingleVariantEnum::Variant(0);
1624

@@ -19,6 +27,9 @@ fn infallible_destructuring_match_enum() {
1927
SingleVariantEnum::Variant(i) => i,
2028
};
2129

30+
// This shouldn't (inside macro)
31+
match_enum!(wrapper);
32+
2233
// This shouldn't!
2334
let data = match wrapper {
2435
SingleVariantEnum::Variant(_) => -1,
@@ -32,6 +43,14 @@ fn infallible_destructuring_match_enum() {
3243
let SingleVariantEnum::Variant(data) = wrapper;
3344
}
3445

46+
macro_rules! match_struct {
47+
($param:expr) => {
48+
let data = match $param {
49+
TupleStruct(i) => i,
50+
};
51+
};
52+
}
53+
3554
fn infallible_destructuring_match_struct() {
3655
let wrapper = TupleStruct(0);
3756

@@ -40,6 +59,9 @@ fn infallible_destructuring_match_struct() {
4059
TupleStruct(i) => i,
4160
};
4261

62+
// This shouldn't (inside macro)
63+
match_struct!(wrapper);
64+
4365
// This shouldn't!
4466
let data = match wrapper {
4567
TupleStruct(_) => -1,
@@ -53,6 +75,14 @@ fn infallible_destructuring_match_struct() {
5375
let TupleStruct(data) = wrapper;
5476
}
5577

78+
macro_rules! match_never_enum {
79+
($param:expr) => {
80+
let data = match $param {
81+
Ok(i) => i,
82+
};
83+
};
84+
}
85+
5686
fn never_enum() {
5787
let wrapper: Result<i32, !> = Ok(23);
5888

@@ -61,6 +91,9 @@ fn never_enum() {
6191
Ok(i) => i,
6292
};
6393

94+
// This shouldn't (inside macro)
95+
match_never_enum!(wrapper);
96+
6497
// This shouldn't!
6598
let data = match wrapper {
6699
Ok(_) => -1,

tests/ui/infallible_destructuring_match.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
2-
--> $DIR/infallible_destructuring_match.rs:18:5
2+
--> $DIR/infallible_destructuring_match.rs:26:5
33
|
44
LL | / let data = match wrapper {
55
LL | | SingleVariantEnum::Variant(i) => i,
@@ -9,15 +9,15 @@ LL | | };
99
= note: `-D clippy::infallible-destructuring-match` implied by `-D warnings`
1010

1111
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
12-
--> $DIR/infallible_destructuring_match.rs:39:5
12+
--> $DIR/infallible_destructuring_match.rs:58:5
1313
|
1414
LL | / let data = match wrapper {
1515
LL | | TupleStruct(i) => i,
1616
LL | | };
1717
| |______^ help: try this: `let TupleStruct(data) = wrapper;`
1818

1919
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
20-
--> $DIR/infallible_destructuring_match.rs:60:5
20+
--> $DIR/infallible_destructuring_match.rs:90:5
2121
|
2222
LL | / let data = match wrapper {
2323
LL | | Ok(i) => i,

tests/ui/match_single_binding.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ fn coords() -> Point {
1212
Point { x: 1, y: 2 }
1313
}
1414

15+
macro_rules! foo {
16+
($param:expr) => {
17+
match $param {
18+
_ => println!("whatever"),
19+
}
20+
};
21+
}
22+
1523
fn main() {
1624
let a = 1;
1725
let b = 2;
@@ -25,6 +33,8 @@ fn main() {
2533
let (x, y, z) = (a, b, c);
2634
println!("{} {} {}", x, y, z);
2735
// Ok
36+
foo!(a);
37+
// Ok
2838
match a {
2939
2 => println!("2"),
3040
_ => println!("Not 2"),

tests/ui/match_single_binding.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ fn coords() -> Point {
1212
Point { x: 1, y: 2 }
1313
}
1414

15+
macro_rules! foo {
16+
($param:expr) => {
17+
match $param {
18+
_ => println!("whatever"),
19+
}
20+
};
21+
}
22+
1523
fn main() {
1624
let a = 1;
1725
let b = 2;
@@ -27,6 +35,8 @@ fn main() {
2735
(x, y, z) => println!("{} {} {}", x, y, z),
2836
}
2937
// Ok
38+
foo!(a);
39+
// Ok
3040
match a {
3141
2 => println!("2"),
3242
_ => println!("Not 2"),

tests/ui/match_single_binding.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this match could be written as a `let` statement
2-
--> $DIR/match_single_binding.rs:20:5
2+
--> $DIR/match_single_binding.rs:28:5
33
|
44
LL | / match (a, b, c) {
55
LL | | (x, y, z) => {
@@ -18,7 +18,7 @@ LL | }
1818
|
1919

2020
error: this match could be written as a `let` statement
21-
--> $DIR/match_single_binding.rs:26:5
21+
--> $DIR/match_single_binding.rs:34:5
2222
|
2323
LL | / match (a, b, c) {
2424
LL | | (x, y, z) => println!("{} {} {}", x, y, z),
@@ -32,15 +32,15 @@ LL | println!("{} {} {}", x, y, z);
3232
|
3333

3434
error: this match could be replaced by its body itself
35-
--> $DIR/match_single_binding.rs:41:5
35+
--> $DIR/match_single_binding.rs:51:5
3636
|
3737
LL | / match a {
3838
LL | | _ => println!("whatever"),
3939
LL | | }
4040
| |_____^ help: consider using the match body instead: `println!("whatever");`
4141

4242
error: this match could be replaced by its body itself
43-
--> $DIR/match_single_binding.rs:45:5
43+
--> $DIR/match_single_binding.rs:55:5
4444
|
4545
LL | / match a {
4646
LL | | _ => {
@@ -59,7 +59,7 @@ LL | }
5959
|
6060

6161
error: this match could be replaced by its body itself
62-
--> $DIR/match_single_binding.rs:52:5
62+
--> $DIR/match_single_binding.rs:62:5
6363
|
6464
LL | / match a {
6565
LL | | _ => {
@@ -81,7 +81,7 @@ LL | }
8181
|
8282

8383
error: this match could be written as a `let` statement
84-
--> $DIR/match_single_binding.rs:62:5
84+
--> $DIR/match_single_binding.rs:72:5
8585
|
8686
LL | / match p {
8787
LL | | Point { x, y } => println!("Coords: ({}, {})", x, y),
@@ -95,7 +95,7 @@ LL | println!("Coords: ({}, {})", x, y);
9595
|
9696

9797
error: this match could be written as a `let` statement
98-
--> $DIR/match_single_binding.rs:66:5
98+
--> $DIR/match_single_binding.rs:76:5
9999
|
100100
LL | / match p {
101101
LL | | Point { x: x1, y: y1 } => println!("Coords: ({}, {})", x1, y1),
@@ -109,7 +109,7 @@ LL | println!("Coords: ({}, {})", x1, y1);
109109
|
110110

111111
error: this match could be written as a `let` statement
112-
--> $DIR/match_single_binding.rs:71:5
112+
--> $DIR/match_single_binding.rs:81:5
113113
|
114114
LL | / match x {
115115
LL | | ref r => println!("Got a reference to {}", r),
@@ -123,7 +123,7 @@ LL | println!("Got a reference to {}", r);
123123
|
124124

125125
error: this match could be written as a `let` statement
126-
--> $DIR/match_single_binding.rs:76:5
126+
--> $DIR/match_single_binding.rs:86:5
127127
|
128128
LL | / match x {
129129
LL | | ref mut mr => println!("Got a mutable reference to {}", mr),
@@ -137,7 +137,7 @@ LL | println!("Got a mutable reference to {}", mr);
137137
|
138138

139139
error: this match could be written as a `let` statement
140-
--> $DIR/match_single_binding.rs:80:5
140+
--> $DIR/match_single_binding.rs:90:5
141141
|
142142
LL | / let product = match coords() {
143143
LL | | Point { x, y } => x * y,
@@ -151,7 +151,7 @@ LL | let product = x * y;
151151
|
152152

153153
error: this match could be written as a `let` statement
154-
--> $DIR/match_single_binding.rs:88:18
154+
--> $DIR/match_single_binding.rs:98:18
155155
|
156156
LL | .map(|i| match i.unwrap() {
157157
| __________________^

tests/ui/rest_pat_in_fully_bound_structs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ struct A {
66
c: &'static str,
77
}
88

9+
macro_rules! foo {
10+
($param:expr) => {
11+
match $param {
12+
A { a: 0, b: 0, c: "", .. } => {},
13+
_ => {},
14+
}
15+
};
16+
}
17+
918
fn main() {
1019
let a_struct = A { a: 5, b: 42, c: "A" };
1120

@@ -27,4 +36,7 @@ fn main() {
2736
A { a: 0, b: 0, .. } => {},
2837
_ => {},
2938
}
39+
40+
// No lint
41+
foo!(a_struct);
3042
}

tests/ui/rest_pat_in_fully_bound_structs.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary use of `..` pattern in struct binding. All fields were already bound
2-
--> $DIR/rest_pat_in_fully_bound_structs.rs:13:9
2+
--> $DIR/rest_pat_in_fully_bound_structs.rs:22:9
33
|
44
LL | A { a: 5, b: 42, c: "", .. } => {}, // Lint
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,15 +8,15 @@ LL | A { a: 5, b: 42, c: "", .. } => {}, // Lint
88
= help: consider removing `..` from this binding
99

1010
error: unnecessary use of `..` pattern in struct binding. All fields were already bound
11-
--> $DIR/rest_pat_in_fully_bound_structs.rs:14:9
11+
--> $DIR/rest_pat_in_fully_bound_structs.rs:23:9
1212
|
1313
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider removing `..` from this binding
1717

1818
error: unnecessary use of `..` pattern in struct binding. All fields were already bound
19-
--> $DIR/rest_pat_in_fully_bound_structs.rs:20:9
19+
--> $DIR/rest_pat_in_fully_bound_structs.rs:29:9
2020
|
2121
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)