Skip to content

Commit a3d9818

Browse files
committed
Do not lint use_self on type parameters
1 parent d3c20c8 commit a3d9818

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

clippy_lints/src/use_self.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
386386
if same_type_and_consts(ty, self_ty);
387387
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
388388
then {
389-
!matches!(path.res, def::Res::SelfTy(..))
389+
!matches!(path.res, Res::SelfTy(..) | Res::Def(DefKind::TyParam, _))
390390
} else {
391391
false
392392
}

tests/ui/use_self.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,26 @@ mod issue7206 {
492492
}
493493
}
494494
}
495+
496+
mod self_is_ty_param {
497+
trait Trait {
498+
type Type;
499+
type Hi;
500+
501+
fn test();
502+
}
503+
504+
impl<I> Trait for I
505+
where
506+
I: Iterator,
507+
I::Item: Trait, // changing this to Self would require <Self as Iterator>
508+
{
509+
type Type = I;
510+
type Hi = I::Item;
511+
512+
fn test() {
513+
let _: I::Item;
514+
let _: I; // this could lint, but is questionable
515+
}
516+
}
517+
}

tests/ui/use_self.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ mod generics {
279279
impl<T> Foo<T> {
280280
// `Self` is applicable here
281281
fn foo(value: T) -> Foo<T> {
282-
Foo { value }
282+
Foo::<T> { value }
283283
}
284284

285285
// `Cannot` use `Self` as a return type as the generic types are different
@@ -492,3 +492,26 @@ mod issue7206 {
492492
}
493493
}
494494
}
495+
496+
mod self_is_ty_param {
497+
trait Trait {
498+
type Type;
499+
type Hi;
500+
501+
fn test();
502+
}
503+
504+
impl<I> Trait for I
505+
where
506+
I: Iterator,
507+
I::Item: Trait, // changing this to Self would require <Self as Iterator>
508+
{
509+
type Type = I;
510+
type Hi = I::Item;
511+
512+
fn test() {
513+
let _: I::Item;
514+
let _: I; // this could lint, but is questionable
515+
}
516+
}
517+
}

tests/ui/use_self.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ LL | fn foo(value: T) -> Foo<T> {
153153
error: unnecessary structure name repetition
154154
--> $DIR/use_self.rs:282:13
155155
|
156-
LL | Foo { value }
157-
| ^^^ help: use the applicable keyword: `Self`
156+
LL | Foo::<T> { value }
157+
| ^^^^^^^^ help: use the applicable keyword: `Self`
158158

159159
error: unnecessary structure name repetition
160160
--> $DIR/use_self.rs:454:13

0 commit comments

Comments
 (0)