Skip to content

Commit

Permalink
Add suggestion to use raw identifiers when fixing snake-case lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Skynoodle committed Jan 1, 2021
1 parent b919aa4 commit 91f436b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions compiler/rustc_lint/src/nonstandard_style.rs
Expand Up @@ -275,10 +275,24 @@ impl NonSnakeCase {
// We have a valid span in almost all cases, but we don't have one when linting a crate
// name provided via the command line.
if !ident.span.is_dummy() {
let sc_ident = Ident::from_str_and_span(&sc, ident.span);
let (message, suggestion) = if sc_ident.is_reserved() {
// We shouldn't suggest a reserved identifier to fix non-snake-case identifiers.
// Instead, recommend renaming the identifier entirely or, if permitted,
// escaping it to create a raw identifier.
if sc_ident.name.can_be_raw() {
("rename the identifier or convert it to a snake case raw identifier", sc_ident.to_string())
} else {
("rename the identifier", String::new())
}
} else {
("convert the identifier to snake case", sc)
};

err.span_suggestion(
ident.span,
"convert the identifier to snake case",
sc,
message,
suggestion,
Applicability::MaybeIncorrect,
);
} else {
Expand Down

0 comments on commit 91f436b

Please sign in to comment.