Skip to content

Commit 2e40dc8

Browse files
committed
Auto merge of rust-lang#8456 - ebobrow:use_self_pat, r=llogiq
check `use_self` in `pat` fixes rust-lang#6955 changelog: check `use_self` in `pat`
2 parents 27869d6 + 914ae1e commit 2e40dc8

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

clippy_lints/src/use_self.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use rustc_hir::{
99
def::{CtorOf, DefKind, Res},
1010
def_id::LocalDefId,
1111
intravisit::{walk_inf, walk_ty, Visitor},
12-
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Path, QPath, TyKind,
12+
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatKind, Path, QPath,
13+
TyKind,
1314
};
1415
use rustc_lint::{LateContext, LateLintPass};
1516
use rustc_semver::RustcVersion;
@@ -252,6 +253,22 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
252253
}
253254
}
254255

256+
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
257+
if_chain! {
258+
if !pat.span.from_expansion();
259+
if meets_msrv(self.msrv.as_ref(), &msrvs::TYPE_ALIAS_ENUM_VARIANTS);
260+
if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last();
261+
if let PatKind::Path(QPath::Resolved(_, path)) = pat.kind;
262+
if !matches!(path.res, Res::SelfTy { .. } | Res::Def(DefKind::TyParam, _));
263+
if cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id);
264+
if let [first, ..] = path.segments;
265+
if let Some(hir_id) = first.hir_id;
266+
then {
267+
span_lint(cx, cx.tcx.hir().span(hir_id));
268+
}
269+
}
270+
}
271+
255272
extract_msrv_attr!(LateContext);
256273
}
257274

tests/ui/use_self.fixed

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// aux-build:proc_macro_derive.rs
33

44
#![warn(clippy::use_self)]
5-
#![allow(dead_code)]
5+
#![allow(dead_code, unreachable_code)]
66
#![allow(
77
clippy::should_implement_trait,
88
clippy::upper_case_acronyms,
@@ -519,3 +519,26 @@ mod self_is_ty_param {
519519
}
520520
}
521521
}
522+
523+
mod use_self_in_pat {
524+
enum Foo {
525+
Bar,
526+
Baz,
527+
}
528+
529+
impl Foo {
530+
fn do_stuff(self) {
531+
match self {
532+
Self::Bar => unimplemented!(),
533+
Self::Baz => unimplemented!(),
534+
}
535+
match Some(1) {
536+
Some(_) => unimplemented!(),
537+
None => unimplemented!(),
538+
}
539+
if let Self::Bar = self {
540+
unimplemented!()
541+
}
542+
}
543+
}
544+
}

tests/ui/use_self.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// aux-build:proc_macro_derive.rs
33

44
#![warn(clippy::use_self)]
5-
#![allow(dead_code)]
5+
#![allow(dead_code, unreachable_code)]
66
#![allow(
77
clippy::should_implement_trait,
88
clippy::upper_case_acronyms,
@@ -519,3 +519,26 @@ mod self_is_ty_param {
519519
}
520520
}
521521
}
522+
523+
mod use_self_in_pat {
524+
enum Foo {
525+
Bar,
526+
Baz,
527+
}
528+
529+
impl Foo {
530+
fn do_stuff(self) {
531+
match self {
532+
Foo::Bar => unimplemented!(),
533+
Foo::Baz => unimplemented!(),
534+
}
535+
match Some(1) {
536+
Some(_) => unimplemented!(),
537+
None => unimplemented!(),
538+
}
539+
if let Foo::Bar = self {
540+
unimplemented!()
541+
}
542+
}
543+
}
544+
}

tests/ui/use_self.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,23 @@ error: unnecessary structure name repetition
168168
LL | S2::new()
169169
| ^^ help: use the applicable keyword: `Self`
170170

171-
error: aborting due to 28 previous errors
171+
error: unnecessary structure name repetition
172+
--> $DIR/use_self.rs:532:17
173+
|
174+
LL | Foo::Bar => unimplemented!(),
175+
| ^^^ help: use the applicable keyword: `Self`
176+
177+
error: unnecessary structure name repetition
178+
--> $DIR/use_self.rs:533:17
179+
|
180+
LL | Foo::Baz => unimplemented!(),
181+
| ^^^ help: use the applicable keyword: `Self`
182+
183+
error: unnecessary structure name repetition
184+
--> $DIR/use_self.rs:539:20
185+
|
186+
LL | if let Foo::Bar = self {
187+
| ^^^ help: use the applicable keyword: `Self`
188+
189+
error: aborting due to 31 previous errors
172190

0 commit comments

Comments
 (0)