Skip to content

Commit 29958f0

Browse files
committed
Auto merge of #11602 - koka831:fix/11601, r=xFrednet
Avoid invoking `ignored_unit_patterns` in macro definition Fixes rust-lang/rust-clippy#11601 The reported problem occured in [a derive macro](https://github.com/mpalmer/ct-structs/actions/runs/6386980382/job/17334587328#step:6:239). This PR avoid linting in macros. changelog: [`ignored_unit_patterns`] No longer lints inside macro definitions
2 parents 81400e2 + e465264 commit 29958f0

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

clippy_lints/src/ignored_unit_patterns.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ declare_lint_pass!(IgnoredUnitPatterns => [IGNORED_UNIT_PATTERNS]);
3737

3838
impl<'tcx> LateLintPass<'tcx> for IgnoredUnitPatterns {
3939
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx hir::Pat<'tcx>) {
40+
if pat.span.from_expansion() {
41+
return;
42+
}
43+
4044
match cx.tcx.hir().get_parent(pat.hir_id) {
4145
Node::Param(param) if matches!(cx.tcx.hir().get_parent(param.hir_id), Node::Item(_)) => {
4246
// Ignore function parameters

tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,19 @@ pub fn shadow_derive(_: TokenStream) -> TokenStream {
153153
.into(),
154154
])
155155
}
156+
157+
#[proc_macro_derive(StructIgnoredUnitPattern)]
158+
pub fn derive_ignored_unit_pattern(_: TokenStream) -> TokenStream {
159+
quote! {
160+
struct A;
161+
impl A {
162+
fn a(&self) -> Result<(), ()> {
163+
unimplemented!()
164+
}
165+
166+
pub fn b(&self) {
167+
let _ = self.a().unwrap();
168+
}
169+
}
170+
}
171+
}

tests/ui/ignored_unit_patterns.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![warn(clippy::ignored_unit_patterns)]
23
#![allow(clippy::let_unit_value, clippy::redundant_pattern_matching, clippy::single_match)]
34

@@ -14,8 +15,22 @@ fn main() {
1415
//~^ ERROR: matching over `()` is more explicit
1516
let _ = foo().map_err(|()| todo!());
1617
//~^ ERROR: matching over `()` is more explicit
18+
19+
println!(
20+
"{:?}",
21+
match foo() {
22+
Ok(()) => {},
23+
//~^ ERROR: matching over `()` is more explicit
24+
Err(()) => {},
25+
//~^ ERROR: matching over `()` is more explicit
26+
}
27+
);
1728
}
1829

30+
// ignored_unit_patterns in derive macro should be ok
31+
#[derive(proc_macro_derive::StructIgnoredUnitPattern)]
32+
pub struct B;
33+
1934
#[allow(unused)]
2035
pub fn moo(_: ()) {
2136
let () = foo().unwrap();

tests/ui/ignored_unit_patterns.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![warn(clippy::ignored_unit_patterns)]
23
#![allow(clippy::let_unit_value, clippy::redundant_pattern_matching, clippy::single_match)]
34

@@ -14,8 +15,22 @@ fn main() {
1415
//~^ ERROR: matching over `()` is more explicit
1516
let _ = foo().map_err(|_| todo!());
1617
//~^ ERROR: matching over `()` is more explicit
18+
19+
println!(
20+
"{:?}",
21+
match foo() {
22+
Ok(_) => {},
23+
//~^ ERROR: matching over `()` is more explicit
24+
Err(_) => {},
25+
//~^ ERROR: matching over `()` is more explicit
26+
}
27+
);
1728
}
1829

30+
// ignored_unit_patterns in derive macro should be ok
31+
#[derive(proc_macro_derive::StructIgnoredUnitPattern)]
32+
pub struct B;
33+
1934
#[allow(unused)]
2035
pub fn moo(_: ()) {
2136
let _ = foo().unwrap();

tests/ui/ignored_unit_patterns.stderr

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: matching over `()` is more explicit
2-
--> $DIR/ignored_unit_patterns.rs:10:12
2+
--> $DIR/ignored_unit_patterns.rs:11:12
33
|
44
LL | Ok(_) => {},
55
| ^ help: use `()` instead of `_`: `()`
@@ -8,28 +8,40 @@ LL | Ok(_) => {},
88
= help: to override `-D warnings` add `#[allow(clippy::ignored_unit_patterns)]`
99

1010
error: matching over `()` is more explicit
11-
--> $DIR/ignored_unit_patterns.rs:11:13
11+
--> $DIR/ignored_unit_patterns.rs:12:13
1212
|
1313
LL | Err(_) => {},
1414
| ^ help: use `()` instead of `_`: `()`
1515

1616
error: matching over `()` is more explicit
17-
--> $DIR/ignored_unit_patterns.rs:13:15
17+
--> $DIR/ignored_unit_patterns.rs:14:15
1818
|
1919
LL | if let Ok(_) = foo() {}
2020
| ^ help: use `()` instead of `_`: `()`
2121

2222
error: matching over `()` is more explicit
23-
--> $DIR/ignored_unit_patterns.rs:15:28
23+
--> $DIR/ignored_unit_patterns.rs:16:28
2424
|
2525
LL | let _ = foo().map_err(|_| todo!());
2626
| ^ help: use `()` instead of `_`: `()`
2727

2828
error: matching over `()` is more explicit
29-
--> $DIR/ignored_unit_patterns.rs:21:9
29+
--> $DIR/ignored_unit_patterns.rs:22:16
30+
|
31+
LL | Ok(_) => {},
32+
| ^ help: use `()` instead of `_`: `()`
33+
34+
error: matching over `()` is more explicit
35+
--> $DIR/ignored_unit_patterns.rs:24:17
36+
|
37+
LL | Err(_) => {},
38+
| ^ help: use `()` instead of `_`: `()`
39+
40+
error: matching over `()` is more explicit
41+
--> $DIR/ignored_unit_patterns.rs:36:9
3042
|
3143
LL | let _ = foo().unwrap();
3244
| ^ help: use `()` instead of `_`: `()`
3345

34-
error: aborting due to 5 previous errors
46+
error: aborting due to 7 previous errors
3547

0 commit comments

Comments
 (0)