]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.fixed
rework use_self impl based on ty::Ty comparison
[rust.git] / tests / ui / use_self.fixed
1 // run-rustfix
2 // edition:2018
3
4 #![warn(clippy::use_self)]
5 #![allow(dead_code)]
6 #![allow(clippy::should_implement_trait, clippy::upper_case_acronyms)]
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             Foo::new()
19         }
20     }
21
22     impl Default for Foo {
23         fn default() -> Self {
24             // FIXME: applicable here
25             Foo::new()
26         }
27     }
28 }
29
30 mod better {
31     struct Foo {}
32
33     impl Foo {
34         fn new() -> Self {
35             Self {}
36         }
37         fn test() -> Self {
38             Self::new()
39         }
40     }
41
42     impl Default for Foo {
43         fn default() -> Self {
44             Self::new()
45         }
46     }
47 }
48
49 mod lifetimes {
50     struct Foo<'a> {
51         foo_str: &'a str,
52     }
53
54     impl<'a> Foo<'a> {
55         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
56         // Foo<'b>`
57         fn foo(s: &str) -> Foo {
58             Foo { foo_str: s }
59         }
60         // cannot replace with `Self`, because that's `Foo<'a>`
61         fn bar() -> Foo<'static> {
62             Foo { foo_str: "foo" }
63         }
64
65         // FIXME: the lint does not handle lifetimed struct
66         // `Self` should be applicable here
67         fn clone(&self) -> Foo<'a> {
68             Foo { foo_str: self.foo_str }
69         }
70     }
71 }
72
73 mod issue2894 {
74     trait IntoBytes {
75         #[allow(clippy::wrong_self_convention)]
76         fn into_bytes(&self) -> Vec<u8>;
77     }
78
79     // This should not be linted
80     impl IntoBytes for u8 {
81         fn into_bytes(&self) -> Vec<u8> {
82             vec![*self]
83         }
84     }
85 }
86
87 mod existential {
88     struct Foo;
89
90     impl Foo {
91         // FIXME:
92         // TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
93         // See documentation in rustc_hir::hir::TyKind.
94         // The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
95         fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
96             foos.iter()
97         }
98
99         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
100             foos.iter()
101         }
102     }
103 }
104
105 mod tuple_structs {
106     pub struct TS(i32);
107
108     impl TS {
109         pub fn ts() -> Self {
110             Self(0)
111         }
112     }
113 }
114
115 mod macros {
116     macro_rules! use_self_expand {
117         () => {
118             fn new() -> Self {
119                 Self {}
120             }
121         };
122     }
123
124     struct Foo {}
125
126     impl Foo {
127         use_self_expand!(); // Should lint in local macros
128     }
129 }
130
131 mod nesting {
132     struct Foo {}
133     impl Foo {
134         fn foo() {
135             #[allow(unused_imports)]
136             use self::Foo; // Can't use Self here
137             struct Bar {
138                 foo: Foo, // Foo != Self
139             }
140
141             impl Bar {
142                 fn bar() -> Self {
143                     Self { foo: Foo {} }
144                 }
145             }
146
147             // Can't use Self here
148             fn baz() -> Foo {
149                 Foo {}
150             }
151         }
152
153         // Should lint here
154         fn baz() -> Self {
155             Self {}
156         }
157     }
158
159     enum Enum {
160         A,
161         B(u64),
162         C { field: bool },
163     }
164     impl Enum {
165         fn method() {
166             #[allow(unused_imports)]
167             use self::Enum::*; // Issue 3425
168             static STATIC: Enum = Enum::A; // Can't use Self as type
169         }
170
171         fn method2() {
172             let _ = Self::B(42);
173             let _ = Self::C { field: true };
174             let _ = Self::A;
175         }
176     }
177 }
178
179 mod issue3410 {
180
181     struct A;
182     struct B;
183
184     trait Trait<T> {
185         fn a(v: T) -> Self;
186     }
187
188     impl Trait<Vec<A>> for Vec<B> {
189         fn a(_: Vec<A>) -> Self {
190             unimplemented!()
191         }
192     }
193
194     impl<T> Trait<Vec<A>> for Vec<T>
195     where
196         T: Trait<B>,
197     {
198         fn a(v: Vec<A>) -> Self {
199             <Vec<B>>::a(v).into_iter().map(Trait::a).collect()
200         }
201     }
202 }
203
204 #[allow(clippy::no_effect, path_statements)]
205 mod rustfix {
206     mod nested {
207         pub struct A {}
208     }
209
210     impl nested::A {
211         const A: bool = true;
212
213         fn fun_1() {}
214
215         fn fun_2() {
216             nested::A::fun_1();
217             nested::A::A;
218
219             Self {};
220         }
221     }
222 }
223
224 mod issue3567 {
225     struct TestStruct {}
226     impl TestStruct {
227         fn from_something() -> Self {
228             Self {}
229         }
230     }
231
232     trait Test {
233         fn test() -> TestStruct;
234     }
235
236     impl Test for TestStruct {
237         fn test() -> TestStruct {
238             // FIXME: applicable here
239             TestStruct::from_something()
240         }
241     }
242 }
243
244 mod paths_created_by_lowering {
245     use std::ops::Range;
246
247     struct S {}
248
249     impl S {
250         const A: usize = 0;
251         const B: usize = 1;
252
253         // FIXME: applicable here
254         async fn g() -> S {
255             Self {}
256         }
257
258         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
259             // FIXME: applicable here twice
260             &p[S::A..S::B]
261         }
262     }
263
264     trait T {
265         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
266     }
267
268     impl T for Range<u8> {
269         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
270             &p[0..1]
271         }
272     }
273 }
274
275 // reused from #1997
276 mod generics {
277     struct Foo<T> {
278         value: T,
279     }
280
281     impl<T> Foo<T> {
282         // `Self` is applicable here
283         fn foo(value: T) -> Self {
284             Self { value }
285         }
286
287         // `Cannot` use `Self` as a return type as the generic types are different
288         fn bar(value: i32) -> Foo<i32> {
289             Foo { value }
290         }
291     }
292 }
293
294 mod issue4140 {
295     pub struct Error<From, To> {
296         _from: From,
297         _too: To,
298     }
299
300     pub trait From<T> {
301         type From;
302         type To;
303
304         fn from(value: T) -> Self;
305     }
306
307     pub trait TryFrom<T>
308     where
309         Self: Sized,
310     {
311         type From;
312         type To;
313
314         fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>;
315     }
316
317     impl<F, T> TryFrom<F> for T
318     where
319         T: From<F>,
320     {
321         type From = Self;
322         type To = Self;
323
324         fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
325             Ok(From::from(value))
326         }
327     }
328
329     impl From<bool> for i64 {
330         type From = bool;
331         type To = Self;
332
333         fn from(value: bool) -> Self {
334             if value {
335                 100
336             } else {
337                 0
338             }
339         }
340     }
341 }
342
343 mod issue2843 {
344     trait Foo {
345         type Bar;
346     }
347
348     impl Foo for usize {
349         type Bar = u8;
350     }
351
352     impl<T: Foo> Foo for Option<T> {
353         type Bar = Option<T::Bar>;
354     }
355 }
356
357 mod issue3859 {
358     pub struct Foo;
359     pub struct Bar([usize; 3]);
360
361     impl Foo {
362         pub const BAR: usize = 3;
363
364         pub fn foo() {
365             const _X: usize = Foo::BAR;
366             // const _Y: usize = Self::BAR;
367         }
368     }
369 }
370
371 mod issue4305 {
372     trait Foo: 'static {}
373
374     struct Bar;
375
376     impl Foo for Bar {}
377
378     impl<T: Foo> From<T> for Box<dyn Foo> {
379         fn from(t: T) -> Self {
380             // FIXME: applicable here
381             Box::new(t)
382         }
383     }
384 }
385
386 mod lint_at_item_level {
387     struct Foo {}
388
389     #[allow(clippy::use_self)]
390     impl Foo {
391         fn new() -> Foo {
392             Foo {}
393         }
394     }
395
396     #[allow(clippy::use_self)]
397     impl Default for Foo {
398         fn default() -> Foo {
399             Foo::new()
400         }
401     }
402 }
403
404 mod lint_at_impl_item_level {
405     struct Foo {}
406
407     impl Foo {
408         #[allow(clippy::use_self)]
409         fn new() -> Foo {
410             Foo {}
411         }
412     }
413
414     impl Default for Foo {
415         #[allow(clippy::use_self)]
416         fn default() -> Foo {
417             Foo::new()
418         }
419     }
420 }
421
422 mod issue4734 {
423     #[repr(C, packed)]
424     pub struct X {
425         pub x: u32,
426     }
427
428     impl From<X> for u32 {
429         fn from(c: X) -> Self {
430             unsafe { core::mem::transmute(c) }
431         }
432     }
433 }
434
435 mod nested_paths {
436     use std::convert::Into;
437     mod submod {
438         pub struct B {}
439         pub struct C {}
440
441         impl Into<C> for B {
442             fn into(self) -> C {
443                 C {}
444             }
445         }
446     }
447
448     struct A<T> {
449         t: T,
450     }
451
452     impl<T> A<T> {
453         fn new<V: Into<T>>(v: V) -> Self {
454             Self { t: Into::into(v) }
455         }
456     }
457
458     impl A<submod::C> {
459         fn test() -> Self {
460             // FIXME: applicable here
461             A::new::<submod::B>(submod::B {})
462         }
463     }
464 }