Skip to content

Commit

Permalink
Auto merge of rust-lang#8456 - ebobrow:use_self_pat, r=llogiq
Browse files Browse the repository at this point in the history
check `use_self` in `pat`

fixes rust-lang#6955

changelog: check `use_self` in `pat`
  • Loading branch information
bors committed Mar 2, 2022
2 parents 27869d6 + 914ae1e commit 2e40dc8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
19 changes: 18 additions & 1 deletion clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use rustc_hir::{
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;
Expand Down Expand Up @@ -252,6 +253,22 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
}
}

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);
}

Expand Down
25 changes: 24 additions & 1 deletion tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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!()
}
}
}
}
25 changes: 24 additions & 1 deletion tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Foo::Bar => unimplemented!(),
Foo::Baz => unimplemented!(),
}
match Some(1) {
Some(_) => unimplemented!(),
None => unimplemented!(),
}
if let Foo::Bar = self {
unimplemented!()
}
}
}
}
20 changes: 19 additions & 1 deletion tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2e40dc8

Please sign in to comment.