Skip to content

Commit 6f7e5cb

Browse files
committed
Some minor cleanup
1 parent c8f016f commit 6f7e5cb

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

clippy_lints/src/enum_variants.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ impl EnumVariantNames {
115115
}
116116

117117
impl_lint_pass!(EnumVariantNames => [
118-
ENUM_VARIANT_NAMES,
119-
MODULE_NAME_REPETITIONS,
120-
MODULE_INCEPTION
118+
ENUM_VARIANT_NAMES,
119+
MODULE_NAME_REPETITIONS,
120+
MODULE_INCEPTION
121121
]);
122122

123123
fn check_enum_start(cx: &LateContext<'_>, item_name: &str, variant: &Variant<'_>) {
@@ -169,17 +169,15 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n
169169

170170
pre = pre
171171
.iter()
172-
.copied()
173-
.zip(variant_split.iter().copied())
172+
.zip(variant_split.iter())
174173
.take_while(|(a, b)| a == b)
175-
.map(|e| e.0)
174+
.map(|e| *e.0)
176175
.collect();
177176
post = post
178177
.iter()
179-
.copied()
180-
.zip(variant_split.iter().rev().copied())
178+
.zip(variant_split.iter().rev())
181179
.take_while(|(a, b)| a == b)
182-
.map(|e| e.0)
180+
.map(|e| *e.0)
183181
.collect();
184182
}
185183
let (what, value) = match (pre.is_empty(), post.is_empty()) {

clippy_utils/src/str_utils.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ pub fn camel_case_start(s: &str) -> StrIndex {
7777
/// # use clippy_utils::str_utils::{camel_case_start_from_idx, StrIndex};
7878
/// assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
7979
/// assert_eq!(camel_case_start_from_idx("AbcDef", 1), StrIndex::new(3, 3));
80+
/// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 0), StrIndex::new(0, 0));
81+
/// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 1), StrIndex::new(3, 3));
82+
/// assert_eq!(camel_case_start_from_idx("Abcdefg", 1), StrIndex::new(7, 7));
8083
/// ```
8184
pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
8285
let char_count = s.chars().count();
@@ -94,7 +97,7 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
9497
let mut last_index = StrIndex::new(char_count, s.len());
9598
for (char_index, (byte_index, c)) in iter {
9699
if byte_index < start_idx {
97-
continue;
100+
break;
98101
}
99102
if down {
100103
if c.is_uppercase() {
@@ -120,12 +123,17 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
120123
/// Get the indexes of camel case components of a string `s`
121124
///
122125
/// ```
123-
/// # use clippy_utils::str_utils::{camel_case_indexes, StrIndex};
124-
/// assert_eq!(camel_case_indexes("AbcDef"), vec![StrIndex::new(0, 0), StrIndex::new(3, 3),
125-
/// StrIndex::new(6, 6)]);
126-
/// assert_eq!(camel_case_indexes("abcDef"), vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]);
126+
/// # use clippy_utils::str_utils::{camel_case_indices, StrIndex};
127+
/// assert_eq!(
128+
/// camel_case_indices("AbcDef"),
129+
/// vec![StrIndex::new(0, 0), StrIndex::new(3, 3), StrIndex::new(6, 6)]
130+
/// );
131+
/// assert_eq!(
132+
/// camel_case_indices("abcDef"),
133+
/// vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]
134+
/// );
127135
/// ```
128-
pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
136+
pub fn camel_case_indices(s: &str) -> Vec<StrIndex> {
129137
let mut result = Vec::new();
130138
let mut str_idx = camel_case_start(s);
131139

@@ -146,20 +154,15 @@ pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
146154
/// assert_eq!(camel_case_split("AbcDef"), vec!["Abc", "Def"]);
147155
/// ```
148156
pub fn camel_case_split(s: &str) -> Vec<&str> {
149-
let offsets = camel_case_indexes(s);
150-
let mut idxs_iter = offsets.iter().map(|str_idx| str_idx.byte_index).peekable();
151-
let idxs: Vec<usize> = if let Some(&idx) = idxs_iter.peek() {
152-
if idx == 0 {
153-
idxs_iter.collect()
154-
} else {
155-
Vec::<usize>::from([0]).into_iter().chain(idxs_iter).collect()
156-
}
157-
} else {
158-
return vec![s];
159-
};
160-
let split_points: Vec<(&usize, &usize)> = idxs[..idxs.len() - 1].iter().zip(&idxs[1..]).collect();
157+
let mut offsets = camel_case_indices(s)
158+
.iter()
159+
.map(|e| e.byte_index)
160+
.collect::<Vec<usize>>();
161+
if offsets[0] != 0 {
162+
offsets.insert(0, 0);
163+
}
161164

162-
split_points.iter().map(|(&start, &stop)| &s[start..stop]).collect()
165+
offsets.windows(2).map(|w| &s[w[0]..w[1]]).collect()
163166
}
164167

165168
/// Dealing with sting comparison can be complicated, this struct ensures that both the
@@ -298,11 +301,14 @@ mod test {
298301
assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
299302
assert_eq!(camel_case_start_from_idx("AbcDef", 1), StrIndex::new(3, 3));
300303
assert_eq!(camel_case_start_from_idx("AbcDef", 4), StrIndex::new(6, 6));
304+
assert_eq!(camel_case_start_from_idx("AbcDefGhi", 0), StrIndex::new(0, 0));
305+
assert_eq!(camel_case_start_from_idx("AbcDefGhi", 1), StrIndex::new(3, 3));
306+
assert_eq!(camel_case_start_from_idx("Abcdefg", 1), StrIndex::new(7, 7));
301307
}
302308

303309
#[test]
304-
fn camel_case_indexes_full() {
305-
assert_eq!(camel_case_indexes("Abc\u{f6}\u{f6}DD"), vec![StrIndex::new(7, 9)]);
310+
fn camel_case_indices_full() {
311+
assert_eq!(camel_case_indices("Abc\u{f6}\u{f6}DD"), vec![StrIndex::new(7, 9)]);
306312
}
307313

308314
#[test]

0 commit comments

Comments
 (0)