Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::module_name_repetitions: Detects public item names that are prefixed or suffixed by the containing public module’s name
What it meansDetects public item names that are prefixed or suffixed by the containing public module’s name. It is in the restriction group and off by default.
Does it matter?
It requires the user to type the module name twice in each usage, especially if they choose to import the module rather than its contents. Lack of such repetition is also the style used in the Rust standard library; e.g. io::Error and fmt::Error rather than io::IoError and fmt::FmtError; and array::from_ref rather than array::array_from_ref.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
pub mod foo {
mod iteration {
pub struct FooIter {}
}
pub use iteration::*; // creates the path `foo::FooIter`
}
Write this instead
mod cake {
struct BlackForestCake;
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
module_name_repetitions = "warn"In code
#![warn(clippy::module_name_repetitions)]One run
cargo clippy -- -W clippy::module-name-repetitionsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
allow-exact-repetitions: Whether an item should be allowed to have the same name as its containing moduleallowed-prefixes: List of prefixes to allow when determining whether an item’s name ends with the module’s name. If the rest of an item’s name is an allowed prefix (e.g. item ToFoo or to_foo in module foo), then don’t emit a warning.
Clippy's own documentation
What it does
Detects public item names that are prefixed or suffixed by the containing public module’s name.
Why is this bad?
It requires the user to type the module name twice in each usage, especially if they choose to import the module rather than its contents.
Lack of such repetition is also the style used in the Rust standard library;
e.g. io::Error and fmt::Error rather than io::IoError and fmt::FmtError;
and array::from_ref rather than array::array_from_ref.
Known issues
Glob re-exports are ignored; e.g. this will not warn even though it should:
pub mod foo {
mod iteration {
pub struct FooIter {}
}
pub use iteration::*; // creates the path `foo::FooIter`
}
Example
mod cake {
struct BlackForestCake;
}
Use instead:
mod cake {
struct BlackForest;
}
Past names
- stutter
Configuration
-
allow-exact-repetitions: Whether an item should be allowed to have the same name as its containing module(default:
true) -
allowed-prefixes: List of prefixes to allow when determining whether an item’s name ends with the module’s name. If the rest of an item’s name is an allowed prefix (e.g. itemToFooorto_fooin modulefoo), then don’t emit a warning.
Example
allowed-prefixes = [ "to", "from" ]
Noteworthy
-
By default, the following prefixes are allowed:
to,as,into,from,try_intoandtry_from -
PascalCase variant is included automatically for each snake_case variant (e.g. if
try_intois included,TryIntowill also be included) -
Use
".."as part of the list to indicate that the configured values should be appended to the default configuration of Clippy. By default, any configuration will replace the default value(default:
["to", "as", "into", "from", "try_into", "try_from"])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- cognitive_complexityWe used to think it measured how hard a method is to understand
- missing_trait_methodsChecks if a provided method is used implicitly by a trait implementation
- mixed_read_write_in_expressionunsequenced read of `x`
- mod_module_filesChecks that module layout uses only self named module files; bans mod.rs files
- modulo_arithmeticyou are using modulo operator on constants with different signs: `-17 % 3`
- multiple_inherent_implmultiple implementations of this structure
- multiple_unsafe_ops_per_blockthis `unsafe` block contains 2 unsafe operations, expected only one
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?