Skip to content

Commit

Permalink
Auto merge of rust-lang#8899 - botahamec:use-self-tuple-struct-varian…
Browse files Browse the repository at this point in the history
…ts, r=Alexendoo

Fix `[use_self]` false negative with on struct and tuple struct patterns

fixes rust-lang#8845

changelog: Triggered the warning for ``[`use_self`]`` on `TupleStruct` and `Struct` patterns, whereas currently it's only triggered for `Path` patterns
  • Loading branch information
bors committed May 30, 2022
2 parents 39231b4 + 2aa4569 commit d9f4978
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 6 deletions.
18 changes: 13 additions & 5 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,21 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
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),
_ => ()
}
}
}
}
Expand Down
66 changes: 66 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
66 changes: 66 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
}
}
}
62 changes: 61 additions & 1 deletion tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d9f4978

Please sign in to comment.