1 #![warn(clippy::new_ret_no_self)]
2 #![allow(dead_code)]
3
main()4 fn main() {}
5
6 trait R {
7 type Item;
8 }
9
10 trait Q {
11 type Item;
12 type Item2;
13 }
14
15 struct S;
16
17 impl R for S {
18 type Item = Self;
19 }
20
21 impl S {
22 // should not trigger the lint
new() -> impl R<Item = Self>23 pub fn new() -> impl R<Item = Self> {
24 S
25 }
26 }
27
28 struct S2;
29
30 impl R for S2 {
31 type Item = Self;
32 }
33
34 impl S2 {
35 // should not trigger the lint
new(_: String) -> impl R<Item = Self>36 pub fn new(_: String) -> impl R<Item = Self> {
37 S2
38 }
39 }
40
41 struct S3;
42
43 impl R for S3 {
44 type Item = u32;
45 }
46
47 impl S3 {
48 // should trigger the lint
new(_: String) -> impl R<Item = u32>49 pub fn new(_: String) -> impl R<Item = u32> {
50 S3
51 }
52 }
53
54 struct S4;
55
56 impl Q for S4 {
57 type Item = u32;
58 type Item2 = Self;
59 }
60
61 impl S4 {
62 // should not trigger the lint
new(_: String) -> impl Q<Item = u32, Item2 = Self>63 pub fn new(_: String) -> impl Q<Item = u32, Item2 = Self> {
64 S4
65 }
66 }
67
68 struct T;
69
70 impl T {
71 // should not trigger lint
new() -> Self72 pub fn new() -> Self {
73 unimplemented!();
74 }
75 }
76
77 struct U;
78
79 impl U {
80 // should trigger lint
new() -> u3281 pub fn new() -> u32 {
82 unimplemented!();
83 }
84 }
85
86 struct V;
87
88 impl V {
89 // should trigger lint
new(_: String) -> u3290 pub fn new(_: String) -> u32 {
91 unimplemented!();
92 }
93 }
94
95 struct TupleReturnerOk;
96
97 impl TupleReturnerOk {
98 // should not trigger lint
new() -> (Self, u32)99 pub fn new() -> (Self, u32) {
100 unimplemented!();
101 }
102 }
103
104 struct TupleReturnerOk2;
105
106 impl TupleReturnerOk2 {
107 // should not trigger lint (it doesn't matter which element in the tuple is Self)
new() -> (u32, Self)108 pub fn new() -> (u32, Self) {
109 unimplemented!();
110 }
111 }
112
113 struct TupleReturnerOk3;
114
115 impl TupleReturnerOk3 {
116 // should not trigger lint (tuple can contain multiple Self)
new() -> (Self, Self)117 pub fn new() -> (Self, Self) {
118 unimplemented!();
119 }
120 }
121
122 struct TupleReturnerBad;
123
124 impl TupleReturnerBad {
125 // should trigger lint
new() -> (u32, u32)126 pub fn new() -> (u32, u32) {
127 unimplemented!();
128 }
129 }
130
131 struct MutPointerReturnerOk;
132
133 impl MutPointerReturnerOk {
134 // should not trigger lint
new() -> *mut Self135 pub fn new() -> *mut Self {
136 unimplemented!();
137 }
138 }
139
140 struct ConstPointerReturnerOk2;
141
142 impl ConstPointerReturnerOk2 {
143 // should not trigger lint
new() -> *const Self144 pub fn new() -> *const Self {
145 unimplemented!();
146 }
147 }
148
149 struct MutPointerReturnerBad;
150
151 impl MutPointerReturnerBad {
152 // should trigger lint
new() -> *mut V153 pub fn new() -> *mut V {
154 unimplemented!();
155 }
156 }
157
158 struct GenericReturnerOk;
159
160 impl GenericReturnerOk {
161 // should not trigger lint
new() -> Option<Self>162 pub fn new() -> Option<Self> {
163 unimplemented!();
164 }
165 }
166
167 struct GenericReturnerBad;
168
169 impl GenericReturnerBad {
170 // should trigger lint
new() -> Option<u32>171 pub fn new() -> Option<u32> {
172 unimplemented!();
173 }
174 }
175
176 struct NestedReturnerOk;
177
178 impl NestedReturnerOk {
179 // should not trigger lint
new() -> (Option<Self>, u32)180 pub fn new() -> (Option<Self>, u32) {
181 unimplemented!();
182 }
183 }
184
185 struct NestedReturnerOk2;
186
187 impl NestedReturnerOk2 {
188 // should not trigger lint
new() -> ((Self, u32), u32)189 pub fn new() -> ((Self, u32), u32) {
190 unimplemented!();
191 }
192 }
193
194 struct NestedReturnerOk3;
195
196 impl NestedReturnerOk3 {
197 // should not trigger lint
new() -> Option<(Self, u32)>198 pub fn new() -> Option<(Self, u32)> {
199 unimplemented!();
200 }
201 }
202
203 struct WithLifetime<'a> {
204 cat: &'a str,
205 }
206
207 impl<'a> WithLifetime<'a> {
208 // should not trigger the lint, because the lifetimes are different
new<'b: 'a>(s: &'b str) -> WithLifetime<'b>209 pub fn new<'b: 'a>(s: &'b str) -> WithLifetime<'b> {
210 unimplemented!();
211 }
212 }
213
214 mod issue5435 {
215 struct V;
216
217 pub trait TraitRetSelf {
218 // should not trigger lint
new() -> Self219 fn new() -> Self;
220 }
221
222 pub trait TraitRet {
223 // should trigger lint as we are in trait definition
new() -> String224 fn new() -> String;
225 }
226 pub struct StructRet;
227 impl TraitRet for StructRet {
228 // should not trigger lint as we are in the impl block
new() -> String229 fn new() -> String {
230 unimplemented!();
231 }
232 }
233
234 pub trait TraitRet2 {
235 // should trigger lint
new(_: String) -> String236 fn new(_: String) -> String;
237 }
238
239 trait TupleReturnerOk {
240 // should not trigger lint
new() -> (Self, u32) where Self: Sized,241 fn new() -> (Self, u32)
242 where
243 Self: Sized,
244 {
245 unimplemented!();
246 }
247 }
248
249 trait TupleReturnerOk2 {
250 // should not trigger lint (it doesn't matter which element in the tuple is Self)
new() -> (u32, Self) where Self: Sized,251 fn new() -> (u32, Self)
252 where
253 Self: Sized,
254 {
255 unimplemented!();
256 }
257 }
258
259 trait TupleReturnerOk3 {
260 // should not trigger lint (tuple can contain multiple Self)
new() -> (Self, Self) where Self: Sized,261 fn new() -> (Self, Self)
262 where
263 Self: Sized,
264 {
265 unimplemented!();
266 }
267 }
268
269 trait TupleReturnerBad {
270 // should trigger lint
new() -> (u32, u32)271 fn new() -> (u32, u32) {
272 unimplemented!();
273 }
274 }
275
276 trait MutPointerReturnerOk {
277 // should not trigger lint
new() -> *mut Self where Self: Sized,278 fn new() -> *mut Self
279 where
280 Self: Sized,
281 {
282 unimplemented!();
283 }
284 }
285
286 trait ConstPointerReturnerOk2 {
287 // should not trigger lint
new() -> *const Self where Self: Sized,288 fn new() -> *const Self
289 where
290 Self: Sized,
291 {
292 unimplemented!();
293 }
294 }
295
296 trait MutPointerReturnerBad {
297 // should trigger lint
new() -> *mut V298 fn new() -> *mut V {
299 unimplemented!();
300 }
301 }
302
303 trait GenericReturnerOk {
304 // should not trigger lint
new() -> Option<Self> where Self: Sized,305 fn new() -> Option<Self>
306 where
307 Self: Sized,
308 {
309 unimplemented!();
310 }
311 }
312
313 trait NestedReturnerOk {
314 // should not trigger lint
new() -> (Option<Self>, u32) where Self: Sized,315 fn new() -> (Option<Self>, u32)
316 where
317 Self: Sized,
318 {
319 unimplemented!();
320 }
321 }
322
323 trait NestedReturnerOk2 {
324 // should not trigger lint
new() -> ((Self, u32), u32) where Self: Sized,325 fn new() -> ((Self, u32), u32)
326 where
327 Self: Sized,
328 {
329 unimplemented!();
330 }
331 }
332
333 trait NestedReturnerOk3 {
334 // should not trigger lint
new() -> Option<(Self, u32)> where Self: Sized,335 fn new() -> Option<(Self, u32)>
336 where
337 Self: Sized,
338 {
339 unimplemented!();
340 }
341 }
342 }
343
344 // issue #1724
345 struct RetOtherSelf<T>(T);
346 struct RetOtherSelfWrapper<T>(T);
347
348 impl RetOtherSelf<T> {
new(t: T) -> RetOtherSelf<RetOtherSelfWrapper<T>>349 fn new(t: T) -> RetOtherSelf<RetOtherSelfWrapper<T>> {
350 RetOtherSelf(RetOtherSelfWrapper(t))
351 }
352 }
353