Do not lint use_self on type parameters

This commit is contained in:
Cameron Steffen 2021-05-27 16:25:37 -05:00
parent d3c20c835f
commit 29b4b4c10d
4 changed files with 50 additions and 4 deletions

View File

@ -386,7 +386,7 @@ fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
if same_type_and_consts(ty, self_ty);
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
then {
!matches!(path.res, def::Res::SelfTy(..))
!matches!(path.res, Res::SelfTy(..) | Res::Def(DefKind::TyParam, _))
} else {
false
}

View File

@ -492,3 +492,26 @@ mod issue7206 {
}
}
}
mod self_is_ty_param {
trait Trait {
type Type;
type Hi;
fn test();
}
impl<I> Trait for I
where
I: Iterator,
I::Item: Trait, // changing this to Self would require <Self as Iterator>
{
type Type = I;
type Hi = I::Item;
fn test() {
let _: I::Item;
let _: I; // this could lint, but is questionable
}
}
}

View File

@ -279,7 +279,7 @@ mod generics {
impl<T> Foo<T> {
// `Self` is applicable here
fn foo(value: T) -> Foo<T> {
Foo { value }
Foo::<T> { value }
}
// `Cannot` use `Self` as a return type as the generic types are different
@ -492,3 +492,26 @@ mod issue7206 {
}
}
}
mod self_is_ty_param {
trait Trait {
type Type;
type Hi;
fn test();
}
impl<I> Trait for I
where
I: Iterator,
I::Item: Trait, // changing this to Self would require <Self as Iterator>
{
type Type = I;
type Hi = I::Item;
fn test() {
let _: I::Item;
let _: I; // this could lint, but is questionable
}
}
}

View File

@ -153,8 +153,8 @@ LL | fn foo(value: T) -> Foo<T> {
error: unnecessary structure name repetition
--> $DIR/use_self.rs:282:13
|
LL | Foo { value }
| ^^^ help: use the applicable keyword: `Self`
LL | Foo::<T> { value }
| ^^^^^^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:454:13