Skip to content

Commit 91f436b

Browse files
committed
Add suggestion to use raw identifiers when fixing snake-case lints
1 parent b919aa4 commit 91f436b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

compiler/rustc_lint/src/nonstandard_style.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,24 @@ impl NonSnakeCase {
275275
// We have a valid span in almost all cases, but we don't have one when linting a crate
276276
// name provided via the command line.
277277
if !ident.span.is_dummy() {
278+
let sc_ident = Ident::from_str_and_span(&sc, ident.span);
279+
let (message, suggestion) = if sc_ident.is_reserved() {
280+
// We shouldn't suggest a reserved identifier to fix non-snake-case identifiers.
281+
// Instead, recommend renaming the identifier entirely or, if permitted,
282+
// escaping it to create a raw identifier.
283+
if sc_ident.name.can_be_raw() {
284+
("rename the identifier or convert it to a snake case raw identifier", sc_ident.to_string())
285+
} else {
286+
("rename the identifier", String::new())
287+
}
288+
} else {
289+
("convert the identifier to snake case", sc)
290+
};
291+
278292
err.span_suggestion(
279293
ident.span,
280-
"convert the identifier to snake case",
281-
sc,
294+
message,
295+
suggestion,
282296
Applicability::MaybeIncorrect,
283297
);
284298
} else {

0 commit comments

Comments
 (0)