|
2 | 2 |
|
3 | 3 | use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
4 | 4 | use clippy_utils::source::is_present_in_source;
|
5 |
| -use clippy_utils::str_utils::{self, count_match_end, count_match_start}; |
| 5 | +use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start}; |
6 | 6 | use rustc_hir::{EnumDef, Item, ItemKind, Variant};
|
7 | 7 | use rustc_lint::{LateContext, LateLintPass};
|
8 | 8 | use rustc_session::{declare_tool_lint, impl_lint_pass};
|
@@ -157,39 +157,35 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n
|
157 | 157 | }
|
158 | 158 |
|
159 | 159 | let first = &def.variants[0].ident.name.as_str();
|
160 |
| - let mut pre = &first[..str_utils::camel_case_until(&*first).byte_index]; |
161 |
| - let mut post = &first[str_utils::camel_case_start(&*first).byte_index..]; |
| 160 | + let mut pre = camel_case_split(first); |
| 161 | + let mut post = pre.clone(); |
| 162 | + post.reverse(); |
162 | 163 | for var in def.variants {
|
163 | 164 | check_enum_start(cx, item_name, var);
|
164 | 165 | check_enum_end(cx, item_name, var);
|
165 | 166 | let name = var.ident.name.as_str();
|
166 |
| - let pre_match = count_match_start(pre, &name).byte_count; |
167 |
| - pre = &pre[..pre_match]; |
168 |
| - let pre_camel = str_utils::camel_case_until(pre).byte_index; |
169 |
| - pre = &pre[..pre_camel]; |
170 |
| - while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() { |
171 |
| - if next.is_numeric() { |
172 |
| - return; |
173 |
| - } |
174 |
| - if next.is_lowercase() { |
175 |
| - let last = pre.len() - last.len_utf8(); |
176 |
| - let last_camel = str_utils::camel_case_until(&pre[..last]); |
177 |
| - pre = &pre[..last_camel.byte_index]; |
178 |
| - } else { |
179 |
| - break; |
180 |
| - } |
181 |
| - } |
182 | 167 |
|
183 |
| - let post_match = count_match_end(post, &name); |
184 |
| - let post_end = post.len() - post_match.byte_count; |
185 |
| - post = &post[post_end..]; |
186 |
| - let post_camel = str_utils::camel_case_start(post); |
187 |
| - post = &post[post_camel.byte_index..]; |
| 168 | + let variant_split = camel_case_split(&name); |
| 169 | + |
| 170 | + pre = pre |
| 171 | + .iter() |
| 172 | + .copied() |
| 173 | + .zip(variant_split.iter().copied()) |
| 174 | + .take_while(|(a, b)| a == b) |
| 175 | + .map(|e| e.0) |
| 176 | + .collect(); |
| 177 | + post = post |
| 178 | + .iter() |
| 179 | + .copied() |
| 180 | + .zip(variant_split.iter().rev().copied()) |
| 181 | + .take_while(|(a, b)| a == b) |
| 182 | + .map(|e| e.0) |
| 183 | + .collect(); |
188 | 184 | }
|
189 | 185 | let (what, value) = match (pre.is_empty(), post.is_empty()) {
|
190 | 186 | (true, true) => return,
|
191 |
| - (false, _) => ("pre", pre), |
192 |
| - (true, false) => ("post", post), |
| 187 | + (false, _) => ("pre", pre.join("")), |
| 188 | + (true, false) => ("post", post.join("")), |
193 | 189 | };
|
194 | 190 | span_lint_and_help(
|
195 | 191 | cx,
|
|
0 commit comments