Skip to content

SCREAMING_SNAKE_CASE and UPPERCASE names don't get flagged by module_name_repetitions #13968

Open
@omansfeld

Description

@omansfeld

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.

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.

let item_name = item.ident.name.as_str();
let item_camel = to_camel_case(item_name);

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions