-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect missing fields with default values and suggest
..
When a struct definition has default field values, and the use struct ctor has missing field, if all those missing fields have defaults suggest `..`: ``` error[E0063]: missing fields `field1` and `field2` in initializer of `S` --> $DIR/non-exhaustive-ctor.rs:16:13 | LL | let _ = S { field: () }; | ^ missing `field1` and `field2` | help: all remaining fields have defaults, use `..` | LL | let _ = S { field: (), .. }; | ++++ ```
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/ui/structs/default-field-values/non-exhaustive-ctor.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![feature(default_field_values)] | ||
use m::S; | ||
|
||
mod m { | ||
pub struct S { | ||
pub field: () = (), | ||
pub field1: Priv = Priv, | ||
pub field2: Priv = Priv, | ||
} | ||
struct Priv; | ||
} | ||
|
||
fn main() { | ||
let _ = S { .. }; // ok | ||
let _ = S { field: (), .. }; // ok | ||
let _ = S { }; | ||
//~^ ERROR missing fields `field`, `field1` and `field2` | ||
let _ = S { field: () }; | ||
//~^ ERROR missing fields `field1` and `field2` | ||
let _ = S { field: (), field1: m::Priv }; | ||
//~^ ERROR missing field `field2` | ||
//~| ERROR unit struct `Priv` is private | ||
let _ = S { field: (), field1: m::Priv, field2: m::Priv }; | ||
//~^ ERROR unit struct `Priv` is private | ||
//~| ERROR unit struct `Priv` is private | ||
} |
73 changes: 73 additions & 0 deletions
73
tests/ui/structs/default-field-values/non-exhaustive-ctor.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
error[E0603]: unit struct `Priv` is private | ||
--> $DIR/non-exhaustive-ctor.rs:20:39 | ||
| | ||
LL | let _ = S { field: (), field1: m::Priv }; | ||
| ^^^^ private unit struct | ||
| | ||
note: the unit struct `Priv` is defined here | ||
--> $DIR/non-exhaustive-ctor.rs:10:5 | ||
| | ||
LL | struct Priv; | ||
| ^^^^^^^^^^^^ | ||
|
||
error[E0603]: unit struct `Priv` is private | ||
--> $DIR/non-exhaustive-ctor.rs:23:39 | ||
| | ||
LL | let _ = S { field: (), field1: m::Priv, field2: m::Priv }; | ||
| ^^^^ private unit struct | ||
| | ||
note: the unit struct `Priv` is defined here | ||
--> $DIR/non-exhaustive-ctor.rs:10:5 | ||
| | ||
LL | struct Priv; | ||
| ^^^^^^^^^^^^ | ||
|
||
error[E0603]: unit struct `Priv` is private | ||
--> $DIR/non-exhaustive-ctor.rs:23:56 | ||
| | ||
LL | let _ = S { field: (), field1: m::Priv, field2: m::Priv }; | ||
| ^^^^ private unit struct | ||
| | ||
note: the unit struct `Priv` is defined here | ||
--> $DIR/non-exhaustive-ctor.rs:10:5 | ||
| | ||
LL | struct Priv; | ||
| ^^^^^^^^^^^^ | ||
|
||
error[E0063]: missing fields `field`, `field1` and `field2` in initializer of `S` | ||
--> $DIR/non-exhaustive-ctor.rs:16:13 | ||
| | ||
LL | let _ = S { }; | ||
| ^ missing `field`, `field1` and `field2` | ||
| | ||
help: all remaining fields have default values, you can use those values with `..` | ||
| | ||
LL | let _ = S { .. }; | ||
| ~~~~~~ | ||
|
||
error[E0063]: missing fields `field1` and `field2` in initializer of `S` | ||
--> $DIR/non-exhaustive-ctor.rs:18:13 | ||
| | ||
LL | let _ = S { field: () }; | ||
| ^ missing `field1` and `field2` | ||
| | ||
help: all remaining fields have default values, you can use those values with `..` | ||
| | ||
LL | let _ = S { field: (), .. }; | ||
| ++++ | ||
|
||
error[E0063]: missing field `field2` in initializer of `S` | ||
--> $DIR/non-exhaustive-ctor.rs:20:13 | ||
| | ||
LL | let _ = S { field: (), field1: m::Priv }; | ||
| ^ missing `field2` | ||
| | ||
help: all remaining fields have default values, you can use those values with `..` | ||
| | ||
LL | let _ = S { field: (), field1: m::Priv, .. }; | ||
| ++++ | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0063, E0603. | ||
For more information about an error, try `rustc --explain E0063`. |