Skip to content

Commit 779e041

Browse files
committed
Simplify is_camel_case to use map_windows
1 parent 8655ffb commit 779e041

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(control_flow_enum)]
3535
#![feature(if_let_guard)]
3636
#![feature(iter_intersperse)]
37+
#![feature(iter_map_windows)]
3738
#![feature(iter_order_by)]
3839
#![feature(let_chains)]
3940
#![feature(min_specialization)]

compiler/rustc_lint/src/nonstandard_style.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,21 @@ fn is_camel_case(name: &str) -> bool {
8989
// ones (some scripts don't have a concept of upper/lowercase)
9090
!name.chars().next().unwrap().is_lowercase()
9191
&& !name.contains("__")
92-
&& !name.chars().collect::<Vec<_>>().array_windows().any(|&[fst, snd]| {
93-
// contains a capitalisable character followed by, or preceded by, an underscore
94-
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
95-
})
9692
&& !name
9793
.chars()
98-
.collect::<Vec<_>>()
99-
.array_windows()
100-
.any(|&[fst, snd, thr]| fst.is_uppercase() && snd.is_uppercase() && thr.is_uppercase())
94+
.map_windows(|&[fst, snd]| (fst, snd))
95+
.any(|(fst, snd)| {
96+
// contains a capitalisable character followed by, or preceded by, an underscore
97+
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
98+
})
99+
&& !name
100+
.chars()
101+
.map_windows(|&[fst, snd, thd]| (fst, snd, thd))
102+
.any(|(fst, snd, thd)| {
103+
// three capitalized characters in a row
104+
char_has_case(fst) && char_has_case(snd) && char_has_case(thd)
105+
&& fst.is_uppercase() && snd.is_uppercase() && thd.is_uppercase()
106+
})
101107
}
102108

103109
fn to_camel_case(s: &str) -> String {

compiler/rustc_lint/src/nonstandard_style/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ fn camel_case() {
2323
assert_eq!(to_camel_case("ONE"), "One");
2424

2525
assert!(is_camel_case("AStr"));
26+
27+
assert!(is_camel_case("Foo"));
2628
}

0 commit comments

Comments
 (0)