Skip to content

Commit f14d007

Browse files
committed
Add suggestion for incorrect field syntax.
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.
1 parent 6bba352 commit f14d007

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
@@ -2263,8 +2263,24 @@ impl<'a> Parser<'a> {
22632263
let lo = self.span;
22642264

22652265
// Check if a colon exists one ahead. This means we're parsing a fieldname.
2266-
let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
2266+
let (fieldname, expr, is_shorthand) = if self.look_ahead(1, |t| {
2267+
t == &token::Colon || t == &token::Eq
2268+
}) {
22672269
let fieldname = self.parse_field_name()?;
2270+
2271+
// Check for an equals token. This means the source incorrectly attempts to
2272+
// initialize a field with an eq rather than a colon.
2273+
if self.token == token::Eq {
2274+
self.diagnostic()
2275+
.struct_span_err(self.span, "expected `:`, found `=`")
2276+
.span_suggestion_with_applicability(
2277+
fieldname.span.shrink_to_hi().to(self.span),
2278+
"replace equals symbol with a colon",
2279+
":".to_string(),
2280+
Applicability::MachineApplicable,
2281+
)
2282+
.emit();
2283+
}
22682284
self.bump(); // `:`
22692285
(fieldname, self.parse_expr()?, false)
22702286
} 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)