Skip to content

Commit ea04951

Browse files
committed
parser: suggest quotes only for ident attribute values
When encountering code like: ``` #[cfg(key = ["foo", "bar"])] fn main() {} ``` the parser incorrectly suggests: ``` error: expected unsuffixed literal, found `[` --> ../pg/src/main.rs:1:13 | 1 | #[cfg(key = ["foo", "bar"])] | ^ | help: surround the identifier with quotation marks to parse it as a string | 1 | #[cfg(key =" "["foo", "bar"])] | + + error: aborting due to 1 previous error ``` This commit modifies the parser to check if an attribute value is an ident before suggesting.
1 parent fcc06c8 commit ea04951

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

compiler/rustc_parse/src/parser/attr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ impl<'a> Parser<'a> {
412412
// Check for unquoted idents in meta items, e.g.: #[cfg(key = foo)]
413413
// `from_expansion()` ensures we don't suggest for cases such as
414414
// `#[cfg(feature = $expr)]` in macros
415-
if self.prev_token == token::Eq && !self.token.span.from_expansion() {
415+
if self.token.is_ident()
416+
&& self.prev_token == token::Eq
417+
&& !self.token.span.from_expansion()
418+
{
416419
let before = self.token.span.shrink_to_lo();
417420
while matches!(self.token.kind, token::Ident(..)) {
418421
self.bump();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Ensures to suggest quoting attribute values only when they are identifiers.
2+
3+
#[doc(alias = "val")]
4+
#[doc(alias = val)] //~ ERROR expected unsuffixed literal, found `val`
5+
#[doc(alias = ["va", "al"])] //~ ERROR expected unsuffixed literal or identifier, found `[`
6+
#[doc(alias = &["va", "al"])] //~ ERROR expected unsuffixed literal or identifier, found `&`
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected unsuffixed literal, found `val`
2+
--> $DIR/suggest_quoting_values_only_for_idents.rs:4:15
3+
|
4+
LL | #[doc(alias = val)]
5+
| ^^^
6+
|
7+
help: surround the identifier with quotation marks to parse it as a string
8+
|
9+
LL | #[doc(alias = "val")]
10+
| + +
11+
12+
error: expected unsuffixed literal or identifier, found `[`
13+
--> $DIR/suggest_quoting_values_only_for_idents.rs:5:15
14+
|
15+
LL | #[doc(alias = ["va", "al"])]
16+
| ^
17+
18+
error: expected unsuffixed literal or identifier, found `&`
19+
--> $DIR/suggest_quoting_values_only_for_idents.rs:6:15
20+
|
21+
LL | #[doc(alias = &["va", "al"])]
22+
| ^
23+
24+
error: aborting due to 3 previous errors
25+

0 commit comments

Comments
 (0)