Skip to content

Commit 539402c

Browse files
committed
Auto merge of rust-lang#77805 - JohnTitor:non-standard-char-sugg, r=Dylan-DPC
lint: Do not provide suggestions for non standard characters Fixes rust-lang#77273 Only provide suggestions if the case-fixed result is different than the original.
2 parents 822fa7c + 410fc0e commit 539402c

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

โ€Žcompiler/rustc_lint/src/nonstandard_style.rs

+35-19
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,20 @@ impl NonCamelCaseTypes {
127127
if !is_camel_case(name) {
128128
cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, |lint| {
129129
let msg = format!("{} `{}` should have an upper camel case name", sort, name);
130-
lint.build(&msg)
131-
.span_suggestion(
130+
let mut err = lint.build(&msg);
131+
let cc = to_camel_case(name);
132+
// We cannot provide meaningful suggestions
133+
// if the characters are in the category of "Lowercase Letter".
134+
if name.to_string() != cc {
135+
err.span_suggestion(
132136
ident.span,
133137
"convert the identifier to upper camel case",
134138
to_camel_case(name),
135139
Applicability::MaybeIncorrect,
136-
)
137-
.emit()
140+
);
141+
}
142+
143+
err.emit();
138144
})
139145
}
140146
}
@@ -263,17 +269,21 @@ impl NonSnakeCase {
263269
let sc = NonSnakeCase::to_snake_case(name);
264270
let msg = format!("{} `{}` should have a snake case name", sort, name);
265271
let mut err = lint.build(&msg);
266-
// We have a valid span in almost all cases, but we don't have one when linting a crate
267-
// name provided via the command line.
268-
if !ident.span.is_dummy() {
269-
err.span_suggestion(
270-
ident.span,
271-
"convert the identifier to snake case",
272-
sc,
273-
Applicability::MaybeIncorrect,
274-
);
275-
} else {
276-
err.help(&format!("convert the identifier to snake case: `{}`", sc));
272+
// We cannot provide meaningful suggestions
273+
// if the characters are in the category of "Uppercase Letter".
274+
if name.to_string() != sc {
275+
// We have a valid span in almost all cases, but we don't have one when linting a crate
276+
// name provided via the command line.
277+
if !ident.span.is_dummy() {
278+
err.span_suggestion(
279+
ident.span,
280+
"convert the identifier to snake case",
281+
sc,
282+
Applicability::MaybeIncorrect,
283+
);
284+
} else {
285+
err.help(&format!("convert the identifier to snake case: `{}`", sc));
286+
}
277287
}
278288

279289
err.emit();
@@ -441,14 +451,20 @@ impl NonUpperCaseGlobals {
441451
if name.chars().any(|c| c.is_lowercase()) {
442452
cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, ident.span, |lint| {
443453
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
444-
lint.build(&format!("{} `{}` should have an upper case name", sort, name))
445-
.span_suggestion(
454+
let mut err =
455+
lint.build(&format!("{} `{}` should have an upper case name", sort, name));
456+
// We cannot provide meaningful suggestions
457+
// if the characters are in the category of "Lowercase Letter".
458+
if name.to_string() != uc {
459+
err.span_suggestion(
446460
ident.span,
447461
"convert the identifier to upper case",
448462
uc,
449463
Applicability::MaybeIncorrect,
450-
)
451-
.emit();
464+
);
465+
}
466+
467+
err.emit();
452468
})
453469
}
454470
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// (#77273) These characters are in the general categories of
2+
// "Uppercase/Lowercase Letter".
3+
// The diagnostics don't provide meaningful suggestions for them
4+
// as we cannot convert them properly.
5+
6+
// check-pass
7+
8+
#![feature(non_ascii_idents)]
9+
#![allow(uncommon_codepoints, unused)]
10+
11+
struct ๐•Ÿ๐• ๐•ฅ๐•’๐•”๐•’๐•ž๐•–๐•;
12+
//~^ WARN: type `๐•Ÿ๐• ๐•ฅ๐•’๐•”๐•’๐•ž๐•–๐•` should have an upper camel case name
13+
14+
// FIXME: How we should handle this?
15+
struct ๐•Ÿ๐• ๐•ฅ_๐•’_๐•”๐•’๐•ž๐•–๐•;
16+
//~^ WARN: type `๐•Ÿ๐• ๐•ฅ_๐•’_๐•”๐•’๐•ž๐•–๐•` should have an upper camel case name
17+
18+
static ๐—ป๐—ผ๐—ป๐˜‚๐—ฝ๐—ฝ๐—ฒ๐—ฟ๐—ฐ๐—ฎ๐˜€๐—ฒ: i32 = 1;
19+
//~^ WARN: static variable `๐—ป๐—ผ๐—ป๐˜‚๐—ฝ๐—ฝ๐—ฒ๐—ฟ๐—ฐ๐—ฎ๐˜€๐—ฒ` should have an upper case name
20+
21+
fn main() {
22+
let ๐“ข๐“๐“๐“๐“๐“๐“š๐“”๐“ข = 1;
23+
//~^ WARN: variable `๐“ข๐“๐“๐“๐“๐“๐“š๐“”๐“ข` should have a snake case name
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
warning: type `๐•Ÿ๐• ๐•ฅ๐•’๐•”๐•’๐•ž๐•–๐•` should have an upper camel case name
2+
--> $DIR/special-upper-lower-cases.rs:11:8
3+
|
4+
LL | struct ๐•Ÿ๐• ๐•ฅ๐•’๐•”๐•’๐•ž๐•–๐•;
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(non_camel_case_types)]` on by default
8+
9+
warning: type `๐•Ÿ๐• ๐•ฅ_๐•’_๐•”๐•’๐•ž๐•–๐•` should have an upper camel case name
10+
--> $DIR/special-upper-lower-cases.rs:15:8
11+
|
12+
LL | struct ๐•Ÿ๐• ๐•ฅ_๐•’_๐•”๐•’๐•ž๐•–๐•;
13+
| ^^^^^^^^^^^ help: convert the identifier to upper camel case: `๐•Ÿ๐• ๐•ฅ๐•’๐•”๐•’๐•ž๐•–๐•`
14+
15+
warning: static variable `๐—ป๐—ผ๐—ป๐˜‚๐—ฝ๐—ฝ๐—ฒ๐—ฟ๐—ฐ๐—ฎ๐˜€๐—ฒ` should have an upper case name
16+
--> $DIR/special-upper-lower-cases.rs:18:8
17+
|
18+
LL | static ๐—ป๐—ผ๐—ป๐˜‚๐—ฝ๐—ฝ๐—ฒ๐—ฟ๐—ฐ๐—ฎ๐˜€๐—ฒ: i32 = 1;
19+
| ^^^^^^^^^^^^
20+
|
21+
= note: `#[warn(non_upper_case_globals)]` on by default
22+
23+
warning: variable `๐“ข๐“๐“๐“๐“๐“๐“š๐“”๐“ข` should have a snake case name
24+
--> $DIR/special-upper-lower-cases.rs:22:9
25+
|
26+
LL | let ๐“ข๐“๐“๐“๐“๐“๐“š๐“”๐“ข = 1;
27+
| ^^^^^^^^^
28+
|
29+
= note: `#[warn(non_snake_case)]` on by default
30+
31+
warning: 4 warnings emitted
32+

0 commit comments

Comments
ย (0)