Skip to content

Commit

Permalink
Do not lint use_self on type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed May 28, 2021
1 parent d3c20c8 commit 29b4b4c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
25 changes: 24 additions & 1 deletion tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29b4b4c

Please sign in to comment.