Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eb0df1d

Browse files
committed
Auto merge of rust-lang#11454 - samueltardieu:issue-11403, r=Centri3
Ignore wildcards in function arguments and local bindings Fix rust-lang#11403 changelog: none
2 parents bcf856b + 2f5c445 commit eb0df1d

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

clippy_lints/src/ignored_unit_patterns.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use hir::PatKind;
2+
use hir::{Node, PatKind};
33
use rustc_errors::Applicability;
44
use rustc_hir as hir;
55
use rustc_lint::{LateContext, LateLintPass};
@@ -37,6 +37,17 @@ 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+
match cx.tcx.hir().get_parent(pat.hir_id) {
41+
Node::Param(param) if matches!(cx.tcx.hir().get_parent(param.hir_id), Node::Item(_)) => {
42+
// Ignore function parameters
43+
return;
44+
},
45+
Node::Local(local) if local.ty.is_some() => {
46+
// Ignore let bindings with explicit type
47+
return;
48+
},
49+
_ => {},
50+
}
4051
if matches!(pat.kind, PatKind::Wild) && cx.typeck_results().pat_ty(pat).is_unit() {
4152
span_lint_and_sugg(
4253
cx,

tests/ui/ignored_unit_patterns.fixed

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
#![warn(clippy::ignored_unit_patterns)]
2-
#![allow(clippy::redundant_pattern_matching, clippy::single_match)]
2+
#![allow(clippy::let_unit_value, clippy::redundant_pattern_matching, clippy::single_match)]
33

44
fn foo() -> Result<(), ()> {
55
unimplemented!()
66
}
77

88
fn main() {
99
match foo() {
10-
Ok(()) => {},
11-
Err(()) => {},
10+
Ok(()) => {}, //~ ERROR: matching over `()` is more explicit
11+
Err(()) => {}, //~ ERROR: matching over `()` is more explicit
1212
}
1313
if let Ok(()) = foo() {}
14+
//~^ ERROR: matching over `()` is more explicit
1415
let _ = foo().map_err(|()| todo!());
16+
//~^ ERROR: matching over `()` is more explicit
17+
}
18+
19+
#[allow(unused)]
20+
pub fn moo(_: ()) {
21+
let () = foo().unwrap();
22+
//~^ ERROR: matching over `()` is more explicit
23+
let _: () = foo().unwrap();
24+
let _: () = ();
1525
}

tests/ui/ignored_unit_patterns.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
#![warn(clippy::ignored_unit_patterns)]
2-
#![allow(clippy::redundant_pattern_matching, clippy::single_match)]
2+
#![allow(clippy::let_unit_value, clippy::redundant_pattern_matching, clippy::single_match)]
33

44
fn foo() -> Result<(), ()> {
55
unimplemented!()
66
}
77

88
fn main() {
99
match foo() {
10-
Ok(_) => {},
11-
Err(_) => {},
10+
Ok(_) => {}, //~ ERROR: matching over `()` is more explicit
11+
Err(_) => {}, //~ ERROR: matching over `()` is more explicit
1212
}
1313
if let Ok(_) = foo() {}
14+
//~^ ERROR: matching over `()` is more explicit
1415
let _ = foo().map_err(|_| todo!());
16+
//~^ ERROR: matching over `()` is more explicit
17+
}
18+
19+
#[allow(unused)]
20+
pub fn moo(_: ()) {
21+
let _ = foo().unwrap();
22+
//~^ ERROR: matching over `()` is more explicit
23+
let _: () = foo().unwrap();
24+
let _: () = ();
1525
}

tests/ui/ignored_unit_patterns.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ LL | if let Ok(_) = foo() {}
1919
| ^ help: use `()` instead of `_`: `()`
2020

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

27-
error: aborting due to 4 previous errors
27+
error: matching over `()` is more explicit
28+
--> $DIR/ignored_unit_patterns.rs:21:9
29+
|
30+
LL | let _ = foo().unwrap();
31+
| ^ help: use `()` instead of `_`: `()`
32+
33+
error: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)