Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: suggest quotes only for ident attribute values #124512

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ impl<'a> Parser<'a> {
// Check for unquoted idents in meta items, e.g.: #[cfg(key = foo)]
// `from_expansion()` ensures we don't suggest for cases such as
// `#[cfg(feature = $expr)]` in macros
if self.prev_token == token::Eq && !self.token.span.from_expansion() {
if self.token.is_ident()
&& self.prev_token == token::Eq
&& !self.token.span.from_expansion()
{
let before = self.token.span.shrink_to_lo();
while matches!(self.token.kind, token::Ident(..)) {
self.bump();
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/attributes/suggest_quoting_values_only_for_idents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Ensures to suggest quoting attribute values only when they are identifiers.

#[doc(alias = "val")]
#[doc(alias = val)] //~ ERROR expected unsuffixed literal, found `val`
#[doc(alias = ["va", "al"])] //~ ERROR expected unsuffixed literal or identifier, found `[`
#[doc(alias = &["va", "al"])] //~ ERROR expected unsuffixed literal or identifier, found `&`
fn main() {}
25 changes: 25 additions & 0 deletions tests/ui/attributes/suggest_quoting_values_only_for_idents.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: expected unsuffixed literal, found `val`
--> $DIR/suggest_quoting_values_only_for_idents.rs:4:15
|
LL | #[doc(alias = val)]
| ^^^
|
help: surround the identifier with quotation marks to parse it as a string
|
LL | #[doc(alias = "val")]
| + +

error: expected unsuffixed literal or identifier, found `[`
--> $DIR/suggest_quoting_values_only_for_idents.rs:5:15
|
LL | #[doc(alias = ["va", "al"])]
| ^

error: expected unsuffixed literal or identifier, found `&`
--> $DIR/suggest_quoting_values_only_for_idents.rs:6:15
|
LL | #[doc(alias = &["va", "al"])]
| ^

error: aborting due to 3 previous errors

Loading