]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/use_self.fixed
Merge commit '7248d06384c6a90de58c04c1f46be88821278d8b' into sync-from-clippy
[rust.git] / src / tools / clippy / tests / ui / use_self.fixed
1 // run-rustfix
2 // aux-build:proc_macro_derive.rs
3
4 #![warn(clippy::use_self)]
5 #![allow(dead_code, unreachable_code)]
6 #![allow(
7     clippy::should_implement_trait,
8     clippy::upper_case_acronyms,
9     clippy::from_over_into,
10     clippy::self_named_constructors
11 )]
12
13 #[macro_use]
14 extern crate proc_macro_derive;
15
16 fn main() {}
17
18 mod use_self {
19     struct Foo;
20
21     impl Foo {
22         fn new() -> Self {
23             Self {}
24         }
25         fn test() -> Self {
26             Self::new()
27         }
28     }
29
30     impl Default for Foo {
31         fn default() -> Self {
32             Self::new()
33         }
34     }
35 }
36
37 mod better {
38     struct Foo;
39
40     impl Foo {
41         fn new() -> Self {
42             Self {}
43         }
44         fn test() -> Self {
45             Self::new()
46         }
47     }
48
49     impl Default for Foo {
50         fn default() -> Self {
51             Self::new()
52         }
53     }
54 }
55
56 mod lifetimes {
57     struct Foo<'a> {
58         foo_str: &'a str,
59     }
60
61     impl<'a> Foo<'a> {
62         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) ->
63         // Foo<'b>`
64         fn foo(s: &str) -> Foo {
65             Foo { foo_str: s }
66         }
67         // cannot replace with `Self`, because that's `Foo<'a>`
68         fn bar() -> Foo<'static> {
69             Foo { foo_str: "foo" }
70         }
71
72         // FIXME: the lint does not handle lifetimed struct
73         // `Self` should be applicable here
74         fn clone(&self) -> Foo<'a> {
75             Foo { foo_str: self.foo_str }
76         }
77     }
78 }
79
80 mod issue2894 {
81     trait IntoBytes {
82         fn to_bytes(self) -> Vec<u8>;
83     }
84
85     // This should not be linted
86     impl IntoBytes for u8 {
87         fn to_bytes(self) -> Vec<u8> {
88             vec![self]
89         }
90     }
91 }
92
93 mod existential {
94     struct Foo;
95
96     impl Foo {
97         fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
98             foos.iter()
99         }
100
101         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
102             foos.iter()
103         }
104     }
105 }
106
107 mod tuple_structs {
108     pub struct TS(i32);
109
110     impl TS {
111         pub fn ts() -> Self {
112             Self(0)
113         }
114     }
115 }
116
117 mod macros {
118     macro_rules! use_self_expand {
119         () => {
120             fn new() -> Foo {
121                 Foo {}
122             }
123         };
124     }
125
126     struct Foo;
127
128     impl Foo {
129         use_self_expand!(); // Should not lint in local macros
130     }
131
132     #[derive(StructAUseSelf)] // Should not lint in derives
133     struct A;
134 }
135
136 mod nesting {
137     struct Foo;
138     impl Foo {
139         fn foo() {
140             #[allow(unused_imports)]
141             use self::Foo; // Can't use Self here
142             struct Bar {
143                 foo: Foo, // Foo != Self
144             }
145
146             impl Bar {
147                 fn bar() -> Self {
148                     Self { foo: Foo {} }
149                 }
150             }
151
152             // Can't use Self here
153             fn baz() -> Foo {
154                 Foo {}
155             }
156         }
157
158         // Should lint here
159         fn baz() -> Self {
160             Self {}
161         }
162     }
163
164     enum Enum {
165         A,
166         B(u64),
167         C { field: bool },
168     }
169     impl Enum {
170         fn method() {
171             #[allow(unused_imports)]
172             use self::Enum::*; // Issue 3425
173             static STATIC: Enum = Enum::A; // Can't use Self as type
174         }
175
176         fn method2() {
177             let _ = Self::B(42);
178             let _ = Self::C { field: true };
179             let _ = Self::A;
180         }
181     }
182 }
183
184 mod issue3410 {
185
186     struct A;
187     struct B;
188
189     trait Trait<T> {
190         fn a(v: T) -> Self;
191     }
192
193     impl Trait<Vec<A>> for Vec<B> {
194         fn a(_: Vec<A>) -> Self {
195             unimplemented!()
196         }
197     }
198
199     impl<T> Trait<Vec<A>> for Vec<T>
200     where
201         T: Trait<B>,
202     {
203         fn a(v: Vec<A>) -> Self {
204             <Vec<B>>::a(v).into_iter().map(Trait::a).collect()
205         }
206     }
207 }
208
209 #[allow(clippy::no_effect, path_statements)]
210 mod rustfix {
211     mod nested {
212         pub struct A;
213     }
214
215     impl nested::A {
216         const A: bool = true;
217
218         fn fun_1() {}
219
220         fn fun_2() {
221             Self::fun_1();
222             Self::A;
223
224             Self {};
225         }
226     }
227 }
228
229 mod issue3567 {
230     struct TestStruct;
231     impl TestStruct {
232         fn from_something() -> Self {
233             Self {}
234         }
235     }
236
237     trait Test {
238         fn test() -> TestStruct;
239     }
240
241     impl Test for TestStruct {
242         fn test() -> TestStruct {
243             Self::from_something()
244         }
245     }
246 }
247
248 mod paths_created_by_lowering {
249     use std::ops::Range;
250
251     struct S;
252
253     impl S {
254         const A: usize = 0;
255         const B: usize = 1;
256
257         async fn g() -> Self {
258             Self {}
259         }
260
261         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
262             &p[Self::A..Self::B]
263         }
264     }
265
266     trait T {
267         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
268     }
269
270     impl T for Range<u8> {
271         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
272             &p[0..1]
273         }
274     }
275 }
276
277 // reused from #1997
278 mod generics {
279     struct Foo<T> {
280         value: T,
281     }
282
283     impl<T> Foo<T> {
284         // `Self` is applicable here
285         fn foo(value: T) -> Self {
286             Self { value }
287         }
288
289         // `Cannot` use `Self` as a return type as the generic types are different
290         fn bar(value: i32) -> Foo<i32> {
291             Foo { value }
292         }
293     }
294 }
295
296 mod issue4140 {
297     pub struct Error<From, To> {
298         _from: From,
299         _too: To,
300     }
301
302     pub trait From<T> {
303         type From;
304         type To;
305
306         fn from(value: T) -> Self;
307     }
308
309     pub trait TryFrom<T>
310     where
311         Self: Sized,
312     {
313         type From;
314         type To;
315
316         fn try_from(value: T) -> Result<Self, Error<Self::From, Self::To>>;
317     }
318
319     // FIXME: Suggested fix results in infinite recursion.
320     // impl<F, T> TryFrom<F> for T
321     // where
322     //     T: From<F>,
323     // {
324     //     type From = Self::From;
325     //     type To = Self::To;
326
327     //     fn try_from(value: F) -> Result<Self, Error<Self::From, Self::To>> {
328     //         Ok(From::from(value))
329     //     }
330     // }
331
332     impl From<bool> for i64 {
333         type From = bool;
334         type To = Self;
335
336         fn from(value: bool) -> Self {
337             if value { 100 } else { 0 }
338         }
339     }
340 }
341
342 mod issue2843 {
343     trait Foo {
344         type Bar;
345     }
346
347     impl Foo for usize {
348         type Bar = u8;
349     }
350
351     impl<T: Foo> Foo for Option<T> {
352         type Bar = Option<T::Bar>;
353     }
354 }
355
356 mod issue3859 {
357     pub struct Foo;
358     pub struct Bar([usize; 3]);
359
360     impl Foo {
361         pub const BAR: usize = 3;
362
363         pub fn foo() {
364             const _X: usize = Foo::BAR;
365             // const _Y: usize = Self::BAR;
366         }
367     }
368 }
369
370 mod issue4305 {
371     trait Foo: 'static {}
372
373     struct Bar;
374
375     impl Foo for Bar {}
376
377     impl<T: Foo> From<T> for Box<dyn Foo> {
378         fn from(t: T) -> Self {
379             Box::new(t)
380         }
381     }
382 }
383
384 mod lint_at_item_level {
385     struct Foo;
386
387     #[allow(clippy::use_self)]
388     impl Foo {
389         fn new() -> Foo {
390             Foo {}
391         }
392     }
393
394     #[allow(clippy::use_self)]
395     impl Default for Foo {
396         fn default() -> Foo {
397             Foo::new()
398         }
399     }
400 }
401
402 mod lint_at_impl_item_level {
403     struct Foo;
404
405     impl Foo {
406         #[allow(clippy::use_self)]
407         fn new() -> Foo {
408             Foo {}
409         }
410     }
411
412     impl Default for Foo {
413         #[allow(clippy::use_self)]
414         fn default() -> Foo {
415             Foo::new()
416         }
417     }
418 }
419
420 mod issue4734 {
421     #[repr(C, packed)]
422     pub struct X {
423         pub x: u32,
424     }
425
426     impl From<X> for u32 {
427         fn from(c: X) -> Self {
428             unsafe { core::mem::transmute(c) }
429         }
430     }
431 }
432
433 mod nested_paths {
434     use std::convert::Into;
435     mod submod {
436         pub struct B;
437         pub struct C;
438
439         impl Into<C> for B {
440             fn into(self) -> C {
441                 C {}
442             }
443         }
444     }
445
446     struct A<T> {
447         t: T,
448     }
449
450     impl<T> A<T> {
451         fn new<V: Into<T>>(v: V) -> Self {
452             Self { t: Into::into(v) }
453         }
454     }
455
456     impl A<submod::C> {
457         fn test() -> Self {
458             Self::new::<submod::B>(submod::B {})
459         }
460     }
461 }
462
463 mod issue6818 {
464     #[derive(serde::Deserialize)]
465     struct A {
466         a: i32,
467     }
468 }
469
470 mod issue7206 {
471     struct MyStruct<const C: char>;
472     impl From<MyStruct<'a'>> for MyStruct<'b'> {
473         fn from(_s: MyStruct<'a'>) -> Self {
474             Self
475         }
476     }
477
478     // keep linting non-`Const` generic args
479     struct S<'a> {
480         inner: &'a str,
481     }
482
483     struct S2<T> {
484         inner: T,
485     }
486
487     impl<T> S2<T> {
488         fn new() -> Self {
489             unimplemented!();
490         }
491     }
492
493     impl<'a> S2<S<'a>> {
494         fn new_again() -> Self {
495             Self::new()
496         }
497     }
498 }
499
500 mod self_is_ty_param {
501     trait Trait {
502         type Type;
503         type Hi;
504
505         fn test();
506     }
507
508     impl<I> Trait for I
509     where
510         I: Iterator,
511         I::Item: Trait, // changing this to Self would require <Self as Iterator>
512     {
513         type Type = I;
514         type Hi = I::Item;
515
516         fn test() {
517             let _: I::Item;
518             let _: I; // this could lint, but is questionable
519         }
520     }
521 }
522
523 mod use_self_in_pat {
524     enum Foo {
525         Bar,
526         Baz,
527     }
528
529     impl Foo {
530         fn do_stuff(self) {
531             match self {
532                 Self::Bar => unimplemented!(),
533                 Self::Baz => unimplemented!(),
534             }
535             match Some(1) {
536                 Some(_) => unimplemented!(),
537                 None => unimplemented!(),
538             }
539             if let Self::Bar = self {
540                 unimplemented!()
541             }
542         }
543     }
544 }
545
546 mod issue8845 {
547     pub enum Something {
548         Num(u8),
549         TupleNums(u8, u8),
550         StructNums { one: u8, two: u8 },
551     }
552
553     struct Foo(u8);
554
555     struct Bar {
556         x: u8,
557         y: usize,
558     }
559
560     impl Something {
561         fn get_value(&self) -> u8 {
562             match self {
563                 Self::Num(n) => *n,
564                 Self::TupleNums(n, _m) => *n,
565                 Self::StructNums { one, two: _ } => *one,
566             }
567         }
568
569         fn use_crate(&self) -> u8 {
570             match self {
571                 Self::Num(n) => *n,
572                 Self::TupleNums(n, _m) => *n,
573                 Self::StructNums { one, two: _ } => *one,
574             }
575         }
576
577         fn imported_values(&self) -> u8 {
578             use Something::*;
579             match self {
580                 Num(n) => *n,
581                 TupleNums(n, _m) => *n,
582                 StructNums { one, two: _ } => *one,
583             }
584         }
585     }
586
587     impl Foo {
588         fn get_value(&self) -> u8 {
589             let Self(x) = self;
590             *x
591         }
592
593         fn use_crate(&self) -> u8 {
594             let Self(x) = self;
595             *x
596         }
597     }
598
599     impl Bar {
600         fn get_value(&self) -> u8 {
601             let Self { x, .. } = self;
602             *x
603         }
604
605         fn use_crate(&self) -> u8 {
606             let Self { x, .. } = self;
607             *x
608         }
609     }
610 }
611
612 mod issue6902 {
613     use serde::Serialize;
614
615     #[derive(Serialize)]
616     pub enum Foo {
617         Bar = 1,
618     }
619 }