Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d58fdfb

Browse files
committed
Fix False Positive on enum_variants when prefixes are not camel-case
1 parent f327f0e commit d58fdfb

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

clippy_lints/src/enum_variants.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
44
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};
66
use rustc_hir::{EnumDef, Item, ItemKind, Variant};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -157,39 +157,35 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n
157157
}
158158

159159
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();
162163
for var in def.variants {
163164
check_enum_start(cx, item_name, var);
164165
check_enum_end(cx, item_name, var);
165166
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-
}
182167

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();
188184
}
189185
let (what, value) = match (pre.is_empty(), post.is_empty()) {
190186
(true, true) => return,
191-
(false, _) => ("pre", pre),
192-
(true, false) => ("post", post),
187+
(false, _) => ("pre", pre.join("")),
188+
(true, false) => ("post", post.join("")),
193189
};
194190
span_lint_and_help(
195191
cx,

0 commit comments

Comments
 (0)