]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.fixed
Auto merge of #5272 - jmeyers35:file_read_lint, r=flip1995
[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)]
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 mod issue2894 {
73     trait IntoBytes {
74         fn into_bytes(&self) -> Vec<u8>;
75     }
76
77     // This should not be linted
78     impl IntoBytes for u8 {
79         fn into_bytes(&self) -> Vec<u8> {
80             vec![*self]
81         }
82     }
83 }
84
85 mod existential {
86     struct Foo;
87
88     impl Foo {
89         fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
90             foos.iter()
91         }
92
93         fn good(foos: &[Self]) -> impl Iterator<Item = &Self> {
94             foos.iter()
95         }
96     }
97 }
98
99 mod tuple_structs {
100     pub struct TS(i32);
101
102     impl TS {
103         pub fn ts() -> Self {
104             Self(0)
105         }
106     }
107 }
108
109 mod macros {
110     macro_rules! use_self_expand {
111         () => {
112             fn new() -> Self {
113                 Self {}
114             }
115         };
116     }
117
118     struct Foo {}
119
120     impl Foo {
121         use_self_expand!(); // Should lint in local macros
122     }
123 }
124
125 mod nesting {
126     struct Foo {}
127     impl Foo {
128         fn foo() {
129             #[allow(unused_imports)]
130             use self::Foo; // Can't use Self here
131             struct Bar {
132                 foo: Foo, // Foo != Self
133             }
134
135             impl Bar {
136                 fn bar() -> Self {
137                     Self { foo: Foo {} }
138                 }
139             }
140
141             // Can't use Self here
142             fn baz() -> Foo {
143                 Foo {}
144             }
145         }
146
147         // Should lint here
148         fn baz() -> Self {
149             Self {}
150         }
151     }
152
153     enum Enum {
154         A,
155         B(u64),
156         C { field: bool },
157     }
158     impl Enum {
159         fn method() {
160             #[allow(unused_imports)]
161             use self::Enum::*; // Issue 3425
162             static STATIC: Enum = Enum::A; // Can't use Self as type
163         }
164
165         fn method2() {
166             let _ = Self::B(42);
167             let _ = Self::C { field: true };
168             let _ = Self::A;
169         }
170     }
171 }
172
173 mod issue3410 {
174
175     struct A;
176     struct B;
177
178     trait Trait<T> {
179         fn a(v: T);
180     }
181
182     impl Trait<Vec<A>> for Vec<B> {
183         fn a(_: Vec<A>) {}
184     }
185 }
186
187 #[allow(clippy::no_effect, path_statements)]
188 mod rustfix {
189     mod nested {
190         pub struct A {}
191     }
192
193     impl nested::A {
194         const A: bool = true;
195
196         fn fun_1() {}
197
198         fn fun_2() {
199             Self::fun_1();
200             Self::A;
201
202             Self {};
203         }
204     }
205 }
206
207 mod issue3567 {
208     struct TestStruct {}
209     impl TestStruct {
210         fn from_something() -> Self {
211             Self {}
212         }
213     }
214
215     trait Test {
216         fn test() -> TestStruct;
217     }
218
219     impl Test for TestStruct {
220         fn test() -> TestStruct {
221             Self::from_something()
222         }
223     }
224 }
225
226 mod paths_created_by_lowering {
227     use std::ops::Range;
228
229     struct S {}
230
231     impl S {
232         const A: usize = 0;
233         const B: usize = 1;
234
235         async fn g() -> Self {
236             Self {}
237         }
238
239         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
240             &p[Self::A..Self::B]
241         }
242     }
243
244     trait T {
245         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8];
246     }
247
248     impl T for Range<u8> {
249         fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
250             &p[0..1]
251         }
252     }
253 }