Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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