Skip to content

Commit 3a1083b

Browse files
committed
remove unnecessary check and move to_camel_case to str_util
1 parent 4bd5a78 commit 3a1083b

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

clippy_lints/foo.txt

Whitespace-only changes.

clippy_lints/src/item_name_repetitions.rs

Lines changed: 6 additions & 36 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, span_lint_hir};
44
use clippy_utils::source::is_present_in_source;
5-
use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start, to_snake_case};
5+
use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start, to_camel_case, to_snake_case};
66
use rustc_hir::{EnumDef, FieldDef, Item, ItemKind, OwnerId, Variant, VariantData};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -165,48 +165,20 @@ impl_lint_pass!(ItemNameRepetitions => [
165165
MODULE_INCEPTION
166166
]);
167167

168-
fn is_snake_case(name: &str) -> bool {
169-
name.chars().all(|c| !c.is_uppercase() || c == '_')
170-
}
171-
172168
#[must_use]
173169
fn have_no_extra_prefix(prefixes: &[&str]) -> bool {
174170
prefixes.iter().all(|p| p == &"" || p == &"_")
175171
}
176172

177-
#[must_use]
178-
fn to_camel_case(item_name: &str) -> String {
179-
let mut s = String::new();
180-
let mut up = true;
181-
for c in item_name.chars() {
182-
if c.is_uppercase() {
183-
// we only turn snake case text into CamelCase
184-
return item_name.to_string();
185-
}
186-
if c == '_' {
187-
up = true;
188-
continue;
189-
}
190-
if up {
191-
up = false;
192-
s.extend(c.to_uppercase());
193-
} else {
194-
s.push(c);
195-
}
196-
}
197-
s
198-
}
199-
200173
fn check_fields(cx: &LateContext<'_>, threshold: u64, fields: &[FieldDef<'_>], item_name: &str, item_span: Span) {
201174
if (fields.len() as u64) < threshold {
202175
return;
203176
}
204177

178+
let item_name_snake = to_snake_case(item_name);
205179
for field in fields {
206-
if is_snake_case(field.ident.name.as_str()) {
207-
check_struct_start(cx, item_name, field);
208-
check_struct_end(cx, item_name, field);
209-
}
180+
check_struct_start(cx, &item_name_snake, field);
181+
check_struct_end(cx, &item_name_snake, field);
210182
}
211183

212184
let mut pre: Vec<&str> = match fields.get(0) {
@@ -255,10 +227,9 @@ fn check_fields(cx: &LateContext<'_>, threshold: u64, fields: &[FieldDef<'_>], i
255227
}
256228

257229
fn check_struct_start(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_>) {
258-
let item_name_snake = to_snake_case(item_name);
259230
let name = field.ident.name.as_str();
260231

261-
if name.starts_with(&item_name_snake) {
232+
if name.starts_with(item_name) {
262233
span_lint_hir(
263234
cx,
264235
STRUCT_FIELD_NAMES,
@@ -270,10 +241,9 @@ fn check_struct_start(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_
270241
}
271242

272243
fn check_struct_end(cx: &LateContext<'_>, item_name: &str, field: &FieldDef<'_>) {
273-
let item_name_snake = to_snake_case(item_name);
274244
let name = field.ident.name.as_str();
275245

276-
if name.ends_with(&item_name_snake) {
246+
if name.ends_with(item_name) {
277247
span_lint_hir(
278248
cx,
279249
STRUCT_FIELD_NAMES,

clippy_utils/src/str_utils.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,35 @@ pub fn to_snake_case(name: &str) -> String {
259259
}
260260
s
261261
}
262+
/// Returns a `CamelCase` version of the input
263+
/// ```
264+
/// use clippy_utils::str_utils::to_camel_case;
265+
/// assert_eq!(to_snake_case("abc_def"), "AbcDef");
266+
/// assert_eq!(to_snake_case("a_b_c_d"), "ABCD");
267+
/// assert_eq!(to_snake_case("abc_d_d"), "AbcDD");
268+
/// assert_eq!(to_snake_case("abc1_d_d"), "Abc1DD");
269+
/// ```
270+
pub fn to_camel_case(item_name: &str) -> String {
271+
let mut s = String::new();
272+
let mut up = true;
273+
for c in item_name.chars() {
274+
if c.is_uppercase() {
275+
// we only turn snake case text into CamelCase
276+
return item_name.to_string();
277+
}
278+
if c == '_' {
279+
up = true;
280+
continue;
281+
}
282+
if up {
283+
up = false;
284+
s.extend(c.to_uppercase());
285+
} else {
286+
s.push(c);
287+
}
288+
}
289+
s
290+
}
262291

263292
#[cfg(test)]
264293
mod test {

0 commit comments

Comments
 (0)