From 91f436b456130561d514c5e7050486cb0768996f Mon Sep 17 00:00:00 2001 From: Skynoodle Date: Fri, 1 Jan 2021 18:34:20 +0000 Subject: [PATCH] Add suggestion to use raw identifiers when fixing snake-case lints --- compiler/rustc_lint/src/nonstandard_style.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 6d61b86f32e..39cebe7f969 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -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 {