Skip to content

Commit 2535804

Browse files
committed
Allow underscores in camel-case between non-alphabetic characters
1 parent b39c4bc commit 2535804

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_lint/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version = "0.0.0"
66
[lib]
77
name = "rustc_lint"
88
path = "lib.rs"
9-
crate-type = ["dylib"]
9+
crate-types = ["rlib", "dylib"]
1010
test = false
1111

1212
[dependencies]
@@ -15,3 +15,5 @@ rustc = { path = "../librustc" }
1515
rustc_const_eval = { path = "../librustc_const_eval" }
1616
syntax = { path = "../libsyntax" }
1717
syntax_pos = { path = "../libsyntax_pos" }
18+
lazy_static = "1.0"
19+
regex = "0.2.3"

src/librustc_lint/bad_style.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ use syntax_pos::Span;
2121
use rustc::hir::{self, PatKind};
2222
use rustc::hir::intravisit::FnKind;
2323

24+
use regex::Regex;
25+
26+
lazy_static! {
27+
static ref ALPHABETIC_UNDERSCORE: Regex = Regex::new("([[:alpha:]])_([[:alpha:]])").unwrap();
28+
}
29+
2430
#[derive(PartialEq)]
2531
pub enum MethodLateContext {
2632
TraitAutoImpl,
@@ -62,20 +68,24 @@ impl NonCamelCaseTypes {
6268

6369
// start with a non-lowercase letter rather than non-uppercase
6470
// ones (some scripts don't have a concept of upper/lowercase)
65-
!name.is_empty() && !name.chars().next().unwrap().is_lowercase() && !name.contains('_')
71+
!name.is_empty() && !name.chars().next().unwrap().is_lowercase() &&
72+
!ALPHABETIC_UNDERSCORE.is_match(name)
6673
}
6774

6875
fn to_camel_case(s: &str) -> String {
69-
s.split('_')
70-
.flat_map(|word| {
71-
word.chars().enumerate().map(|(i, c)| if i == 0 {
72-
c.to_uppercase().collect::<String>()
73-
} else {
74-
c.to_lowercase().collect()
75-
})
76-
})
77-
.collect::<Vec<_>>()
78-
.concat()
76+
let s = s.split('_')
77+
.map(|word| {
78+
word.chars().enumerate().map(|(i, c)| if i == 0 {
79+
c.to_uppercase().collect::<String>()
80+
} else {
81+
c.to_lowercase().collect()
82+
})
83+
.collect::<Vec<_>>()
84+
.concat()
85+
})
86+
.collect::<Vec<_>>()
87+
.join("_");
88+
ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string()
7989
}
8090

8191
if !is_camel_case(name) {

src/librustc_lint/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern crate rustc;
4141
extern crate log;
4242
extern crate rustc_const_eval;
4343
extern crate syntax_pos;
44+
#[macro_use]
45+
extern crate lazy_static;
46+
extern crate regex;
4447

4548
use rustc::lint;
4649
use rustc::middle;

0 commit comments

Comments
 (0)