]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.fixed
Auto merge of #4575 - Manishearth:suggestions, r=oli-obk
[rust.git] / tests / ui / use_self.fixed
1 // run-rustfix
2
3 #![warn(clippy::use_self)]
4 #![allow(dead_code)]
5 #![allow(clippy::should_implement_trait)]
6
7 fn main() {}
8
9 mod use_self {
10     struct Foo {}
11
12     impl Foo {
13         fn new() -> Self {
14             Self {}
15         }
16         fn test() -> Self {
17             Self::new()
18         }
19     }
20
21     impl Default for Foo {
22         fn default() -> Self {
23             Self::new()
24         }
25     }
26 }
27
28 mod better {
29     struct Foo {}
30
31     impl Foo {
32         fn new() -> Self {
33             Self {}
34         }
35         fn test() -> Self {
36             Self::new()
37         }
38     }
39
40     impl Default for Foo {
41         fn default() -> Self {
42             Self::new()
43         }
44     }
45 }
46
47 mod lifetimes {
48     struct Foo<'a> {
49         foo_str: &'a str,
50     }
51
52     impl<'a> Foo<'a> {
53         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
54         // Foo<'b>`
55         fn foo(s: &str) -> Foo {
56             Foo { foo_str: s }
57         }
58         // cannot replace with `Self`, because that's `Foo<'a>`
59         fn bar() -> Foo<'static> {
60             Foo { foo_str: "foo" }
61         }
62
63         // FIXME: the lint does not handle lifetimed struct
64         // `Self` should be applicable here
65         fn clone(&self) -> Foo<'a> {
66             Foo { foo_str: self.foo_str }
67         }
68     }
69 }
70
71 #[allow(clippy::boxed_local)]
72 mod traits {
73
74     use std::ops::Mul;
75
76     trait SelfTrait {
77         fn refs(p1: &Self) -> &Self;
78         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
79         fn mut_refs(p1: &mut Self) -> &mut Self;
80         fn nested(p1: Box<Self>, p2: (&u8, &Self));
81         fn vals(r: Self) -> Self;
82     }
83
84     #[derive(Default)]
85     struct Bad;
86
87     impl SelfTrait for Bad {
88         fn refs(p1: &Self) -> &Self {
89             p1
90         }
91
92         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
93             p1
94         }
95
96         fn mut_refs(p1: &mut Self) -> &mut Self {
97             p1
98         }
99
100         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
101
102         fn vals(_: Self) -> Self {
103             Self::default()
104         }
105     }
106
107     impl Mul for Bad {
108         type Output = Self;
109
110         fn mul(self, rhs: Self) -> Self {
111             rhs
112         }
113     }
114
115     impl Clone for Bad {
116         fn clone(&self) -> Self {
117             Self
118         }
119     }
120
121     #[derive(Default)]
122     struct Good;
123
124     impl SelfTrait for Good {
125         fn refs(p1: &Self) -> &Self {
126             p1
127         }
128
129         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
130             p1
131         }
132
133         fn mut_refs(p1: &mut Self) -> &mut Self {
134             p1
135         }
136
137         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {}
138
139         fn vals(_: Self) -> Self {
140             Self::default()
141         }
142     }
143
144     impl Mul for Good {
145         type Output = Self;
146
147         fn mul(self, rhs: Self) -> Self {
148             rhs
149         }
150     }
151
152     trait NameTrait {
153         fn refs(p1: &u8) -> &u8;
154         fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
155         fn mut_refs(p1: &mut u8) -> &mut u8;
156         fn nested(p1: Box<u8>, p2: (&u8, &u8));
157         fn vals(p1: u8) -> u8;
158     }
159
160     // Using `Self` instead of the type name is OK
161     impl NameTrait for u8 {
162         fn refs(p1: &Self) -> &Self {
163             p1
164         }
165
166         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
167             p1
168         }
169
170         fn mut_refs(p1: &mut Self) -> &mut Self {
171             p1
172         }
173
174         fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {}
175
176         fn vals(_: Self) -> Self {
177             Self::default()
178         }
179     }
180 }
181
182 mod issue2894 {
183     trait IntoBytes {
184         fn into_bytes(&self) -> Vec<u8>;
185     }
186
187     // This should not be linted
188     impl IntoBytes for u8 {
189         fn into_bytes(&self) -> Vec<u8> {
190             vec![*self]
191         }
192     }
193 }
194
195 mod existential {
196     struct Foo;
197
198     impl Foo {
199         fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
200             foos.iter()
201         }
202
203         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
204             foos.iter()
205         }
206     }
207 }
208
209 mod tuple_structs {
210     pub struct TS(i32);
211
212     impl TS {
213         pub fn ts() -> Self {
214             Self(0)
215         }
216     }
217 }
218
219 mod macros {
220     macro_rules! use_self_expand {
221         () => {
222             fn new() -> Self {
223                 Self {}
224             }
225         };
226     }
227
228     struct Foo {}
229
230     impl Foo {
231         use_self_expand!(); // Should lint in local macros
232     }
233 }
234
235 mod nesting {
236     struct Foo {}
237     impl Foo {
238         fn foo() {
239             #[allow(unused_imports)]
240             use self::Foo; // Can't use Self here
241             struct Bar {
242                 foo: Foo, // Foo != Self
243             }
244
245             impl Bar {
246                 fn bar() -> Self {
247                     Self { foo: Foo {} }
248                 }
249             }
250
251             // Can't use Self here
252             fn baz() -> Foo {
253                 Foo {}
254             }
255         }
256
257         // Should lint here
258         fn baz() -> Self {
259             Self {}
260         }
261     }
262
263     enum Enum {
264         A,
265         B(u64),
266         C { field: bool },
267     }
268     impl Enum {
269         fn method() {
270             #[allow(unused_imports)]
271             use self::Enum::*; // Issue 3425
272             static STATIC: Enum = Enum::A; // Can't use Self as type
273         }
274
275         fn method2() {
276             let _ = Self::B(42);
277             let _ = Self::C { field: true };
278             let _ = Self::A;
279         }
280     }
281 }
282
283 mod issue3410 {
284
285     struct A;
286     struct B;
287
288     trait Trait<T> {
289         fn a(v: T);
290     }
291
292     impl Trait<Vec<A>> for Vec<B> {
293         fn a(_: Vec<A>) {}
294     }
295 }
296
297 #[allow(clippy::no_effect, path_statements)]
298 mod rustfix {
299     mod nested {
300         pub struct A {}
301     }
302
303     impl nested::A {
304         const A: bool = true;
305
306         fn fun_1() {}
307
308         fn fun_2() {
309             Self::fun_1();
310             Self::A;
311
312             Self {};
313         }
314     }
315 }
316
317 mod issue3567 {
318     struct TestStruct {}
319     impl TestStruct {
320         fn from_something() -> Self {
321             Self {}
322         }
323     }
324
325     trait Test {
326         fn test() -> TestStruct;
327     }
328
329     impl Test for TestStruct {
330         fn test() -> TestStruct {
331             Self::from_something()
332         }
333     }
334 }