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

Commit 2f5c445

Browse files
committed
Ignore wildcards in function arguments and local bindings
1 parent 822c7df commit 2f5c445

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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!()
@@ -15,3 +15,11 @@ fn main() {
1515
let _ = foo().map_err(|()| todo!());
1616
//~^ ERROR: matching over `()` is more explicit
1717
}
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 _: () = ();
25+
}

tests/ui/ignored_unit_patterns.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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!()
@@ -15,3 +15,11 @@ fn main() {
1515
let _ = foo().map_err(|_| todo!());
1616
//~^ ERROR: matching over `()` is more explicit
1717
}
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 _: () = ();
25+
}

tests/ui/ignored_unit_patterns.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ error: matching over `()` is more explicit
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)