mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-16 09:48:10 +00:00
Merge #7567
7567: Remove unnecessary allocs in case_conv r=Veykril a=Veykril and some replace unwraps bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
c72b0c3719
1 changed files with 16 additions and 18 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue