]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/new_without_default.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / new_without_default.rs
CommitLineData
f20569fa
XL
1#![allow(dead_code, clippy::missing_safety_doc)]
2#![warn(clippy::new_without_default)]
3
4pub struct Foo;
5
6impl Foo {
7 pub fn new() -> Foo {
8 Foo
9 }
10}
11
12pub struct Bar;
13
14impl Bar {
15 pub fn new() -> Self {
16 Bar
17 }
18}
19
20pub struct Ok;
21
22impl Ok {
23 pub fn new() -> Self {
24 Ok
25 }
26}
27
28impl Default for Ok {
29 fn default() -> Self {
30 Ok
31 }
32}
33
34pub struct Params;
35
36impl Params {
37 pub fn new(_: u32) -> Self {
38 Params
39 }
40}
41
42pub struct GenericsOk<T> {
43 bar: T,
44}
45
46impl<U> Default for GenericsOk<U> {
47 fn default() -> Self {
48 unimplemented!();
49 }
50}
51
52impl<'c, V> GenericsOk<V> {
53 pub fn new() -> GenericsOk<V> {
54 unimplemented!()
55 }
56}
57
58pub struct LtOk<'a> {
59 foo: &'a bool,
60}
61
62impl<'b> Default for LtOk<'b> {
63 fn default() -> Self {
64 unimplemented!();
65 }
66}
67
68impl<'c> LtOk<'c> {
69 pub fn new() -> LtOk<'c> {
70 unimplemented!()
71 }
72}
73
74pub struct LtKo<'a> {
75 foo: &'a bool,
76}
77
78impl<'c> LtKo<'c> {
79 pub fn new() -> LtKo<'c> {
80 unimplemented!()
81 }
82 // FIXME: that suggestion is missing lifetimes
83}
84
85struct Private;
86
87impl Private {
88 fn new() -> Private {
89 unimplemented!()
90 } // We don't lint private items
91}
92
93struct Const;
94
95impl Const {
96 pub const fn new() -> Const {
97 Const
98 } // const fns can't be implemented via Default
99}
100
101pub struct IgnoreGenericNew;
102
103impl 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
109pub trait TraitWithNew: Sized {
110 fn new() -> Self {
111 panic!()
112 }
113}
114
115pub struct IgnoreUnsafeNew;
116
117impl IgnoreUnsafeNew {
118 pub unsafe fn new() -> Self {
119 IgnoreUnsafeNew
120 }
121}
122
123#[derive(Default)]
124pub struct OptionRefWrapper<'a, T>(Option<&'a T>);
125
126impl<'a, T> OptionRefWrapper<'a, T> {
127 pub fn new() -> Self {
128 OptionRefWrapper(None)
129 }
130}
131
132pub struct Allow(Foo);
133
134impl Allow {
135 #[allow(clippy::new_without_default)]
136 pub fn new() -> Self {
137 unimplemented!()
138 }
139}
140
141pub struct AllowDerive;
142
143impl AllowDerive {
144 #[allow(clippy::new_without_default)]
145 pub fn new() -> Self {
146 unimplemented!()
147 }
148}
149
150pub struct NewNotEqualToDerive {
151 foo: i32,
152}
153
154impl 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
cdc7bbd5
XL
161// see #6933
162pub struct FooGenerics<T>(std::marker::PhantomData<T>);
163impl<T> FooGenerics<T> {
164 pub fn new() -> Self {
165 Self(Default::default())
166 }
167}
168
169pub struct BarGenerics<T>(std::marker::PhantomData<T>);
170impl<T: Copy> BarGenerics<T> {
171 pub fn new() -> Self {
172 Self(Default::default())
173 }
174}
175
f20569fa 176fn main() {}