From 914ae1e8490c4d6e34dc090cad30303b95d44bb7 Mon Sep 17 00:00:00 2001 From: Elliot Bobrow Date: Mon, 21 Feb 2022 14:38:22 -0800 Subject: [PATCH] check `use_self` in `pat` --- clippy_lints/src/use_self.rs | 19 ++++++++++++++++++- tests/ui/use_self.fixed | 25 ++++++++++++++++++++++++- tests/ui/use_self.rs | 25 ++++++++++++++++++++++++- tests/ui/use_self.stderr | 20 +++++++++++++++++++- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index be20282b3b8..4fa9a8ea2ec 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -9,7 +9,8 @@ def::{CtorOf, DefKind, Res}, def_id::LocalDefId, intravisit::{walk_inf, walk_ty, Visitor}, - Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Path, QPath, TyKind, + Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatKind, Path, QPath, + TyKind, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_semver::RustcVersion; @@ -252,6 +253,22 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { } } + fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { + if_chain! { + if !pat.span.from_expansion(); + if meets_msrv(self.msrv.as_ref(), &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, _)); + 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)); + } + } + } + extract_msrv_attr!(LateContext); } diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index 4e33e343ce0..9d216f56ae6 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -2,7 +2,7 @@ // aux-build:proc_macro_derive.rs #![warn(clippy::use_self)] -#![allow(dead_code)] +#![allow(dead_code, unreachable_code)] #![allow( clippy::should_implement_trait, clippy::upper_case_acronyms, @@ -519,3 +519,26 @@ mod self_is_ty_param { } } } + +mod use_self_in_pat { + enum Foo { + Bar, + Baz, + } + + impl Foo { + fn do_stuff(self) { + match self { + Self::Bar => unimplemented!(), + Self::Baz => unimplemented!(), + } + match Some(1) { + Some(_) => unimplemented!(), + None => unimplemented!(), + } + if let Self::Bar = self { + unimplemented!() + } + } + } +} diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index 7b621ff9bca..5f604fe25e4 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -2,7 +2,7 @@ // aux-build:proc_macro_derive.rs #![warn(clippy::use_self)] -#![allow(dead_code)] +#![allow(dead_code, unreachable_code)] #![allow( clippy::should_implement_trait, clippy::upper_case_acronyms, @@ -519,3 +519,26 @@ fn test() { } } } + +mod use_self_in_pat { + enum Foo { + Bar, + Baz, + } + + impl Foo { + fn do_stuff(self) { + match self { + Foo::Bar => unimplemented!(), + Foo::Baz => unimplemented!(), + } + match Some(1) { + Some(_) => unimplemented!(), + None => unimplemented!(), + } + if let Foo::Bar = self { + unimplemented!() + } + } + } +} diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index ecb78b3f972..34d98618253 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -168,5 +168,23 @@ error: unnecessary structure name repetition LL | S2::new() | ^^ help: use the applicable keyword: `Self` -error: aborting due to 28 previous errors +error: unnecessary structure name repetition + --> $DIR/use_self.rs:532:17 + | +LL | Foo::Bar => unimplemented!(), + | ^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:533:17 + | +LL | Foo::Baz => unimplemented!(), + | ^^^ help: use the applicable keyword: `Self` + +error: unnecessary structure name repetition + --> $DIR/use_self.rs:539:20 + | +LL | if let Foo::Bar = self { + | ^^^ help: use the applicable keyword: `Self` + +error: aborting due to 31 previous errors