Skip to content

Commit c72b0c3

Browse files
bors[bot]Veykril
andauthored
Merge #7567
7567: Remove unnecessary allocs in case_conv r=Veykril a=Veykril and some replace unwraps bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents ac59584 + 486c717 commit c72b0c3

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

crates/hir_ty/src/diagnostics/decl_check/case_conv.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// from file /compiler/rustc_lint/src/nonstandard_style.rs
66

77
/// 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.
99
pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
1010
if is_camel_case(ident) {
1111
return None;
@@ -17,7 +17,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
1717
.split('_')
1818
.filter(|component| !component.is_empty())
1919
.map(|component| {
20-
let mut camel_cased_component = String::new();
20+
let mut camel_cased_component = String::with_capacity(component.len());
2121

2222
let mut new_word = true;
2323
let mut prev_is_lower_case = true;
@@ -30,9 +30,9 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
3030
}
3131

3232
if new_word {
33-
camel_cased_component.push_str(&c.to_uppercase().to_string());
33+
camel_cased_component.extend(c.to_uppercase());
3434
} else {
35-
camel_cased_component.push_str(&c.to_lowercase().to_string());
35+
camel_cased_component.extend(c.to_lowercase());
3636
}
3737

3838
prev_is_lower_case = c.is_lowercase();
@@ -41,16 +41,16 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
4141

4242
camel_cased_component
4343
})
44-
.fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
44+
.fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
4545
// separate two components with an underscore if their boundary cannot
4646
// be distinguished using a uppercase/lowercase case distinction
47-
let join = if let Some(prev) = prev {
48-
let l = prev.chars().last().unwrap();
49-
let f = next.chars().next().unwrap();
50-
!char_has_case(l) && !char_has_case(f)
51-
} else {
52-
false
53-
};
47+
let join = prev
48+
.and_then(|prev| {
49+
let f = next.chars().next()?;
50+
let l = prev.chars().last()?;
51+
Some(!char_has_case(l) && !char_has_case(f))
52+
})
53+
.unwrap_or(false);
5454
(acc + if join { "_" } else { "" } + &next, Some(next))
5555
})
5656
.0;
@@ -92,14 +92,12 @@ fn is_camel_case(name: &str) -> bool {
9292
let mut fst = None;
9393
// start with a non-lowercase letter rather than non-uppercase
9494
// 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())
9696
&& !name.contains("__")
9797
&& !name.chars().any(|snd| {
98-
let ret = match (fst, snd) {
99-
(None, _) => false,
100-
(Some(fst), snd) => {
101-
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
102-
}
98+
let ret = match fst {
99+
None => false,
100+
Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_',
103101
};
104102
fst = Some(snd);
105103

0 commit comments

Comments
 (0)