]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.fixed
Merge branch 'master' into fix-4727
[rust.git] / tests / ui / use_self.fixed
1 // run-rustfix
2 // compile-flags: --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() -> Self {
15             Self {}
16         }
17         fn test() -> Self {
18             Self::new()
19         }
20     }
21
22     impl Default for Foo {
23         fn default() -> Self {
24             Self::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 #[allow(clippy::boxed_local)]
73 mod traits {
74
75     use std::ops::Mul;
76
77     trait SelfTrait {
78         fn refs(p1: &Self) -> &Self;
79         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
80         fn mut_refs(p1: &mut Self) -> &mut Self;
81         fn nested(p1: Box<Self>, p2: (&u8, &Self));
82         fn vals(r: Self) -> Self;
83     }
84
85     #[derive(Default)]
86     struct Bad;
87
88     impl SelfTrait for Bad {
89         fn refs(p1: &Self) -> &Self {
90             p1
91         }
92
93         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
94             p1
95         }
96
97         fn mut_refs(p1: &mut Self) -> &mut Self {
98             p1
99         }
100
101         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
102
103         fn vals(_: Self) -> Self {
104             Self::default()
105         }
106     }
107
108     impl Mul for Bad {
109         type Output = Self;
110
111         fn mul(self, rhs: Self) -> Self {
112             rhs
113         }
114     }
115
116     impl Clone for Bad {
117         fn clone(&self) -> Self {
118             Self
119         }
120     }
121
122     #[derive(Default)]
123     struct Good;
124
125     impl SelfTrait for Good {
126         fn refs(p1: &Self) -> &Self {
127             p1
128         }
129
130         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
131             p1
132         }
133
134         fn mut_refs(p1: &mut Self) -> &mut Self {
135             p1
136         }
137
138         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
139
140         fn vals(_: Self) -> Self {
141             Self::default()
142         }
143     }
144
145     impl Mul for Good {
146         type Output = Self;
147
148         fn mul(self, rhs: Self) -> Self {
149             rhs
150         }
151     }
152
153     trait NameTrait {
154         fn refs(p1: &u8) -> &u8;
155         fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
156         fn mut_refs(p1: &mut u8) -> &mut u8;
157         fn nested(p1: Box<u8>, p2: (&u8, &u8));
158         fn vals(p1: u8) -> u8;
159     }
160
161     // Using `Self` instead of the type name is OK
162     impl NameTrait for u8 {
163         fn refs(p1: &Self) -> &Self {
164             p1
165         }
166
167         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
168             p1
169         }
170
171         fn mut_refs(p1: &mut Self) -> &mut Self {
172             p1
173         }
174
175         fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
176
177         fn vals(_: Self) -> Self {
178             Self::default()
179         }
180     }
181 }
182
183 mod issue2894 {
184     trait IntoBytes {
185         fn into_bytes(&self) -> Vec<u8>;
186     }
187
188     // This should not be linted
189     impl IntoBytes for u8 {
190         fn into_bytes(&self) -> Vec<u8> {
191             vec![*self]
192         }
193     }
194 }
195
196 mod existential {
197     struct Foo;
198
199     impl Foo {
200         fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
201             foos.iter()
202         }
203
204         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
205             foos.iter()
206         }
207     }
208 }
209
210 mod tuple_structs {
211     pub struct TS(i32);
212
213     impl TS {
214         pub fn ts() -> Self {
215             Self(0)
216         }
217     }
218 }
219
220 mod macros {
221     macro_rules! use_self_expand {
222         () => {
223             fn new() -> Self {
224                 Self {}
225             }
226         };
227     }
228
229     struct Foo {}
230
231     impl Foo {
232         use_self_expand!(); // Should lint in local macros
233     }
234 }
235
236 mod nesting {
237     struct Foo {}
238     impl Foo {
239         fn foo() {
240             #[allow(unused_imports)]
241             use self::Foo; // Can't use Self here
242             struct Bar {
243                 foo: Foo, // Foo != Self
244             }
245
246             impl Bar {
247                 fn bar() -> Self {
248                     Self { foo: Foo {} }
249                 }
250             }
251
252             // Can't use Self here
253             fn baz() -> Foo {
254                 Foo {}
255             }
256         }
257
258         // Should lint here
259         fn baz() -> Self {
260             Self {}
261         }
262     }
263
264     enum Enum {
265         A,
266         B(u64),
267         C { field: bool },
268     }
269     impl Enum {
270         fn method() {
271             #[allow(unused_imports)]
272             use self::Enum::*; // Issue 3425
273             static STATIC: Enum = Enum::A; // Can't use Self as type
274         }
275
276         fn method2() {
277             let _ = Self::B(42);
278             let _ = Self::C { field: true };
279             let _ = Self::A;
280         }
281     }
282 }
283
284 mod issue3410 {
285
286     struct A;
287     struct B;
288
289     trait Trait<T> {
290         fn a(v: T);
291     }
292
293     impl Trait<Vec<A>> for Vec<B> {
294         fn a(_: Vec<A>) {}
295     }
296 }
297
298 #[allow(clippy::no_effect, path_statements)]
299 mod rustfix {
300     mod nested {
301         pub struct A {}
302     }
303
304     impl nested::A {
305         const A: bool = true;
306
307         fn fun_1() {}
308
309         fn fun_2() {
310             Self::fun_1();
311             Self::A;
312
313             Self {};
314         }
315     }
316 }
317
318 mod issue3567 {
319     struct TestStruct {}
320     impl TestStruct {
321         fn from_something() -> Self {
322             Self {}
323         }
324     }
325
326     trait Test {
327         fn test() -> TestStruct;
328     }
329
330     impl Test for TestStruct {
331         fn test() -> TestStruct {
332             Self::from_something()
333         }
334     }
335 }
336
337 mod paths_created_by_lowering {
338     use std::ops::Range;
339
340     struct S {}
341
342     impl S {
343         const A: usize = 0;
344         const B: usize = 1;
345
346         async fn g() -> Self {
347             Self {}
348         }
349
350         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
351             &p[Self::A..Self::B]
352         }
353     }
354
355     trait T {
356         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
357     }
358
359     impl T for Range<u8> {
360         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
361             &p[0..1]
362         }
363     }
364 }