-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Summary
While refactoring code to adhere to module_name_repetitions
we noticed that our constants wouldn't get flagged.
Example
mod foo {
#![warn(clippy::module_name_repetitions)]
pub const FOO_CONSTANT: usize = 0; // lint does not work
pub fn foo_function() {} // lint gives warning
pub struct FooStruct; // lint gives warning
}
This seems like it might be a bug, as I think the reasoning given in the lint description as well as the arguments given in the discussion around RFC #356 also apply to constants.
Cause
After doing some investigation this appears to be happening due to how to_camel_case()
in clippy_utils::str_utils
handles uppercase input.
rust-clippy/clippy_utils/src/str_utils.rs
Lines 270 to 277 in 894e87c
pub fn to_camel_case(item_name: &str) -> String { | |
let mut s = String::new(); | |
let mut up = true; | |
for c in item_name.chars() { | |
if c.is_uppercase() { | |
// we only turn snake case text into CamelCase | |
return item_name.to_string(); | |
} |
Because of the early return here the comparison between item_camel
and mod_camel
in item_name_repetitions.rs
returns false and the SCREAMING_SNAKE_CASE
name doesn't get flagged by the lint.
rust-clippy/clippy_lints/src/item_name_repetitions.rs
Lines 406 to 407 in 894e87c
let item_name = item.ident.name.as_str(); | |
let item_camel = to_camel_case(item_name); |
rust-clippy/clippy_lints/src/item_name_repetitions.rs
Lines 430 to 431 in 894e87c
let matching = count_match_start(mod_camel, &item_camel); | |
let rmatching = count_match_end(mod_camel, &item_camel); |
If this is a bug indeed and the behavior is not intentional I'd be happy to work on a fix and put in a pull-request!
Lint Name
module_name_repetitions
Reproducer
No response
Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1