]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/new_without_default.rs
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / clippy / tests / ui / new_without_default.rs
1 #![allow(dead_code, clippy::missing_safety_doc, clippy::extra_unused_lifetimes)]
2 #![warn(clippy::new_without_default)]
3
4 pub struct Foo;
5
6 impl Foo {
7     pub fn new() -> Foo {
8         Foo
9     }
10 }
11
12 pub struct Bar;
13
14 impl Bar {
15     pub fn new() -> Self {
16         Bar
17     }
18 }
19
20 pub struct Ok;
21
22 impl Ok {
23     pub fn new() -> Self {
24         Ok
25     }
26 }
27
28 impl Default for Ok {
29     fn default() -> Self {
30         Ok
31     }
32 }
33
34 pub struct Params;
35
36 impl Params {
37     pub fn new(_: u32) -> Self {
38         Params
39     }
40 }
41
42 pub struct GenericsOk<T> {
43     bar: T,
44 }
45
46 impl<U> Default for GenericsOk<U> {
47     fn default() -> Self {
48         unimplemented!();
49     }
50 }
51
52 impl<'c, V> GenericsOk<V> {
53     pub fn new() -> GenericsOk<V> {
54         unimplemented!()
55     }
56 }
57
58 pub struct LtOk<'a> {
59     foo: &'a bool,
60 }
61
62 impl<'b> Default for LtOk<'b> {
63     fn default() -> Self {
64         unimplemented!();
65     }
66 }
67
68 impl<'c> LtOk<'c> {
69     pub fn new() -> LtOk<'c> {
70         unimplemented!()
71     }
72 }
73
74 pub struct LtKo<'a> {
75     foo: &'a bool,
76 }
77
78 impl<'c> LtKo<'c> {
79     pub fn new() -> LtKo<'c> {
80         unimplemented!()
81     }
82     // FIXME: that suggestion is missing lifetimes
83 }
84
85 struct Private;
86
87 impl Private {
88     fn new() -> Private {
89         unimplemented!()
90     } // We don't lint private items
91 }
92
93 struct PrivateStruct;
94
95 impl PrivateStruct {
96     pub fn new() -> PrivateStruct {
97         unimplemented!()
98     } // We don't lint public items on private structs
99 }
100
101 pub struct PrivateItem;
102
103 impl PrivateItem {
104     fn new() -> PrivateItem {
105         unimplemented!()
106     } // We don't lint private items on public structs
107 }
108
109 struct Const;
110
111 impl Const {
112     pub const fn new() -> Const {
113         Const
114     } // const fns can't be implemented via Default
115 }
116
117 pub struct IgnoreGenericNew;
118
119 impl IgnoreGenericNew {
120     pub fn new<T>() -> Self {
121         IgnoreGenericNew
122     } // the derived Default does not make sense here as the result depends on T
123 }
124
125 pub trait TraitWithNew: Sized {
126     fn new() -> Self {
127         panic!()
128     }
129 }
130
131 pub struct IgnoreUnsafeNew;
132
133 impl IgnoreUnsafeNew {
134     pub unsafe fn new() -> Self {
135         IgnoreUnsafeNew
136     }
137 }
138
139 #[derive(Default)]
140 pub struct OptionRefWrapper<'a, T>(Option<&'a T>);
141
142 impl<'a, T> OptionRefWrapper<'a, T> {
143     pub fn new() -> Self {
144         OptionRefWrapper(None)
145     }
146 }
147
148 pub struct Allow(Foo);
149
150 impl Allow {
151     #[allow(clippy::new_without_default)]
152     pub fn new() -> Self {
153         unimplemented!()
154     }
155 }
156
157 pub struct AllowDerive;
158
159 impl AllowDerive {
160     #[allow(clippy::new_without_default)]
161     pub fn new() -> Self {
162         unimplemented!()
163     }
164 }
165
166 pub struct NewNotEqualToDerive {
167     foo: i32,
168 }
169
170 impl NewNotEqualToDerive {
171     // This `new` implementation is not equal to a derived `Default`, so do not suggest deriving.
172     pub fn new() -> Self {
173         NewNotEqualToDerive { foo: 1 }
174     }
175 }
176
177 // see #6933
178 pub struct FooGenerics<T>(std::marker::PhantomData<T>);
179 impl<T> FooGenerics<T> {
180     pub fn new() -> Self {
181         Self(Default::default())
182     }
183 }
184
185 pub struct BarGenerics<T>(std::marker::PhantomData<T>);
186 impl<T: Copy> BarGenerics<T> {
187     pub fn new() -> Self {
188         Self(Default::default())
189     }
190 }
191
192 pub mod issue7220 {
193     pub struct Foo<T> {
194         _bar: *mut T,
195     }
196
197     impl<T> Foo<T> {
198         pub fn new() -> Self {
199             todo!()
200         }
201     }
202 }
203
204 // see issue #8152
205 // This should not create any lints
206 pub struct DocHidden;
207 impl DocHidden {
208     #[doc(hidden)]
209     pub fn new() -> Self {
210         DocHidden
211     }
212 }
213
214 fn main() {}
215
216 pub struct IgnoreConstGenericNew(usize);
217 impl IgnoreConstGenericNew {
218     pub fn new<const N: usize>() -> Self {
219         Self(N)
220     }
221 }
222
223 pub struct IgnoreLifetimeNew;
224 impl IgnoreLifetimeNew {
225     pub fn new<'a>() -> Self {
226         Self
227     }
228 }