diff options
author | Lukas Wirth <[email protected]> | 2021-02-05 14:02:39 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-02-05 14:02:39 +0000 |
commit | 486c7175fe784194e4523c53ca12d5528f6f9134 (patch) | |
tree | a91911ae9662ed705d98af5d45cbf12e1ff76863 /crates/hir_ty | |
parent | ac5958485e5e77b020b3ebe0c100df5a5fe6ca08 (diff) |
Remove unnecessary allocs in case_conv
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check/case_conv.rs | 34 |
1 files changed, 16 insertions, 18 deletions
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 @@ | |||
5 | // from file /compiler/rustc_lint/src/nonstandard_style.rs | 5 | // from file /compiler/rustc_lint/src/nonstandard_style.rs |
6 | 6 | ||
7 | /// Converts an identifier to an UpperCamelCase form. | 7 | /// Converts an identifier to an UpperCamelCase form. |
8 | /// Returns `None` if the string is already is UpperCamelCase. | 8 | /// Returns `None` if the string is already in UpperCamelCase. |
9 | pub(crate) fn to_camel_case(ident: &str) -> Option<String> { | 9 | pub(crate) fn to_camel_case(ident: &str) -> Option<String> { |
10 | if is_camel_case(ident) { | 10 | if is_camel_case(ident) { |
11 | return None; | 11 | return None; |
@@ -17,7 +17,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> { | |||
17 | .split('_') | 17 | .split('_') |
18 | .filter(|component| !component.is_empty()) | 18 | .filter(|component| !component.is_empty()) |
19 | .map(|component| { | 19 | .map(|component| { |
20 | let mut camel_cased_component = String::new(); | 20 | let mut camel_cased_component = String::with_capacity(component.len()); |
21 | 21 | ||
22 | let mut new_word = true; | 22 | let mut new_word = true; |
23 | let mut prev_is_lower_case = true; | 23 | let mut prev_is_lower_case = true; |
@@ -30,9 +30,9 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> { | |||
30 | } | 30 | } |
31 | 31 | ||
32 | if new_word { | 32 | if new_word { |
33 | camel_cased_component.push_str(&c.to_uppercase().to_string()); | 33 | camel_cased_component.extend(c.to_uppercase()); |
34 | } else { | 34 | } else { |
35 | camel_cased_component.push_str(&c.to_lowercase().to_string()); | 35 | camel_cased_component.extend(c.to_lowercase()); |
36 | } | 36 | } |
37 | 37 | ||
38 | prev_is_lower_case = c.is_lowercase(); | 38 | prev_is_lower_case = c.is_lowercase(); |
@@ -41,16 +41,16 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> { | |||
41 | 41 | ||
42 | camel_cased_component | 42 | camel_cased_component |
43 | }) | 43 | }) |
44 | .fold((String::new(), None), |(acc, prev): (String, Option<String>), next| { | 44 | .fold((String::new(), None), |(acc, prev): (_, Option<String>), next| { |
45 | // separate two components with an underscore if their boundary cannot | 45 | // separate two components with an underscore if their boundary cannot |
46 | // be distinguished using a uppercase/lowercase case distinction | 46 | // be distinguished using a uppercase/lowercase case distinction |
47 | let join = if let Some(prev) = prev { | 47 | let join = prev |
48 | let l = prev.chars().last().unwrap(); | 48 | .and_then(|prev| { |
49 | let f = next.chars().next().unwrap(); | 49 | let f = next.chars().next()?; |
50 | !char_has_case(l) && !char_has_case(f) | 50 | let l = prev.chars().last()?; |
51 | } else { | 51 | Some(!char_has_case(l) && !char_has_case(f)) |
52 | false | 52 | }) |
53 | }; | 53 | .unwrap_or(false); |
54 | (acc + if join { "_" } else { "" } + &next, Some(next)) | 54 | (acc + if join { "_" } else { "" } + &next, Some(next)) |
55 | }) | 55 | }) |
56 | .0; | 56 | .0; |
@@ -92,14 +92,12 @@ fn is_camel_case(name: &str) -> bool { | |||
92 | let mut fst = None; | 92 | let mut fst = None; |
93 | // start with a non-lowercase letter rather than non-uppercase | 93 | // start with a non-lowercase letter rather than non-uppercase |
94 | // ones (some scripts don't have a concept of upper/lowercase) | 94 | // ones (some scripts don't have a concept of upper/lowercase) |
95 | !name.chars().next().unwrap().is_lowercase() | 95 | name.chars().next().map_or(true, |c| !c.is_lowercase()) |
96 | && !name.contains("__") | 96 | && !name.contains("__") |
97 | && !name.chars().any(|snd| { | 97 | && !name.chars().any(|snd| { |
98 | let ret = match (fst, snd) { | 98 | let ret = match fst { |
99 | (None, _) => false, | 99 | None => false, |
100 | (Some(fst), snd) => { | 100 | Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_', |
101 | char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_' | ||
102 | } | ||
103 | }; | 101 | }; |
104 | fst = Some(snd); | 102 | fst = Some(snd); |
105 | 103 | ||