]> git.lizzy.rs Git - rust.git/blob - tests/ui/new_without_default.rs
Auto merge of #85109 - RalfJung:remove-const_fn, r=oli-obk
[rust.git] / tests / ui / new_without_default.rs
1 #![allow(dead_code, clippy::missing_safety_doc)]
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 Const;
94
95 impl Const {
96     pub const fn new() -> Const {
97         Const
98     } // const fns can't be implemented via Default
99 }
100
101 pub struct IgnoreGenericNew;
102
103 impl IgnoreGenericNew {
104     pub fn new<T>() -> Self {
105         IgnoreGenericNew
106     } // the derived Default does not make sense here as the result depends on T
107 }
108
109 pub trait TraitWithNew: Sized {
110     fn new() -> Self {
111         panic!()
112     }
113 }
114
115 pub struct IgnoreUnsafeNew;
116
117 impl IgnoreUnsafeNew {
118     pub unsafe fn new() -> Self {
119         IgnoreUnsafeNew
120     }
121 }
122
123 #[derive(Default)]
124 pub struct OptionRefWrapper<'a, T>(Option<&'a T>);
125
126 impl<'a, T> OptionRefWrapper<'a, T> {
127     pub fn new() -> Self {
128         OptionRefWrapper(None)
129     }
130 }
131
132 pub struct Allow(Foo);
133
134 impl Allow {
135     #[allow(clippy::new_without_default)]
136     pub fn new() -> Self {
137         unimplemented!()
138     }
139 }
140
141 pub struct AllowDerive;
142
143 impl AllowDerive {
144     #[allow(clippy::new_without_default)]
145     pub fn new() -> Self {
146         unimplemented!()
147     }
148 }
149
150 pub struct NewNotEqualToDerive {
151     foo: i32,
152 }
153
154 impl NewNotEqualToDerive {
155     // This `new` implementation is not equal to a derived `Default`, so do not suggest deriving.
156     pub fn new() -> Self {
157         NewNotEqualToDerive { foo: 1 }
158     }
159 }
160
161 // see #6933
162 pub struct FooGenerics<T>(std::marker::PhantomData<T>);
163 impl<T> FooGenerics<T> {
164     pub fn new() -> Self {
165         Self(Default::default())
166     }
167 }
168
169 pub struct BarGenerics<T>(std::marker::PhantomData<T>);
170 impl<T: Copy> BarGenerics<T> {
171     pub fn new() -> Self {
172         Self(Default::default())
173     }
174 }
175
176 fn main() {}