From 486c7175fe784194e4523c53ca12d5528f6f9134 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 5 Feb 2021 15:02:39 +0100 Subject: Remove unnecessary allocs in case_conv --- .../hir_ty/src/diagnostics/decl_check/case_conv.rs | 34 ++++++++++------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs index 14e4d92f0..3ab36caf2 100644 --- a/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs +++ b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs @@ -5,7 +5,7 @@ // 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 { if is_camel_case(ident) { return None; @@ -17,7 +17,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option { .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; @@ -30,9 +30,9 @@ pub(crate) fn to_camel_case(ident: &str) -> Option { } 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(); @@ -41,16 +41,16 @@ pub(crate) fn to_camel_case(ident: &str) -> Option { camel_cased_component }) - .fold((String::new(), None), |(acc, prev): (String, Option), next| { + .fold((String::new(), None), |(acc, prev): (_, Option), 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; @@ -92,14 +92,12 @@ fn is_camel_case(name: &str) -> bool { 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); -- cgit v1.2.3