From d10ee0d07e40535e3a167a0a857d9e3a1b1d6a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 29 Jan 2021 11:07:14 -0800 Subject: [PATCH 1/2] Fix invalid camel case suggestion involving unicode idents Follow up to #77805. --- compiler/rustc_lint/src/nonstandard_style.rs | 8 +++++++- src/test/ui/lint/special-upper-lower-cases.stderr | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index ebd6190dc74cc..94fd0e0f097e3 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -57,7 +57,7 @@ declare_lint! { declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]); fn char_has_case(c: char) -> bool { - c.is_lowercase() || c.is_uppercase() + c.to_lowercase().to_string() != c.to_uppercase().to_string() } fn is_camel_case(name: &str) -> bool { @@ -138,6 +138,8 @@ impl NonCamelCaseTypes { to_camel_case(name), Applicability::MaybeIncorrect, ); + } else { + err.span_label(ident.span, "should have an UpperCamelCase name"); } err.emit(); @@ -299,6 +301,8 @@ impl NonSnakeCase { } else { err.help(&format!("convert the identifier to snake case: `{}`", sc)); } + } else { + err.span_label(ident.span, "should have a snake_case name"); } err.emit(); @@ -477,6 +481,8 @@ impl NonUpperCaseGlobals { uc, Applicability::MaybeIncorrect, ); + } else { + err.span_label(ident.span, "should have an UPPER_CASE name"); } err.emit(); diff --git a/src/test/ui/lint/special-upper-lower-cases.stderr b/src/test/ui/lint/special-upper-lower-cases.stderr index f32193a2e4a47..e3b451a15a2cc 100644 --- a/src/test/ui/lint/special-upper-lower-cases.stderr +++ b/src/test/ui/lint/special-upper-lower-cases.stderr @@ -2,7 +2,7 @@ warning: type `𝕟𝕠𝕥𝕒𝕔𝕒𝕞𝕖𝕝` should have an upper camel --> $DIR/special-upper-lower-cases.rs:11:8 | LL | struct 𝕟𝕠𝕥𝕒𝕔𝕒𝕞𝕖𝕝; - | ^^^^^^^^^ + | ^^^^^^^^^ should have an UpperCamelCase name | = note: `#[warn(non_camel_case_types)]` on by default @@ -10,13 +10,13 @@ warning: type `𝕟𝕠𝕥_𝕒_𝕔𝕒𝕞𝕖𝕝` should have an upper came --> $DIR/special-upper-lower-cases.rs:15:8 | LL | struct 𝕟𝕠𝕥_𝕒_𝕔𝕒𝕞𝕖𝕝; - | ^^^^^^^^^^^ help: convert the identifier to upper camel case: `𝕟𝕠𝕥𝕒𝕔𝕒𝕞𝕖𝕝` + | ^^^^^^^^^^^ should have an UpperCamelCase name warning: static variable `𝗻𝗼𝗻𝘂𝗽𝗽𝗲𝗿𝗰𝗮𝘀𝗲` should have an upper case name --> $DIR/special-upper-lower-cases.rs:18:8 | LL | static 𝗻𝗼𝗻𝘂𝗽𝗽𝗲𝗿𝗰𝗮𝘀𝗲: i32 = 1; - | ^^^^^^^^^^^^ + | ^^^^^^^^^^^^ should have an UPPER_CASE name | = note: `#[warn(non_upper_case_globals)]` on by default @@ -24,7 +24,7 @@ warning: variable `𝓢𝓝𝓐𝓐𝓐𝓐𝓚𝓔𝓢` should have a snake cas --> $DIR/special-upper-lower-cases.rs:22:9 | LL | let 𝓢𝓝𝓐𝓐𝓐𝓐𝓚𝓔𝓢 = 1; - | ^^^^^^^^^ + | ^^^^^^^^^ should have a snake_case name | = note: `#[warn(non_snake_case)]` on by default From fa9a99fefce9fbfc73f74732a03925106a3b6e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 30 Jan 2021 22:06:10 -0800 Subject: [PATCH 2/2] review comments --- compiler/rustc_lint/src/nonstandard_style.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 94fd0e0f097e3..121dde325f74b 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -56,8 +56,19 @@ declare_lint! { declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]); +/// Some unicode characters *have* case, are considered upper case or lower case, but they *can't* +/// be upper cased or lower cased. For the purposes of the lint suggestion, we care about being able +/// to change the char's case. fn char_has_case(c: char) -> bool { - c.to_lowercase().to_string() != c.to_uppercase().to_string() + let mut l = c.to_lowercase(); + let mut u = c.to_uppercase(); + while let Some(l) = l.next() { + match u.next() { + Some(u) if l != u => return true, + _ => {} + } + } + u.next().is_some() } fn is_camel_case(name: &str) -> bool {