]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/use_self.fixed
Ignore associated items in trait *implementations* when considering type complexity
[rust.git] / tests / ui / use_self.fixed
index d59750cbfd8d949cc6d043009bd639a5f6730b3e..4e33e343ce0e9c414b42abf6d5e73d6b488392c6 100644 (file)
@@ -1,9 +1,17 @@
 // run-rustfix
-// edition:2018
+// aux-build:proc_macro_derive.rs
 
 #![warn(clippy::use_self)]
 #![allow(dead_code)]
-#![allow(clippy::should_implement_trait, clippy::upper_case_acronyms)]
+#![allow(
+    clippy::should_implement_trait,
+    clippy::upper_case_acronyms,
+    clippy::from_over_into,
+    clippy::self_named_constructors
+)]
+
+#[macro_use]
+extern crate proc_macro_derive;
 
 fn main() {}
 
@@ -15,16 +23,13 @@ mod use_self {
             Self {}
         }
         fn test() -> Self {
-            // FIXME: applicable here
-            Foo::new()
+            Self::new()
         }
     }
 
     impl Default for Foo {
-        // FIXME: applicable here
-        fn default() -> Foo {
-            // FIXME: applicable here
-            Foo::new()
+        fn default() -> Self {
+            Self::new()
         }
     }
 }
@@ -74,14 +79,13 @@ mod lifetimes {
 
 mod issue2894 {
     trait IntoBytes {
-        #[allow(clippy::wrong_self_convention)]
-        fn into_bytes(&self) -> Vec<u8>;
+        fn to_bytes(self) -> Vec<u8>;
     }
 
     // This should not be linted
     impl IntoBytes for u8 {
-        fn into_bytes(&self) -> Vec<u8> {
-            vec![*self]
+        fn to_bytes(self) -> Vec<u8> {
+            vec![self]
         }
     }
 }
@@ -90,11 +94,7 @@ mod existential {
     struct Foo;
 
     impl Foo {
-        // FIXME:
-        // TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
-        // See documentation in rustc_hir::hir::TyKind.
-        // The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
-        fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
+        fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
             foos.iter()
         }
 
@@ -117,8 +117,8 @@ mod tuple_structs {
 mod macros {
     macro_rules! use_self_expand {
         () => {
-            fn new() -> Self {
-                Self {}
+            fn new() -> Foo {
+                Foo {}
             }
         };
     }
@@ -126,8 +126,11 @@ mod macros {
     struct Foo {}
 
     impl Foo {
-        use_self_expand!(); // Should lint in local macros
+        use_self_expand!(); // Should not lint in local macros
     }
+
+    #[derive(StructAUseSelf)] // Should not lint in derives
+    struct A;
 }
 
 mod nesting {
@@ -215,10 +218,8 @@ mod rustfix {
         fn fun_1() {}
 
         fn fun_2() {
-            // FIXME: applicable here
-            nested::A::fun_1();
-            // FIXME: applicable here
-            nested::A::A;
+            Self::fun_1();
+            Self::A;
 
             Self {};
         }
@@ -239,8 +240,7 @@ mod issue3567 {
 
     impl Test for TestStruct {
         fn test() -> TestStruct {
-            // FIXME: applicable here
-            TestStruct::from_something()
+            Self::from_something()
         }
     }
 }
@@ -254,14 +254,12 @@ mod paths_created_by_lowering {
         const A: usize = 0;
         const B: usize = 1;
 
-        // FIXME: applicable here
-        async fn g() -> S {
+        async fn g() -> Self {
             Self {}
         }
 
         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
-            // FIXME: applicable here twice
-            &p[S::A..S::B]
+            &p[Self::A..Self::B]
         }
     }
 
@@ -318,28 +316,25 @@ mod issue4140 {
         fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>;
     }
 
-    impl<F, T> TryFrom<F> for T
-    where
-        T: From<F>,
-    {
-        type From = Self;
-        type To = Self;
+    // FIXME: Suggested fix results in infinite recursion.
+    // impl<F, T> TryFrom<F> for T
+    // where
+    //     T: From<F>,
+    // {
+    //     type From = Self::From;
+    //     type To = Self::To;
 
-        fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
-            Ok(From::from(value))
-        }
-    }
+    //     fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
+    //         Ok(From::from(value))
+    //     }
+    // }
 
     impl From<bool> for i64 {
         type From = bool;
         type To = Self;
 
         fn from(value: bool) -> Self {
-            if value {
-                100
-            } else {
-                0
-            }
+            if value { 100 } else { 0 }
         }
     }
 }
@@ -381,7 +376,6 @@ mod issue4305 {
 
     impl<T: Foo> From<T> for Box<dyn Foo> {
         fn from(t: T) -> Self {
-            // FIXME: applicable here
             Box::new(t)
         }
     }
@@ -461,8 +455,67 @@ mod nested_paths {
 
     impl A<submod::C> {
         fn test() -> Self {
-            // FIXME: applicable here
-            A::new::<submod::B>(submod::B {})
+            Self::new::<submod::B>(submod::B {})
+        }
+    }
+}
+
+mod issue6818 {
+    #[derive(serde::Deserialize)]
+    struct A {
+        a: i32,
+    }
+}
+
+mod issue7206 {
+    struct MyStruct<const C: char>;
+    impl From<MyStruct<'a'>> for MyStruct<'b'> {
+        fn from(_s: MyStruct<'a'>) -> Self {
+            Self
+        }
+    }
+
+    // keep linting non-`Const` generic args
+    struct S<'a> {
+        inner: &'a str,
+    }
+
+    struct S2<T> {
+        inner: T,
+    }
+
+    impl<T> S2<T> {
+        fn new() -> Self {
+            unimplemented!();
+        }
+    }
+
+    impl<'a> S2<S<'a>> {
+        fn new_again() -> Self {
+            Self::new()
+        }
+    }
+}
+
+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
         }
     }
 }