Suggest quoting unquoted idents in attrs
This commit is contained in:
parent
6029085a6f
commit
aa8ecd0652
8 changed files with 107 additions and 6 deletions
|
@ -973,6 +973,25 @@ pub(crate) struct InvalidMetaItem {
|
|||
pub token: Token,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parse_invalid_meta_item_unquoted_ident)]
|
||||
pub(crate) struct InvalidMetaItemUnquotedIdent {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
pub token: Token,
|
||||
#[subdiagnostic]
|
||||
pub sugg: InvalidMetaItemSuggQuoteIdent,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
#[multipart_suggestion(parse_suggestion, applicability = "machine-applicable")]
|
||||
pub(crate) struct InvalidMetaItemSuggQuoteIdent {
|
||||
#[suggestion_part(code = "\"")]
|
||||
pub before: Span,
|
||||
#[suggestion_part(code = "\"")]
|
||||
pub after: Span,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
#[suggestion(
|
||||
parse_sugg_escape_identifier,
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
|
||||
use crate::errors::{
|
||||
InvalidMetaItem, InvalidMetaItemSuggQuoteIdent, InvalidMetaItemUnquotedIdent,
|
||||
SuffixedLiteralInAttribute,
|
||||
};
|
||||
use crate::fluent_generated as fluent;
|
||||
|
||||
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
|
||||
|
@ -417,9 +420,26 @@ impl<'a> Parser<'a> {
|
|||
Err(err) => err.cancel(),
|
||||
}
|
||||
|
||||
Err(self
|
||||
.dcx()
|
||||
.create_err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }))
|
||||
let token = self.token.clone();
|
||||
|
||||
// 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() {
|
||||
let before = self.token.span.shrink_to_lo();
|
||||
while matches!(self.token.kind, token::Ident(..)) {
|
||||
self.bump();
|
||||
}
|
||||
let after = self.prev_token.span.shrink_to_hi();
|
||||
let sugg = InvalidMetaItemSuggQuoteIdent { before, after };
|
||||
return Err(self.dcx().create_err(InvalidMetaItemUnquotedIdent {
|
||||
span: token.span,
|
||||
token,
|
||||
sugg,
|
||||
}));
|
||||
}
|
||||
|
||||
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, token }))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue