about summary refs log tree commit diff
path: root/tests/ui/structs/manual-default-impl-could-be-derived.rs
blob: 773b738998862297cd486b2c0ed2c6c83f6c3029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Warn when we encounter a manual `Default` impl that could be derived.
// Restricted only to types using `default_field_values`.
#![feature(default_field_values)]
#![allow(dead_code)]
#![deny(default_overrides_default_fields)]
struct S(i32);
fn s() -> S { S(1) }

struct A {
    x: S,
    y: i32 = 1,
}

impl Default for A { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        A {
            y: 0,
            x: s(),
        }
    }
}

struct B {
    x: S = S(3),
    y: i32 = 1,
}

impl Default for B { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        B {
            x: s(),
            y: 0,
        }
    }
}

struct C {
    x: S,
    y: i32 = 1,
    z: i32 = 1,
}

impl Default for C { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        C {
            x: s(),
            y: 0,
            ..
        }
    }
}

struct D {
    x: S,
    y: i32 = 1,
    z: i32 = 1,
}

impl Default for D { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        D {
            y: 0,
            x: s(),
            ..
        }
    }
}

struct E {
    x: S,
    y: i32 = 1,
    z: i32 = 1,
}

impl Default for E { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        E {
            y: 0,
            z: 0,
            x: s(),
        }
    }
}

// Let's ensure that the span for `x` and the span for `y` don't overlap when suggesting their
// removal in favor of their default field values.
struct E2 {
    x: S,
    y: i32 = 1,
    z: i32 = 1,
}

impl Default for E2 { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        E2 {
            x: s(),
            y: i(),
            z: 0,
        }
    }
}

fn i() -> i32 {
    1
}

// Account for a `const fn` being the `Default::default()` of a field's type.
struct F {
    x: G,
    y: i32 = 1,
}

impl Default for F { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        F {
            x: g_const(),
            y: 0,
        }
    }
}

struct G;

impl Default for G { // ok
    fn default() -> Self {
        g_const()
    }
}

const fn g_const() -> G {
    G
}

// Account for a `const fn` being used in `Default::default()`, even if the type doesn't use it as
// its own `Default`. We suggest setting the default field value in that case.
struct H {
    x: I,
    y: i32 = 1,
}

impl Default for H { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        H {
            x: i_const(),
            y: 0,
        }
    }
}

struct I;

const fn i_const() -> I {
    I
}

// Account for a `const` and struct literal being the `Default::default()` of a field's type.
struct M {
    x: N,
    y: i32 = 1,
    z: A,
}

impl Default for M { // ok, `y` is not specified
    fn default() -> Self {
        M {
            x: N_CONST,
            z: A {
                x: S(0),
                y: 0,
            },
            ..
        }
    }
}

struct N;

const N_CONST: N = N;

struct O {
    x: Option<i32>,
    y: i32 = 1,
}

impl Default for O { //~ ERROR default_overrides_default_fields
    fn default() -> Self {
        O {
            x: None,
            y: 1,
        }
    }
}

fn main() {}