Triggered the lint on tuple struct and struct patterns

This commit is contained in:
Micha White 2022-05-26 12:52:43 -04:00
parent 6553b98540
commit 2aa4569303
No known key found for this signature in database
GPG Key ID: AED94BFA1C301389
4 changed files with 206 additions and 6 deletions

View File

@ -258,13 +258,21 @@ fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
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),
_ => ()
}
}
}
}

View File

@ -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
}
}
}

View File

@ -542,3 +542,69 @@ fn do_stuff(self) {
}
}
}
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
}
}
}

View File

@ -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