diff options
| author | lengyijun <sjtu5140809011@gmail.com> | 2023-10-15 08:40:47 +0800 |
|---|---|---|
| committer | lengyijun <sjtu5140809011@gmail.com> | 2023-10-17 10:41:15 +0800 |
| commit | 536114c8572357db28c349c5ec1e822c7c0e2267 (patch) | |
| tree | 117d41b198adf2f1c593e84c2ac1344cc79f34f3 | |
| parent | 2cf708d04f23a8aed92becd2f9141db62d775077 (diff) | |
| download | rust-536114c8572357db28c349c5ec1e822c7c0e2267.tar.gz rust-536114c8572357db28c349c5ec1e822c7c0e2267.zip | |
[`ignored_unit_patterns`]: check &(), &&(), ...
| -rw-r--r-- | clippy_lints/src/ignored_unit_patterns.rs | 2 | ||||
| -rw-r--r-- | tests/ui/ignored_unit_patterns.fixed | 16 | ||||
| -rw-r--r-- | tests/ui/ignored_unit_patterns.rs | 16 | ||||
| -rw-r--r-- | tests/ui/ignored_unit_patterns.stderr | 14 |
4 files changed, 46 insertions, 2 deletions
diff --git a/clippy_lints/src/ignored_unit_patterns.rs b/clippy_lints/src/ignored_unit_patterns.rs index ef2a66d4a20..3e77bededcd 100644 --- a/clippy_lints/src/ignored_unit_patterns.rs +++ b/clippy_lints/src/ignored_unit_patterns.rs @@ -52,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for IgnoredUnitPatterns { }, _ => {}, } - if matches!(pat.kind, PatKind::Wild) && cx.typeck_results().pat_ty(pat).is_unit() { + if matches!(pat.kind, PatKind::Wild) && cx.typeck_results().pat_ty(pat).peel_refs().is_unit() { span_lint_and_sugg( cx, IGNORED_UNIT_PATTERNS, diff --git a/tests/ui/ignored_unit_patterns.fixed b/tests/ui/ignored_unit_patterns.fixed index 15eaf1f659a..707a0e76e4e 100644 --- a/tests/ui/ignored_unit_patterns.fixed +++ b/tests/ui/ignored_unit_patterns.fixed @@ -38,3 +38,19 @@ pub fn moo(_: ()) { let _: () = foo().unwrap(); let _: () = (); } + +fn test_unit_ref_1() { + let x: (usize, &&&&&()) = (1, &&&&&&()); + match x { + (1, ()) => unimplemented!(), + //~^ ERROR: matching over `()` is more explicit + _ => unimplemented!(), + }; +} + +fn test_unit_ref_2(v: &[(usize, ())]) { + for (x, ()) in v { + //~^ ERROR: matching over `()` is more explicit + let _ = x; + } +} diff --git a/tests/ui/ignored_unit_patterns.rs b/tests/ui/ignored_unit_patterns.rs index 9cac3a440ab..544f2b8f692 100644 --- a/tests/ui/ignored_unit_patterns.rs +++ b/tests/ui/ignored_unit_patterns.rs @@ -38,3 +38,19 @@ pub fn moo(_: ()) { let _: () = foo().unwrap(); let _: () = (); } + +fn test_unit_ref_1() { + let x: (usize, &&&&&()) = (1, &&&&&&()); + match x { + (1, _) => unimplemented!(), + //~^ ERROR: matching over `()` is more explicit + _ => unimplemented!(), + }; +} + +fn test_unit_ref_2(v: &[(usize, ())]) { + for (x, _) in v { + //~^ ERROR: matching over `()` is more explicit + let _ = x; + } +} diff --git a/tests/ui/ignored_unit_patterns.stderr b/tests/ui/ignored_unit_patterns.stderr index cac01a87dba..05c8f281e55 100644 --- a/tests/ui/ignored_unit_patterns.stderr +++ b/tests/ui/ignored_unit_patterns.stderr @@ -43,5 +43,17 @@ error: matching over `()` is more explicit LL | let _ = foo().unwrap(); | ^ help: use `()` instead of `_`: `()` -error: aborting due to 7 previous errors +error: matching over `()` is more explicit + --> $DIR/ignored_unit_patterns.rs:45:13 + | +LL | (1, _) => unimplemented!(), + | ^ help: use `()` instead of `_`: `()` + +error: matching over `()` is more explicit + --> $DIR/ignored_unit_patterns.rs:52:13 + | +LL | for (x, _) in v { + | ^ help: use `()` instead of `_`: `()` + +error: aborting due to 9 previous errors |
