]> git.lizzy.rs Git - rust.git/commitdiff
Remove unnecessary allocs in case_conv
authorLukas Wirth <lukastw97@gmail.com>
Fri, 5 Feb 2021 14:02:39 +0000 (15:02 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Fri, 5 Feb 2021 14:02:39 +0000 (15:02 +0100)
crates/hir_ty/src/diagnostics/decl_check/case_conv.rs

index 14e4d92f077dd4068781f111d1b9a5dd28acf3e2..3ab36caf2bbf8f59176755339a595108921cdcfa 100644 (file)
@@ -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<String> {
     if is_camel_case(ident) {
         return None;
@@ -17,7 +17,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
         .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<String> {
                 }
 
                 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<String> {
 
             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;
@@ -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);