about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCameron Steffen <cam.steffen94@gmail.com>2021-05-27 16:25:37 -0500
committerCameron Steffen <cam.steffen94@gmail.com>2021-05-27 20:18:07 -0500
commit29b4b4c10d89b2278485ac0e24a393ef58290672 (patch)
treeb571815a161a1ae76f9b775fddfceca9acd215e9
parentd3c20c835f63f1953c3940d9b1f9ce8a943a0bc8 (diff)
downloadrust-29b4b4c10d89b2278485ac0e24a393ef58290672.tar.gz
rust-29b4b4c10d89b2278485ac0e24a393ef58290672.zip
Do not lint use_self on type parameters
-rw-r--r--clippy_lints/src/use_self.rs2
-rw-r--r--tests/ui/use_self.fixed23
-rw-r--r--tests/ui/use_self.rs25
-rw-r--r--tests/ui/use_self.stderr4
4 files changed, 50 insertions, 4 deletions
diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs
index e79983134e4..f71dfd02499 100644
--- a/clippy_lints/src/use_self.rs
+++ b/clippy_lints/src/use_self.rs
@@ -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
         }
diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed
index 631da6fe066..e2c28542efc 100644
--- a/tests/ui/use_self.fixed
+++ b/tests/ui/use_self.fixed
@@ -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
+        }
+    }
+}
diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs
index 7a10d755faa..3cd99b9f5cd 100644
--- a/tests/ui/use_self.rs
+++ b/tests/ui/use_self.rs
@@ -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
+        }
+    }
+}
diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr
index cf6222c9b45..6ac26c9e5a9 100644
--- a/tests/ui/use_self.stderr
+++ b/tests/ui/use_self.stderr
@@ -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