about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-01-02 22:06:17 +0000
committerbors <bors@rust-lang.org>2021-01-02 22:06:17 +0000
commitc7d6c3dfdca64e604838f40573d82561b580a98a (patch)
tree6219a05cb549e7ee4d4e6c1c60ccabc360ab666a /compiler
parentfde692739576089729885b7f79aa2232cb9778c5 (diff)
parent750c52af7334e325e9c3980dbdad10dedeb22f82 (diff)
downloadrust-c7d6c3dfdca64e604838f40573d82561b580a98a.tar.gz
rust-c7d6c3dfdca64e604838f40573d82561b580a98a.zip
Auto merge of #80592 - Skynoodle:snake-case-lint-reserved-identifier, r=davidtwco
Suggest renaming or escaping when fixing non-snake-case identifiers which would conflict with keywords

Fixes #80575
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_lint/src/nonstandard_style.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs
index 6d61b86f32e..8bb3ff0dde0 100644
--- a/compiler/rustc_lint/src/nonstandard_style.rs
+++ b/compiler/rustc_lint/src/nonstandard_style.rs
@@ -275,10 +275,25 @@ 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 {
+                                err.note(&format!("`{}` cannot be used as a raw identifier", sc));
+                                ("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 {