Skip to content

Commit 8470dad

Browse files
authored
Rollup merge of rust-lang#57863 - davidtwco:issue-57684, r=estebank
Add suggestion for incorrect field syntax. Fixes rust-lang#57684. This commit adds a suggestion when a `=` character is used when specifying the value of a field in a struct constructor incorrectly instead of a `:` character. r? @estebank
2 parents b5628bb + f14d007 commit 8470dad

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

src/libsyntax/parse/parser.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -2322,8 +2322,24 @@ impl<'a> Parser<'a> {
23222322
let lo = self.span;
23232323

23242324
// Check if a colon exists one ahead. This means we're parsing a fieldname.
2325-
let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
2325+
let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| {
2326+
t == &token::Colon || t == &token::Eq
2327+
}) {
23262328
let fieldname = self.parse_field_name()?;
2329+
2330+
// Check for an equals token. This means the source incorrectly attempts to
2331+
// initialize a field with an eq rather than a colon.
2332+
if self.token == token::Eq {
2333+
self.diagnostic()
2334+
.struct_span_err(self.span, "expected `:`, found `=`")
2335+
.span_suggestion_with_applicability(
2336+
fieldname.span.shrink_to_hi().to(self.span),
2337+
"replace equals symbol with a colon",
2338+
":".to_string(),
2339+
Applicability::MachineApplicable,
2340+
)
2341+
.emit();
2342+
}
23272343
self.bump(); // `:`
23282344
(fieldname, self.parse_expr()?, false)
23292345
} else {

src/test/ui/issues/issue-57684.fixed

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run-rustfix
2+
3+
#![allow(warnings)]
4+
5+
// This test checks that the following error is emitted when a `=` character is used to initialize
6+
// a struct field when a `:` is expected.
7+
//
8+
// ```
9+
// error: struct fields are initialized with a colon
10+
// --> $DIR/issue-57684.rs:12:20
11+
// |
12+
// LL | let _ = X { f1 = 5 };
13+
// | ^ help: replace equals symbol with a colon: `:`
14+
// ```
15+
16+
struct X {
17+
f1: i32,
18+
}
19+
20+
struct Y {
21+
f1: i32,
22+
f2: i32,
23+
f3: i32,
24+
}
25+
26+
fn main() {
27+
let _ = X { f1: 5 };
28+
//~^ ERROR expected `:`, found `=`
29+
30+
let f3 = 3;
31+
let _ = Y {
32+
f1: 5,
33+
//~^ ERROR expected `:`, found `=`
34+
f2: 4,
35+
f3,
36+
};
37+
}

src/test/ui/issues/issue-57684.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run-rustfix
2+
3+
#![allow(warnings)]
4+
5+
// This test checks that the following error is emitted when a `=` character is used to initialize
6+
// a struct field when a `:` is expected.
7+
//
8+
// ```
9+
// error: struct fields are initialized with a colon
10+
// --> $DIR/issue-57684.rs:12:20
11+
// |
12+
// LL | let _ = X { f1 = 5 };
13+
// | ^ help: replace equals symbol with a colon: `:`
14+
// ```
15+
16+
struct X {
17+
f1: i32,
18+
}
19+
20+
struct Y {
21+
f1: i32,
22+
f2: i32,
23+
f3: i32,
24+
}
25+
26+
fn main() {
27+
let _ = X { f1 = 5 };
28+
//~^ ERROR expected `:`, found `=`
29+
30+
let f3 = 3;
31+
let _ = Y {
32+
f1 = 5,
33+
//~^ ERROR expected `:`, found `=`
34+
f2: 4,
35+
f3,
36+
};
37+
}

src/test/ui/issues/issue-57684.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: expected `:`, found `=`
2+
--> $DIR/issue-57684.rs:27:20
3+
|
4+
LL | let _ = X { f1 = 5 };
5+
| -^
6+
| |
7+
| help: replace equals symbol with a colon: `:`
8+
9+
error: expected `:`, found `=`
10+
--> $DIR/issue-57684.rs:32:12
11+
|
12+
LL | f1 = 5,
13+
| -^
14+
| |
15+
| help: replace equals symbol with a colon: `:`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)