fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
5.4 KiB309 linesraw
1#![allow(
2    dead_code,
3    clippy::missing_safety_doc,
4    clippy::extra_unused_lifetimes,
5    clippy::extra_unused_type_parameters
6)]
7#![warn(clippy::new_without_default)]
8
9pub struct Foo;
10
11impl Default for Foo {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl Foo {
18    pub fn new() -> Foo {
19        //~^ ERROR: you should consider adding a `Default` implementation for `Foo`
20        //~| NOTE: `-D clippy::new-without-default` implied by `-D warnings`
21        Foo
22    }
23}
24
25pub struct Bar;
26
27impl Default for Bar {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl Bar {
34    pub fn new() -> Self {
35        //~^ ERROR: you should consider adding a `Default` implementation for `Bar`
36        Bar
37    }
38}
39
40pub struct Ok;
41
42impl Ok {
43    pub fn new() -> Self {
44        Ok
45    }
46}
47
48impl Default for Ok {
49    fn default() -> Self {
50        Ok
51    }
52}
53
54pub struct Params;
55
56impl Params {
57    pub fn new(_: u32) -> Self {
58        Params
59    }
60}
61
62pub struct GenericsOk<T> {
63    bar: T,
64}
65
66impl<U> Default for GenericsOk<U> {
67    fn default() -> Self {
68        unimplemented!();
69    }
70}
71
72impl<'c, V> GenericsOk<V> {
73    pub fn new() -> GenericsOk<V> {
74        unimplemented!()
75    }
76}
77
78pub struct LtOk<'a> {
79    foo: &'a bool,
80}
81
82impl<'b> Default for LtOk<'b> {
83    fn default() -> Self {
84        unimplemented!();
85    }
86}
87
88impl<'c> LtOk<'c> {
89    pub fn new() -> LtOk<'c> {
90        unimplemented!()
91    }
92}
93
94pub struct LtKo<'a> {
95    foo: &'a bool,
96}
97
98impl<'c> Default for LtKo<'c> {
99    fn default() -> Self {
100        Self::new()
101    }
102}
103
104impl<'c> LtKo<'c> {
105    pub fn new() -> LtKo<'c> {
106        //~^ ERROR: you should consider adding a `Default` implementation for `LtKo<'c>`
107        unimplemented!()
108    }
109}
110
111struct Private;
112
113impl Private {
114    fn new() -> Private {
115        unimplemented!()
116    } // We don't lint private items
117}
118
119struct PrivateStruct;
120
121impl PrivateStruct {
122    pub fn new() -> PrivateStruct {
123        unimplemented!()
124    } // We don't lint public items on private structs
125}
126
127pub struct PrivateItem;
128
129impl PrivateItem {
130    fn new() -> PrivateItem {
131        unimplemented!()
132    } // We don't lint private items on public structs
133}
134
135struct Const;
136
137impl Const {
138    pub const fn new() -> Const {
139        Const
140    } // const fns can't be implemented via Default
141}
142
143pub struct IgnoreGenericNew;
144
145impl IgnoreGenericNew {
146    pub fn new<T>() -> Self {
147        IgnoreGenericNew
148    } // the derived Default does not make sense here as the result depends on T
149}
150
151pub trait TraitWithNew: Sized {
152    fn new() -> Self {
153        panic!()
154    }
155}
156
157pub struct IgnoreUnsafeNew;
158
159impl IgnoreUnsafeNew {
160    pub unsafe fn new() -> Self {
161        IgnoreUnsafeNew
162    }
163}
164
165#[derive(Default)]
166pub struct OptionRefWrapper<'a, T>(Option<&'a T>);
167
168impl<'a, T> OptionRefWrapper<'a, T> {
169    pub fn new() -> Self {
170        OptionRefWrapper(None)
171    }
172}
173
174pub struct Allow(Foo);
175
176impl Allow {
177    #[allow(clippy::new_without_default)]
178    pub fn new() -> Self {
179        unimplemented!()
180    }
181}
182
183pub struct AllowDerive;
184
185impl AllowDerive {
186    #[allow(clippy::new_without_default)]
187    pub fn new() -> Self {
188        unimplemented!()
189    }
190}
191
192pub struct NewNotEqualToDerive {
193    foo: i32,
194}
195
196impl Default for NewNotEqualToDerive {
197    fn default() -> Self {
198        Self::new()
199    }
200}
201
202impl NewNotEqualToDerive {
203    // This `new` implementation is not equal to a derived `Default`, so do not suggest deriving.
204    pub fn new() -> Self {
205        //~^ ERROR: you should consider adding a `Default` implementation for `NewNotEqualToDe
206        NewNotEqualToDerive { foo: 1 }
207    }
208}
209
210// see #6933
211pub struct FooGenerics<T>(std::marker::PhantomData<T>);
212impl<T> Default for FooGenerics<T> {
213    fn default() -> Self {
214        Self::new()
215    }
216}
217
218impl<T> FooGenerics<T> {
219    pub fn new() -> Self {
220        //~^ ERROR: you should consider adding a `Default` implementation for `FooGenerics<T>`
221        Self(Default::default())
222    }
223}
224
225pub struct BarGenerics<T>(std::marker::PhantomData<T>);
226impl<T: Copy> Default for BarGenerics<T> {
227    fn default() -> Self {
228        Self::new()
229    }
230}
231
232impl<T: Copy> BarGenerics<T> {
233    pub fn new() -> Self {
234        //~^ ERROR: you should consider adding a `Default` implementation for `BarGenerics<T>`
235        Self(Default::default())
236    }
237}
238
239pub mod issue7220 {
240    pub struct Foo<T> {
241        _bar: *mut T,
242    }
243
244    impl<T> Default for Foo<T> {
245        fn default() -> Self {
246            Self::new()
247        }
248    }
249
250    impl<T> Foo<T> {
251        pub fn new() -> Self {
252            //~^ ERROR: you should consider adding a `Default` implementation for `Foo<T>`
253            todo!()
254        }
255    }
256}
257
258// see issue #8152
259// This should not create any lints
260pub struct DocHidden;
261impl DocHidden {
262    #[doc(hidden)]
263    pub fn new() -> Self {
264        DocHidden
265    }
266}
267
268fn main() {}
269
270pub struct IgnoreConstGenericNew(usize);
271impl IgnoreConstGenericNew {
272    pub fn new<const N: usize>() -> Self {
273        Self(N)
274    }
275}
276
277pub struct IgnoreLifetimeNew;
278impl IgnoreLifetimeNew {
279    pub fn new<'a>() -> Self {
280        Self
281    }
282}
283
284// From issue #11267
285
286pub struct MyStruct<K, V>
287where
288    K: std::hash::Hash + Eq + PartialEq,
289{
290    _kv: Option<(K, V)>,
291}
292
293impl<K, V> Default for MyStruct<K, V>
294where
295    K: std::hash::Hash + Eq + PartialEq,
296 {
297    fn default() -> Self {
298        Self::new()
299    }
300}
301
302impl<K, V> MyStruct<K, V>
303where
304    K: std::hash::Hash + Eq + PartialEq,
305{
306    pub fn new() -> Self {
307        Self { _kv: None }
308    }
309}