]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/use_self.rs
iterate List by value
[rust.git] / tests / ui / use_self.rs
index c21df403035f78481abec9fb9ea1b9fac4c27fbc..8a182192ab34d67a070ee4990c801e87cbf9d669 100644 (file)
@@ -1,11 +1,5 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+// run-rustfix
+// edition:2018
 
 #![warn(clippy::use_self)]
 #![allow(dead_code)]
@@ -51,8 +45,6 @@ fn default() -> Self {
     }
 }
 
-//todo the lint does not handle lifetimed struct
-//the following module should trigger the lint on the third method only
 mod lifetimes {
     struct Foo<'a> {
         foo_str: &'a str,
@@ -69,175 +61,193 @@ fn bar() -> Foo<'static> {
             Foo { foo_str: "foo" }
         }
 
-        // `Self` is applicable here
+        // FIXME: the lint does not handle lifetimed struct
+        // `Self` should be applicable here
         fn clone(&self) -> Foo<'a> {
             Foo { foo_str: self.foo_str }
         }
     }
 }
 
-#[allow(clippy::boxed_local)]
-mod traits {
-
-    use std::ops::Mul;
-
-    trait SelfTrait {
-        fn refs(p1: &Self) -> &Self;
-        fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
-        fn mut_refs(p1: &mut Self) -> &mut Self;
-        fn nested(p1: Box<Self>, p2: (&u8, &Self));
-        fn vals(r: Self) -> Self;
+mod issue2894 {
+    trait IntoBytes {
+        fn into_bytes(&self) -> Vec<u8>;
     }
 
-    #[derive(Default)]
-    struct Bad;
-
-    impl SelfTrait for Bad {
-        fn refs(p1: &Bad) -> &Bad {
-            p1
+    // This should not be linted
+    impl IntoBytes for u8 {
+        fn into_bytes(&self) -> Vec<u8> {
+            vec![*self]
         }
+    }
+}
 
-        fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
-            p1
-        }
+mod existential {
+    struct Foo;
 
-        fn mut_refs(p1: &mut Bad) -> &mut Bad {
-            p1
+    impl Foo {
+        fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
+            foos.iter()
         }
 
-        fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {}
-
-        fn vals(_: Bad) -> Bad {
-            Bad::default()
+        fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
+            foos.iter()
         }
     }
+}
 
-    impl Mul for Bad {
-        type Output = Bad;
+mod tuple_structs {
+    pub struct TS(i32);
 
-        fn mul(self, rhs: Bad) -> Bad {
-            rhs
+    impl TS {
+        pub fn ts() -> Self {
+            TS(0)
         }
     }
+}
 
-    #[derive(Default)]
-    struct Good;
+mod macros {
+    macro_rules! use_self_expand {
+        () => {
+            fn new() -> Foo {
+                Foo {}
+            }
+        };
+    }
 
-    impl SelfTrait for Good {
-        fn refs(p1: &Self) -> &Self {
-            p1
-        }
+    struct Foo {}
 
-        fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
-            p1
-        }
+    impl Foo {
+        use_self_expand!(); // Should lint in local macros
+    }
+}
 
-        fn mut_refs(p1: &mut Self) -> &mut Self {
-            p1
+mod nesting {
+    struct Foo {}
+    impl Foo {
+        fn foo() {
+            #[allow(unused_imports)]
+            use self::Foo; // Can't use Self here
+            struct Bar {
+                foo: Foo, // Foo != Self
+            }
+
+            impl Bar {
+                fn bar() -> Bar {
+                    Bar { foo: Foo {} }
+                }
+            }
+
+            // Can't use Self here
+            fn baz() -> Foo {
+                Foo {}
+            }
+        }
+
+        // Should lint here
+        fn baz() -> Foo {
+            Foo {}
         }
+    }
 
-        fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
+    enum Enum {
+        A,
+        B(u64),
+        C { field: bool },
+    }
+    impl Enum {
+        fn method() {
+            #[allow(unused_imports)]
+            use self::Enum::*; // Issue 3425
+            static STATIC: Enum = Enum::A; // Can't use Self as type
+        }
 
-        fn vals(_: Self) -> Self {
-            Self::default()
+        fn method2() {
+            let _ = Enum::B(42);
+            let _ = Enum::C { field: true };
+            let _ = Enum::A;
         }
     }
+}
 
-    impl Mul for Good {
-        type Output = Self;
+mod issue3410 {
 
-        fn mul(self, rhs: Self) -> Self {
-            rhs
-        }
+    struct A;
+    struct B;
+
+    trait Trait<T> {
+        fn a(v: T);
     }
 
-    trait NameTrait {
-        fn refs(p1: &u8) -> &u8;
-        fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
-        fn mut_refs(p1: &mut u8) -> &mut u8;
-        fn nested(p1: Box<u8>, p2: (&u8, &u8));
-        fn vals(p1: u8) -> u8;
+    impl Trait<Vec<A>> for Vec<B> {
+        fn a(_: Vec<A>) {}
     }
+}
 
-    // Using `Self` instead of the type name is OK
-    impl NameTrait for u8 {
-        fn refs(p1: &Self) -> &Self {
-            p1
-        }
+#[allow(clippy::no_effect, path_statements)]
+mod rustfix {
+    mod nested {
+        pub struct A {}
+    }
 
-        fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
-            p1
-        }
+    impl nested::A {
+        const A: bool = true;
 
-        fn mut_refs(p1: &mut Self) -> &mut Self {
-            p1
-        }
+        fn fun_1() {}
 
-        fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
+        fn fun_2() {
+            nested::A::fun_1();
+            nested::A::A;
 
-        fn vals(_: Self) -> Self {
-            Self::default()
+            nested::A {};
         }
     }
+}
 
-    // Check that self arg isn't linted
-    impl Clone for Good {
-        fn clone(&self) -> Self {
-            // Note: Not linted and it wouldn't be valid
-            // because "can't use `Self` as a constructor`"
-            Good
+mod issue3567 {
+    struct TestStruct {}
+    impl TestStruct {
+        fn from_something() -> Self {
+            Self {}
         }
     }
-}
 
-mod issue2894 {
-    trait IntoBytes {
-        fn into_bytes(&self) -> Vec<u8>;
+    trait Test {
+        fn test() -> TestStruct;
     }
 
-    // This should not be linted
-    impl IntoBytes for u8 {
-        fn into_bytes(&self) -> Vec<u8> {
-            vec![*self]
+    impl Test for TestStruct {
+        fn test() -> TestStruct {
+            TestStruct::from_something()
         }
     }
 }
 
-mod existential {
-    struct Foo;
+mod paths_created_by_lowering {
+    use std::ops::Range;
 
-    impl Foo {
-        fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
-            foos.iter()
-        }
+    struct S {}
 
-        fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
-            foos.iter()
-        }
-    }
-}
-
-mod issue3410 {
+    impl S {
+        const A: usize = 0;
+        const B: usize = 1;
 
-    struct A;
-    struct B;
+        async fn g() -> S {
+            S {}
+        }
 
-    trait Trait<T> {
-        fn a(v: T);
+        fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
+            &p[S::A..S::B]
+        }
     }
 
-    impl Trait<Vec<A>> for Vec<B> {
-        fn a(_: Vec<A>) {}
+    trait T {
+        fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
     }
-}
 
-mod issue3425 {
-    enum Enum {
-        A,
-    }
-    impl Enum {
-        fn a() {
-            use self::Enum::*;
+    impl T for Range<u8> {
+        fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
+            &p[0..1]
         }
     }
 }