// from file /compiler/rustc_lint/src/nonstandard_style.rs
/// Converts an identifier to an UpperCamelCase form.
-/// Returns `None` if the string is already is UpperCamelCase.
+/// Returns `None` if the string is already in UpperCamelCase.
pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
if is_camel_case(ident) {
return None;
.split('_')
.filter(|component| !component.is_empty())
.map(|component| {
- let mut camel_cased_component = String::new();
+ let mut camel_cased_component = String::with_capacity(component.len());
let mut new_word = true;
let mut prev_is_lower_case = true;
}
if new_word {
- camel_cased_component.push_str(&c.to_uppercase().to_string());
+ camel_cased_component.extend(c.to_uppercase());
} else {
- camel_cased_component.push_str(&c.to_lowercase().to_string());
+ camel_cased_component.extend(c.to_lowercase());
}
prev_is_lower_case = c.is_lowercase();
camel_cased_component
})
- .fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
+ .fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
// separate two components with an underscore if their boundary cannot
// be distinguished using a uppercase/lowercase case distinction
- let join = if let Some(prev) = prev {
- let l = prev.chars().last().unwrap();
- let f = next.chars().next().unwrap();
- !char_has_case(l) && !char_has_case(f)
- } else {
- false
- };
+ let join = prev
+ .and_then(|prev| {
+ let f = next.chars().next()?;
+ let l = prev.chars().last()?;
+ Some(!char_has_case(l) && !char_has_case(f))
+ })
+ .unwrap_or(false);
(acc + if join { "_" } else { "" } + &next, Some(next))
})
.0;
let mut fst = None;
// start with a non-lowercase letter rather than non-uppercase
// ones (some scripts don't have a concept of upper/lowercase)
- !name.chars().next().unwrap().is_lowercase()
+ name.chars().next().map_or(true, |c| !c.is_lowercase())
&& !name.contains("__")
&& !name.chars().any(|snd| {
- let ret = match (fst, snd) {
- (None, _) => false,
- (Some(fst), snd) => {
- char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
- }
+ let ret = match fst {
+ None => false,
+ Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_',
};
fst = Some(snd);