From 2aa45693039c2a5c235125e065c68c5e155fbaf1 Mon Sep 17 00:00:00 2001 From: Micha White Date: Thu, 26 May 2022 12:52:43 -0400 Subject: [PATCH] Triggered the lint on tuple struct and struct patterns --- clippy_lints/src/use_self.rs | 18 +++++++--- tests/ui/use_self.fixed | 66 ++++++++++++++++++++++++++++++++++++ tests/ui/use_self.rs | 66 ++++++++++++++++++++++++++++++++++++ tests/ui/use_self.stderr | 62 ++++++++++++++++++++++++++++++++- 4 files changed, 206 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 66f7748e9e0..486ea5e5ccf 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -258,13 +258,21 @@ fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if !pat.span.from_expansion(); if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last(); - if let PatKind::Path(QPath::Resolved(_, path)) = pat.kind; - if !matches!(path.res, Res::SelfTy { .. } | Res::Def(DefKind::TyParam, _)); + // get the path from the pattern + if let PatKind::Path(QPath::Resolved(_, path)) + | PatKind::TupleStruct(QPath::Resolved(_, path), _, _) + | PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind; if cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id); - if let [first, ..] = path.segments; - if let Some(hir_id) = first.hir_id; then { - span_lint(cx, cx.tcx.hir().span(hir_id)); + match path.res { + Res::Def(DefKind::Ctor(ctor_of, _), ..) => match ctor_of { + CtorOf::Variant => lint_path_to_variant(cx, path), + CtorOf::Struct => span_lint(cx, path.span), + }, + Res::Def(DefKind::Variant, ..) => lint_path_to_variant(cx, path), + Res::Def(DefKind::Struct, ..) => span_lint(cx, path.span), + _ => () + } } } } diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index 3e62ffe74fe..4f80aaecc90 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -542,3 +542,69 @@ mod use_self_in_pat { } } } + +mod issue8845 { + pub enum Something { + Num(u8), + TupleNums(u8, u8), + StructNums { one: u8, two: u8 }, + } + + struct Foo(u8); + + struct Bar { + x: u8, + y: usize, + } + + impl Something { + fn get_value(&self) -> u8 { + match self { + Self::Num(n) => *n, + Self::TupleNums(n, _m) => *n, + Self::StructNums { one, two: _ } => *one, + } + } + + fn use_crate(&self) -> u8 { + match self { + Self::Num(n) => *n, + Self::TupleNums(n, _m) => *n, + Self::StructNums { one, two: _ } => *one, + } + } + + fn imported_values(&self) -> u8 { + use Something::*; + match self { + Num(n) => *n, + TupleNums(n, _m) => *n, + StructNums { one, two: _ } => *one, + } + } + } + + impl Foo { + fn get_value(&self) -> u8 { + let Self(x) = self; + *x + } + + fn use_crate(&self) -> u8 { + let Self(x) = self; + *x + } + } + + impl Bar { + fn get_value(&self) -> u8 { + let Self { x, .. } = self; + *x + } + + fn use_crate(&self) -> u8 { + let Self { x, .. } = self; + *x + } + } +} diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index da2faddee12..52da72db53c 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -542,3 +542,69 @@ fn do_stuff(self) { } } } + +mod issue8845 { + pub enum Something { + Num(u8), + TupleNums(u8, u8), + StructNums { one: u8, two: u8 }, + } + + struct Foo(u8); + + struct Bar { + x: u8, + y: usize, + } + + impl Something { + fn get_value(&self) -> u8 { + match self { + Something::Num(n) => *n, + Something::TupleNums(n, _m) => *n, + Something::StructNums { one, two: _ } => *one, + } + } + + fn use_crate(&self) -> u8 { + match self { + crate::issue8845::Something::Num(n) => *n, + crate::issue8845::Something::TupleNums(n, _m) => *n, + crate::issue8845::Something::StructNums { one, two: _ } => *one, + } + } + + fn imported_values(&self) -> u8 { + use Something::*; + match self { + Num(n) => *n, + TupleNums(n, _m) => *n, + StructNums { one, two: _ } => *one, + } + } + } + + impl Foo { + fn get_value(&self) -> u8 { + let Foo(x) = self; + *x + } + + fn use_crate(&self) -> u8 { + let crate::issue8845::Foo(x) = self; + *x + } + } + + impl Bar { + fn get_value(&self) -> u8 { + let Bar { x, .. } = self; + *x + } + + fn use_crate(&self) -> u8 { + let crate::issue8845::Bar { x, .. } = self; + *x + } + } +} diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index 34d98618253..f06bb959b3b 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -186,5 +186,65 @@ error: unnecessary structure name repetition LL | if let Foo::Bar = self { | ^^^ help: use the applicable keyword: `Self` -error: aborting due to 31 previous errors +error: unnecessary structure name repetition + --> $DIR/use_self.rs:563:17 + | +LL | Something::Num(n) => *n, + | ^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:564:17 + | +LL | Something::TupleNums(n, _m) => *n, + | ^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:565:17 + | +LL | Something::StructNums { one, two: _ } => *one, + | ^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:571:17 + | +LL | crate::issue8845::Something::Num(n) => *n, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:572:17 + | +LL | crate::issue8845::Something::TupleNums(n, _m) => *n, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:573:17 + | +LL | crate::issue8845::Something::StructNums { one, two: _ } => *one, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:589:17 + | +LL | let Foo(x) = self; + | ^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:594:17 + | +LL | let crate::issue8845::Foo(x) = self; + | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:601:17 + | +LL | let Bar { x, .. } = self; + | ^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:606:17 + | +LL | let crate::issue8845::Bar { x, .. } = self; + | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` + +error: aborting due to 41 previous errors