Skip to content

Commit c7d6c3d

Browse files
committed
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
2 parents fde6927 + 750c52a commit c7d6c3d

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

compiler/rustc_lint/src/nonstandard_style.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,25 @@ 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+
err.note(&format!("`{}` cannot be used as a raw identifier", sc));
287+
("rename the identifier", String::new())
288+
}
289+
} else {
290+
("convert the identifier to snake case", sc)
291+
};
292+
278293
err.span_suggestion(
279294
ident.span,
280-
"convert the identifier to snake case",
281-
sc,
295+
message,
296+
suggestion,
282297
Applicability::MaybeIncorrect,
283298
);
284299
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![warn(unused)]
2+
#![allow(dead_code)]
3+
#![deny(non_snake_case)]
4+
5+
mod Impl {}
6+
//~^ ERROR module `Impl` should have a snake case name
7+
8+
fn While() {}
9+
//~^ ERROR function `While` should have a snake case name
10+
11+
fn main() {
12+
let Mod: usize = 0;
13+
//~^ ERROR variable `Mod` should have a snake case name
14+
//~^^ WARN unused variable: `Mod`
15+
16+
let Super: usize = 0;
17+
//~^ ERROR variable `Super` should have a snake case name
18+
//~^^ WARN unused variable: `Super`
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
warning: unused variable: `Mod`
2+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:12:9
3+
|
4+
LL | let Mod: usize = 0;
5+
| ^^^ help: if this is intentional, prefix it with an underscore: `_Mod`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:1:9
9+
|
10+
LL | #![warn(unused)]
11+
| ^^^^^^
12+
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
13+
14+
warning: unused variable: `Super`
15+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:16:9
16+
|
17+
LL | let Super: usize = 0;
18+
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_Super`
19+
20+
error: module `Impl` should have a snake case name
21+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:5:5
22+
|
23+
LL | mod Impl {}
24+
| ^^^^
25+
|
26+
note: the lint level is defined here
27+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:3:9
28+
|
29+
LL | #![deny(non_snake_case)]
30+
| ^^^^^^^^^^^^^^
31+
help: rename the identifier or convert it to a snake case raw identifier
32+
|
33+
LL | mod r#impl {}
34+
| ^^^^^^
35+
36+
error: function `While` should have a snake case name
37+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:8:4
38+
|
39+
LL | fn While() {}
40+
| ^^^^^
41+
|
42+
help: rename the identifier or convert it to a snake case raw identifier
43+
|
44+
LL | fn r#while() {}
45+
| ^^^^^^^
46+
47+
error: variable `Mod` should have a snake case name
48+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:12:9
49+
|
50+
LL | let Mod: usize = 0;
51+
| ^^^
52+
|
53+
help: rename the identifier or convert it to a snake case raw identifier
54+
|
55+
LL | let r#mod: usize = 0;
56+
| ^^^^^
57+
58+
error: variable `Super` should have a snake case name
59+
--> $DIR/lint-non-snake-case-identifiers-suggestion-reserved.rs:16:9
60+
|
61+
LL | let Super: usize = 0;
62+
| ^^^^^ help: rename the identifier
63+
|
64+
= note: `super` cannot be used as a raw identifier
65+
66+
error: aborting due to 4 previous errors; 2 warnings emitted
67+

0 commit comments

Comments
 (0)