Skip to content

Commit d9f4978

Browse files
committed
Auto merge of #8899 - botahamec:use-self-tuple-struct-variants, r=Alexendoo
Fix `[use_self]` false negative with on struct and tuple struct patterns fixes #8845 changelog: Triggered the warning for ``[`use_self`]`` on `TupleStruct` and `Struct` patterns, whereas currently it's only triggered for `Path` patterns
2 parents 39231b4 + 2aa4569 commit d9f4978

File tree

4 files changed

+206
-6
lines changed

4 files changed

+206
-6
lines changed

clippy_lints/src/use_self.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
258258
if !pat.span.from_expansion();
259259
if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS);
260260
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, _));
261+
// get the path from the pattern
262+
if let PatKind::Path(QPath::Resolved(_, path))
263+
| PatKind::TupleStruct(QPath::Resolved(_, path), _, _)
264+
| PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind;
263265
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;
266266
then {
267-
span_lint(cx, cx.tcx.hir().span(hir_id));
267+
match path.res {
268+
Res::Def(DefKind::Ctor(ctor_of, _), ..) => match ctor_of {
269+
CtorOf::Variant => lint_path_to_variant(cx, path),
270+
CtorOf::Struct => span_lint(cx, path.span),
271+
},
272+
Res::Def(DefKind::Variant, ..) => lint_path_to_variant(cx, path),
273+
Res::Def(DefKind::Struct, ..) => span_lint(cx, path.span),
274+
_ => ()
275+
}
268276
}
269277
}
270278
}

tests/ui/use_self.fixed

+66
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,69 @@ mod use_self_in_pat {
542542
}
543543
}
544544
}
545+
546+
mod issue8845 {
547+
pub enum Something {
548+
Num(u8),
549+
TupleNums(u8, u8),
550+
StructNums { one: u8, two: u8 },
551+
}
552+
553+
struct Foo(u8);
554+
555+
struct Bar {
556+
x: u8,
557+
y: usize,
558+
}
559+
560+
impl Something {
561+
fn get_value(&self) -> u8 {
562+
match self {
563+
Self::Num(n) => *n,
564+
Self::TupleNums(n, _m) => *n,
565+
Self::StructNums { one, two: _ } => *one,
566+
}
567+
}
568+
569+
fn use_crate(&self) -> u8 {
570+
match self {
571+
Self::Num(n) => *n,
572+
Self::TupleNums(n, _m) => *n,
573+
Self::StructNums { one, two: _ } => *one,
574+
}
575+
}
576+
577+
fn imported_values(&self) -> u8 {
578+
use Something::*;
579+
match self {
580+
Num(n) => *n,
581+
TupleNums(n, _m) => *n,
582+
StructNums { one, two: _ } => *one,
583+
}
584+
}
585+
}
586+
587+
impl Foo {
588+
fn get_value(&self) -> u8 {
589+
let Self(x) = self;
590+
*x
591+
}
592+
593+
fn use_crate(&self) -> u8 {
594+
let Self(x) = self;
595+
*x
596+
}
597+
}
598+
599+
impl Bar {
600+
fn get_value(&self) -> u8 {
601+
let Self { x, .. } = self;
602+
*x
603+
}
604+
605+
fn use_crate(&self) -> u8 {
606+
let Self { x, .. } = self;
607+
*x
608+
}
609+
}
610+
}

tests/ui/use_self.rs

+66
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,69 @@ mod use_self_in_pat {
542542
}
543543
}
544544
}
545+
546+
mod issue8845 {
547+
pub enum Something {
548+
Num(u8),
549+
TupleNums(u8, u8),
550+
StructNums { one: u8, two: u8 },
551+
}
552+
553+
struct Foo(u8);
554+
555+
struct Bar {
556+
x: u8,
557+
y: usize,
558+
}
559+
560+
impl Something {
561+
fn get_value(&self) -> u8 {
562+
match self {
563+
Something::Num(n) => *n,
564+
Something::TupleNums(n, _m) => *n,
565+
Something::StructNums { one, two: _ } => *one,
566+
}
567+
}
568+
569+
fn use_crate(&self) -> u8 {
570+
match self {
571+
crate::issue8845::Something::Num(n) => *n,
572+
crate::issue8845::Something::TupleNums(n, _m) => *n,
573+
crate::issue8845::Something::StructNums { one, two: _ } => *one,
574+
}
575+
}
576+
577+
fn imported_values(&self) -> u8 {
578+
use Something::*;
579+
match self {
580+
Num(n) => *n,
581+
TupleNums(n, _m) => *n,
582+
StructNums { one, two: _ } => *one,
583+
}
584+
}
585+
}
586+
587+
impl Foo {
588+
fn get_value(&self) -> u8 {
589+
let Foo(x) = self;
590+
*x
591+
}
592+
593+
fn use_crate(&self) -> u8 {
594+
let crate::issue8845::Foo(x) = self;
595+
*x
596+
}
597+
}
598+
599+
impl Bar {
600+
fn get_value(&self) -> u8 {
601+
let Bar { x, .. } = self;
602+
*x
603+
}
604+
605+
fn use_crate(&self) -> u8 {
606+
let crate::issue8845::Bar { x, .. } = self;
607+
*x
608+
}
609+
}
610+
}

tests/ui/use_self.stderr

+61-1
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,65 @@ error: unnecessary structure name repetition
186186
LL | if let Foo::Bar = self {
187187
| ^^^ help: use the applicable keyword: `Self`
188188

189-
error: aborting due to 31 previous errors
189+
error: unnecessary structure name repetition
190+
--> $DIR/use_self.rs:563:17
191+
|
192+
LL | Something::Num(n) => *n,
193+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
194+
195+
error: unnecessary structure name repetition
196+
--> $DIR/use_self.rs:564:17
197+
|
198+
LL | Something::TupleNums(n, _m) => *n,
199+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
200+
201+
error: unnecessary structure name repetition
202+
--> $DIR/use_self.rs:565:17
203+
|
204+
LL | Something::StructNums { one, two: _ } => *one,
205+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
206+
207+
error: unnecessary structure name repetition
208+
--> $DIR/use_self.rs:571:17
209+
|
210+
LL | crate::issue8845::Something::Num(n) => *n,
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
212+
213+
error: unnecessary structure name repetition
214+
--> $DIR/use_self.rs:572:17
215+
|
216+
LL | crate::issue8845::Something::TupleNums(n, _m) => *n,
217+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
218+
219+
error: unnecessary structure name repetition
220+
--> $DIR/use_self.rs:573:17
221+
|
222+
LL | crate::issue8845::Something::StructNums { one, two: _ } => *one,
223+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
224+
225+
error: unnecessary structure name repetition
226+
--> $DIR/use_self.rs:589:17
227+
|
228+
LL | let Foo(x) = self;
229+
| ^^^ help: use the applicable keyword: `Self`
230+
231+
error: unnecessary structure name repetition
232+
--> $DIR/use_self.rs:594:17
233+
|
234+
LL | let crate::issue8845::Foo(x) = self;
235+
| ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
236+
237+
error: unnecessary structure name repetition
238+
--> $DIR/use_self.rs:601:17
239+
|
240+
LL | let Bar { x, .. } = self;
241+
| ^^^ help: use the applicable keyword: `Self`
242+
243+
error: unnecessary structure name repetition
244+
--> $DIR/use_self.rs:606:17
245+
|
246+
LL | let crate::issue8845::Bar { x, .. } = self;
247+
| ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
248+
249+
error: aborting due to 41 previous errors
190250

0 commit comments

Comments
 (0)