Skip to content

Commit e4c02e5

Browse files
committed
Adjust the rules for underscores
1 parent 2535804 commit e4c02e5

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/librustc_lint/bad_style.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use rustc::hir::intravisit::FnKind;
2424
use regex::Regex;
2525

2626
lazy_static! {
27-
static ref ALPHABETIC_UNDERSCORE: Regex = Regex::new("([[:alpha:]])_([[:alpha:]])").unwrap();
27+
static ref ALPHABETIC_UNDERSCORE: Regex =
28+
Regex::new("([[:alpha:]])_+|_+([[:alpha:]])").unwrap();
2829
}
2930

3031
#[derive(PartialEq)]
@@ -69,11 +70,12 @@ impl NonCamelCaseTypes {
6970
// start with a non-lowercase letter rather than non-uppercase
7071
// ones (some scripts don't have a concept of upper/lowercase)
7172
!name.is_empty() && !name.chars().next().unwrap().is_lowercase() &&
72-
!ALPHABETIC_UNDERSCORE.is_match(name)
73+
!name.contains("__") && !ALPHABETIC_UNDERSCORE.is_match(name)
7374
}
7475

7576
fn to_camel_case(s: &str) -> String {
76-
let s = s.split('_')
77+
let s = s.trim_matches('_')
78+
.split('_')
7779
.map(|word| {
7880
word.chars().enumerate().map(|(i, c)| if i == 0 {
7981
c.to_uppercase().collect::<String>()
@@ -83,6 +85,7 @@ impl NonCamelCaseTypes {
8385
.collect::<Vec<_>>()
8486
.concat()
8587
})
88+
.filter(|x| !x.is_empty())
8689
.collect::<Vec<_>>()
8790
.join("_");
8891
ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string()

0 commit comments

Comments
 (0)