Skip to content

Commit 488e4e5

Browse files
committed
Add the allow_exact_repetitions option to the module_name_repetitions lint.
1 parent 373df52 commit 488e4e5

File tree

8 files changed

+59
-5
lines changed

8 files changed

+59
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6483,6 +6483,7 @@ Released 2018-09-13
64836483
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
64846484
[`allow-comparison-to-zero`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-comparison-to-zero
64856485
[`allow-dbg-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-dbg-in-tests
6486+
[`allow-exact-repetitions`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-exact-repetitions
64866487
[`allow-expect-in-consts`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-consts
64876488
[`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests
64886489
[`allow-indexing-slicing-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-indexing-slicing-in-tests

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`
7171
* [`dbg_macro`](https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro)
7272

7373

74+
## `allow-exact-repetitions`
75+
Whether an item should be allowed to have the same name as its containing module
76+
77+
**Default Value:** `true`
78+
79+
---
80+
**Affected lints:**
81+
* [`module_name_repetitions`](https://rust-lang.github.io/rust-clippy/master/index.html#module_name_repetitions)
82+
83+
7484
## `allow-expect-in-consts`
7585
Whether `expect` should be allowed in code always evaluated at compile time
7686

clippy_config/src/conf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ define_Conf! {
360360
/// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`
361361
#[lints(dbg_macro)]
362362
allow_dbg_in_tests: bool = false,
363+
/// Whether an item should be allowed to have the same name as its containing module
364+
#[lints(module_name_repetitions)]
365+
allow_exact_repetitions: bool = true,
363366
/// Whether `expect` should be allowed in code always evaluated at compile time
364367
#[lints(expect_used)]
365368
allow_expect_in_consts: bool = true,

clippy_lints/src/item_name_repetitions.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub struct ItemNameRepetitions {
162162
enum_threshold: u64,
163163
struct_threshold: u64,
164164
avoid_breaking_exported_api: bool,
165+
allow_exact_repetitions: bool,
165166
allow_private_module_inception: bool,
166167
allowed_prefixes: FxHashSet<String>,
167168
}
@@ -173,6 +174,7 @@ impl ItemNameRepetitions {
173174
enum_threshold: conf.enum_variant_name_threshold,
174175
struct_threshold: conf.struct_field_name_threshold,
175176
avoid_breaking_exported_api: conf.avoid_breaking_exported_api,
177+
allow_exact_repetitions: conf.allow_exact_repetitions,
176178
allow_private_module_inception: conf.allow_private_module_inception,
177179
allowed_prefixes: conf.allowed_prefixes.iter().map(|s| to_camel_case(s)).collect(),
178180
}
@@ -486,11 +488,21 @@ impl LateLintPass<'_> for ItemNameRepetitions {
486488
}
487489

488490
// The `module_name_repetitions` lint should only trigger if the item has the module in its
489-
// name. Having the same name is accepted.
490-
if cx.tcx.visibility(item.owner_id).is_public()
491-
&& cx.tcx.visibility(mod_owner_id.def_id).is_public()
492-
&& item_camel.len() > mod_camel.len()
493-
{
491+
// name. Having the same name is only accepted if `allow_exact_repetition` is set to `true`.
492+
493+
let both_are_public =
494+
cx.tcx.visibility(item.owner_id).is_public() && cx.tcx.visibility(mod_owner_id.def_id).is_public();
495+
496+
if both_are_public && !self.allow_exact_repetitions && item_camel == *mod_camel {
497+
span_lint(
498+
cx,
499+
MODULE_NAME_REPETITIONS,
500+
ident.span,
501+
"item name is the same as its containing module's name",
502+
);
503+
}
504+
505+
if both_are_public && item_camel.len() > mod_camel.len() {
494506
let matching = count_match_start(mod_camel, &item_camel);
495507
let rmatching = count_match_end(mod_camel, &item_camel);
496508
let nchars = mod_camel.chars().count();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-exact-repetitions = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![warn(clippy::module_name_repetitions)]
2+
#![allow(dead_code)]
3+
4+
pub mod foo {
5+
// this line should produce a warning:
6+
pub fn foo() {}
7+
//~^ module_name_repetitions
8+
9+
// but this line shouldn't
10+
pub fn to_foo() {}
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: item name is the same as its containing module's name
2+
--> tests/ui-toml/item_name_repetitions/allow_exact_repetitions/item_name_repetitions.rs:6:12
3+
|
4+
LL | pub fn foo() {}
5+
| ^^^
6+
|
7+
= note: `-D clippy::module-name-repetitions` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::module_name_repetitions)]`
9+
10+
error: aborting due to 1 previous error
11+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
55
accept-comment-above-statement
66
allow-comparison-to-zero
77
allow-dbg-in-tests
8+
allow-exact-repetitions
89
allow-expect-in-consts
910
allow-expect-in-tests
1011
allow-indexing-slicing-in-tests
@@ -98,6 +99,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
9899
accept-comment-above-statement
99100
allow-comparison-to-zero
100101
allow-dbg-in-tests
102+
allow-exact-repetitions
101103
allow-expect-in-consts
102104
allow-expect-in-tests
103105
allow-indexing-slicing-in-tests
@@ -191,6 +193,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
191193
accept-comment-above-statement
192194
allow-comparison-to-zero
193195
allow-dbg-in-tests
196+
allow-exact-repetitions
194197
allow-expect-in-consts
195198
allow-expect-in-tests
196199
allow-indexing-slicing-in-tests

0 commit comments

Comments
 (0)