]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.rs
Merge remote-tracking branch 'upstream/master' into doc-markdown
[rust.git] / tests / ui / use_self.rs
1 // run-rustfix
2 // edition:2018
3
4 #![warn(clippy::use_self)]
5 #![allow(dead_code)]
6 #![allow(clippy::should_implement_trait)]
7
8 fn main() {}
9
10 mod use_self {
11     struct Foo {}
12
13     impl Foo {
14         fn new() -> Foo {
15             Foo {}
16         }
17         fn test() -> Foo {
18             Foo::new()
19         }
20     }
21
22     impl Default for Foo {
23         fn default() -> Foo {
24             Foo::new()
25         }
26     }
27 }
28
29 mod better {
30     struct Foo {}
31
32     impl Foo {
33         fn new() -> Self {
34             Self {}
35         }
36         fn test() -> Self {
37             Self::new()
38         }
39     }
40
41     impl Default for Foo {
42         fn default() -> Self {
43             Self::new()
44         }
45     }
46 }
47
48 mod lifetimes {
49     struct Foo<'a> {
50         foo_str: &'a str,
51     }
52
53     impl<'a> Foo<'a> {
54         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
55         // Foo<'b>`
56         fn foo(s: &str) -> Foo {
57             Foo { foo_str: s }
58         }
59         // cannot replace with `Self`, because that's `Foo<'a>`
60         fn bar() -> Foo<'static> {
61             Foo { foo_str: "foo" }
62         }
63
64         // FIXME: the lint does not handle lifetimed struct
65         // `Self` should be applicable here
66         fn clone(&self) -> Foo<'a> {
67             Foo { foo_str: self.foo_str }
68         }
69     }
70 }
71
72 mod issue2894 {
73     trait IntoBytes {
74         #[allow(clippy::wrong_self_convention)]
75         fn into_bytes(&self) -> Vec<u8>;
76     }
77
78     // This should not be linted
79     impl IntoBytes for u8 {
80         fn into_bytes(&self) -> Vec<u8> {
81             vec![*self]
82         }
83     }
84 }
85
86 mod existential {
87     struct Foo;
88
89     impl Foo {
90         fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
91             foos.iter()
92         }
93
94         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
95             foos.iter()
96         }
97     }
98 }
99
100 mod tuple_structs {
101     pub struct TS(i32);
102
103     impl TS {
104         pub fn ts() -> Self {
105             TS(0)
106         }
107     }
108 }
109
110 mod macros {
111     macro_rules! use_self_expand {
112         () => {
113             fn new() -> Foo {
114                 Foo {}
115             }
116         };
117     }
118
119     struct Foo {}
120
121     impl Foo {
122         use_self_expand!(); // Should lint in local macros
123     }
124 }
125
126 mod nesting {
127     struct Foo {}
128     impl Foo {
129         fn foo() {
130             #[allow(unused_imports)]
131             use self::Foo; // Can't use Self here
132             struct Bar {
133                 foo: Foo, // Foo != Self
134             }
135
136             impl Bar {
137                 fn bar() -> Bar {
138                     Bar { foo: Foo {} }
139                 }
140             }
141
142             // Can't use Self here
143             fn baz() -> Foo {
144                 Foo {}
145             }
146         }
147
148         // Should lint here
149         fn baz() -> Foo {
150             Foo {}
151         }
152     }
153
154     enum Enum {
155         A,
156         B(u64),
157         C { field: bool },
158     }
159     impl Enum {
160         fn method() {
161             #[allow(unused_imports)]
162             use self::Enum::*; // Issue 3425
163             static STATIC: Enum = Enum::A; // Can't use Self as type
164         }
165
166         fn method2() {
167             let _ = Enum::B(42);
168             let _ = Enum::C { field: true };
169             let _ = Enum::A;
170         }
171     }
172 }
173
174 mod issue3410 {
175
176     struct A;
177     struct B;
178
179     trait Trait<T> {
180         fn a(v: T);
181     }
182
183     impl Trait<Vec<A>> for Vec<B> {
184         fn a(_: Vec<A>) {}
185     }
186 }
187
188 #[allow(clippy::no_effect, path_statements)]
189 mod rustfix {
190     mod nested {
191         pub struct A {}
192     }
193
194     impl nested::A {
195         const A: bool = true;
196
197         fn fun_1() {}
198
199         fn fun_2() {
200             nested::A::fun_1();
201             nested::A::A;
202
203             nested::A {};
204         }
205     }
206 }
207
208 mod issue3567 {
209     struct TestStruct {}
210     impl TestStruct {
211         fn from_something() -> Self {
212             Self {}
213         }
214     }
215
216     trait Test {
217         fn test() -> TestStruct;
218     }
219
220     impl Test for TestStruct {
221         fn test() -> TestStruct {
222             TestStruct::from_something()
223         }
224     }
225 }
226
227 mod paths_created_by_lowering {
228     use std::ops::Range;
229
230     struct S {}
231
232     impl S {
233         const A: usize = 0;
234         const B: usize = 1;
235
236         async fn g() -> S {
237             S {}
238         }
239
240         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
241             &p[S::A..S::B]
242         }
243     }
244
245     trait T {
246         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
247     }
248
249     impl T for Range<u8> {
250         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
251             &p[0..1]
252         }
253     }
254 }