Skip to content

Commit

Permalink
Fix match_str_case_mismatch on uncased chars
Browse files Browse the repository at this point in the history
Fixes #7863.
  • Loading branch information
Herschel committed Oct 23, 2021
1 parent df65291 commit f453047
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/match_str_case_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {

fn verify_case<'a>(case_method: &'a CaseMethod, arms: &'a [Arm<'_>]) -> Option<(Span, SymbolStr)> {
let case_check = match case_method {
CaseMethod::LowerCase => |input: &str| -> bool { input.chars().all(char::is_lowercase) },
CaseMethod::AsciiLowerCase => |input: &str| -> bool { input.chars().all(|c| matches!(c, 'a'..='z')) },
CaseMethod::UpperCase => |input: &str| -> bool { input.chars().all(char::is_uppercase) },
CaseMethod::AsciiUppercase => |input: &str| -> bool { input.chars().all(|c| matches!(c, 'A'..='Z')) },
CaseMethod::LowerCase => |input: &str| -> bool { !input.chars().any(char::is_uppercase) },
CaseMethod::AsciiLowerCase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_uppercase()) },
CaseMethod::UpperCase => |input: &str| -> bool { !input.chars().any(char::is_lowercase) },
CaseMethod::AsciiUppercase => |input: &str| -> bool { !input.chars().any(|c| c.is_ascii_lowercase()) },
};

for arm in arms {
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/match_str_case_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ fn as_str_match() {
}
}

fn non_alphabetic() {
let var = "~!@#$%^&*()-_=+FOO";

match var.to_ascii_lowercase().as_str() {
"1234567890" => {},
"~!@#$%^&*()-_=+foo" => {},
"\n\r\t\x7F" => {},
_ => {},
}
}

fn unicode_cased() {
let var = "ВОДЫ";

match var.to_lowercase().as_str() {
"水" => {},
"νερό" => {},
"воды" => {},
"물" => {},
_ => {},
}
}

fn addrof_unary_match() {
let var = "BAR";

Expand Down Expand Up @@ -70,6 +93,29 @@ fn as_str_match_mismatch() {
}
}

fn non_alphabetic_mismatch() {
let var = "~!@#$%^&*()-_=+FOO";

match var.to_ascii_lowercase().as_str() {
"1234567890" => {},
"~!@#$%^&*()-_=+Foo" => {},
"\n\r\t\x7F" => {},
_ => {},
}
}

fn unicode_cased_mismatch() {
let var = "ВОДЫ";

match var.to_lowercase().as_str() {
"水" => {},
"νερό" => {},
"Воды" => {},
"물" => {},
_ => {},
}
}

fn addrof_unary_match_mismatch() {
let var = "BAR";

Expand Down
30 changes: 26 additions & 4 deletions tests/ui/match_str_case_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this `match` arm has a differing case than its expression
--> $DIR/match_str_case_mismatch.rs:68:9
--> $DIR/match_str_case_mismatch.rs:91:9
|
LL | "Bar" => {},
| ^^^^^
Expand All @@ -11,7 +11,29 @@ LL | "bar" => {},
| ~~~~~

error: this `match` arm has a differing case than its expression
--> $DIR/match_str_case_mismatch.rs:78:9
--> $DIR/match_str_case_mismatch.rs:101:9
|
LL | "~!@#$%^&*()-_=+Foo" => {},
| ^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the case of this arm to respect `to_ascii_lowercase`
|
LL | "~!@#$%^&*()-_=+foo" => {},
| ~~~~~~~~~~~~~~~~~~~~

error: this `match` arm has a differing case than its expression
--> $DIR/match_str_case_mismatch.rs:113:9
|
LL | "Воды" => {},
| ^^^^^^
|
help: consider changing the case of this arm to respect `to_lower_case`
|
LL | "воды" => {},
| ~~~~~~

error: this `match` arm has a differing case than its expression
--> $DIR/match_str_case_mismatch.rs:124:9
|
LL | "Bar" => {},
| ^^^^^
Expand All @@ -22,7 +44,7 @@ LL | "bar" => {},
| ~~~~~

error: this `match` arm has a differing case than its expression
--> $DIR/match_str_case_mismatch.rs:93:9
--> $DIR/match_str_case_mismatch.rs:139:9
|
LL | "bAR" => {},
| ^^^^^
Expand All @@ -32,5 +54,5 @@ help: consider changing the case of this arm to respect `to_ascii_uppercase`
LL | "BAR" => {},
| ~~~~~

error: aborting due to 3 previous errors
error: aborting due to 5 previous errors

0 comments on commit f453047

Please sign in to comment.