about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/structs/manual-default-impl-could-be-derived.rs194
-rw-r--r--tests/ui/structs/manual-default-impl-could-be-derived.stderr144
2 files changed, 338 insertions, 0 deletions
diff --git a/tests/ui/structs/manual-default-impl-could-be-derived.rs b/tests/ui/structs/manual-default-impl-could-be-derived.rs
new file mode 100644
index 00000000000..773b7389988
--- /dev/null
+++ b/tests/ui/structs/manual-default-impl-could-be-derived.rs
@@ -0,0 +1,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() {}
diff --git a/tests/ui/structs/manual-default-impl-could-be-derived.stderr b/tests/ui/structs/manual-default-impl-could-be-derived.stderr
new file mode 100644
index 00000000000..e8f607fac7e
--- /dev/null
+++ b/tests/ui/structs/manual-default-impl-could-be-derived.stderr
@@ -0,0 +1,144 @@
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:14:1
+   |
+LL | / impl Default for A {
+LL | |     fn default() -> Self {
+LL | |         A {
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+note: the lint level is defined here
+  --> $DIR/manual-default-impl-could-be-derived.rs:5:9
+   |
+LL | #![deny(default_overrides_default_fields)]
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:28:1
+   |
+LL | / impl Default for B {
+LL | |     fn default() -> Self {
+LL | |         B {
+LL | |             x: s(),
+   | |                --- this field has a default value
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: to avoid divergence in behavior between `Struct { .. }` and `<Struct as Default>::default()`, derive the `Default`
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:43:1
+   |
+LL | / impl Default for C {
+LL | |     fn default() -> Self {
+LL | |         C {
+LL | |             x: s(),
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:59:1
+   |
+LL | / impl Default for D {
+LL | |     fn default() -> Self {
+LL | |         D {
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:75:1
+   |
+LL | / impl Default for E {
+LL | |     fn default() -> Self {
+LL | |         E {
+LL | |             y: 0,
+   | |                - this field has a default value
+LL | |             z: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:93:1
+   |
+LL | / impl Default for E2 {
+LL | |     fn default() -> Self {
+LL | |         E2 {
+LL | |             x: s(),
+LL | |             y: i(),
+   | |                --- this field has a default value
+LL | |             z: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:113:1
+   |
+LL | / impl Default for F {
+LL | |     fn default() -> Self {
+LL | |         F {
+LL | |             x: g_const(),
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:141:1
+   |
+LL | / impl Default for H {
+LL | |     fn default() -> Self {
+LL | |         H {
+LL | |             x: i_const(),
+LL | |             y: 0,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: `Default` impl doesn't use the declared default field values
+  --> $DIR/manual-default-impl-could-be-derived.rs:185:1
+   |
+LL | / impl Default for O {
+LL | |     fn default() -> Self {
+LL | |         O {
+LL | |             x: None,
+LL | |             y: 1,
+   | |                - this field has a default value
+...  |
+LL | | }
+   | |_^
+   |
+   = help: use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them diverging over time
+
+error: aborting due to 9 previous errors
+