Skip to content

Commit a4e5f90

Browse files
committed
slightly optimize the non-camel-case-types lint
1 parent 2972b5e commit a4e5f90

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,27 @@ declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]);
5151
/// be upper cased or lower cased. For the purposes of the lint suggestion, we care about being able
5252
/// to change the char's case.
5353
fn char_has_case(c: char) -> bool {
54-
let mut l = c.to_lowercase();
55-
let mut u = c.to_uppercase();
56-
while let Some(l) = l.next() {
57-
match u.next() {
58-
Some(u) if l != u => return true,
59-
_ => {}
60-
}
61-
}
62-
u.next().is_some()
54+
!c.to_lowercase().eq(c.to_uppercase())
55+
}
56+
57+
// contains a capitalisable character followed by, or preceded by, an underscore
58+
fn has_underscore_case(s: &str) -> bool {
59+
let mut last = '\0';
60+
s.chars().any(|c| match (std::mem::replace(&mut last, c), c) {
61+
('_', cs) | (cs, '_') => char_has_case(cs),
62+
_ => false,
63+
})
6364
}
6465

6566
fn is_camel_case(name: &str) -> bool {
6667
let name = name.trim_matches('_');
67-
if name.is_empty() {
68+
let Some(first) = name.chars().next() else {
6869
return true;
69-
}
70+
};
7071

71-
// start with a non-lowercase letter rather than non-uppercase
72+
// start with a non-lowercase letter rather than uppercase
7273
// ones (some scripts don't have a concept of upper/lowercase)
73-
!name.chars().next().unwrap().is_lowercase()
74-
&& !name.contains("__")
75-
&& !name.chars().collect::<Vec<_>>().array_windows().any(|&[fst, snd]| {
76-
// contains a capitalisable character followed by, or preceded by, an underscore
77-
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
78-
})
74+
!(first.is_lowercase() || name.contains("__") || has_underscore_case(name))
7975
}
8076

8177
fn to_camel_case(s: &str) -> String {

0 commit comments

Comments
 (0)