]> git.lizzy.rs Git - rust.git/commitdiff
Add suggestion to use raw identifiers when fixing snake-case lints
authorSkynoodle <iainrobert.maciver@gmail.com>
Fri, 1 Jan 2021 18:34:20 +0000 (18:34 +0000)
committerSkynoodle <iainrobert.maciver@gmail.com>
Fri, 1 Jan 2021 18:38:30 +0000 (18:38 +0000)
compiler/rustc_lint/src/nonstandard_style.rs

index 6d61b86f32ef90155f131c86331cb7c0f1af1a70..39cebe7f969368dbea328e7e485cd54b690d4a4e 100644 (file)
@@ -275,10 +275,24 @@ fn is_snake_case(ident: &str) -> bool {
                     // 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() {
                     // 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,
                         err.span_suggestion(
                             ident.span,
-                            "convert the identifier to snake case",
-                            sc,
+                            message,
+                            suggestion,
                             Applicability::MaybeIncorrect,
                         );
                     } else {
                             Applicability::MaybeIncorrect,
                         );
                     } else {