Skip to content

Commit 7a6d5c0

Browse files
committed
Auto merge of #4006 - phansch:fix_module_name_repetitions_fp, r=flip1995
Fix false positive in module_name_repetitions lint This lint was triggering on modules inside expanded attrs, like for example `#[cfg(test)]` and possibly more. It was not reporting a location in #3892 because `span.lo()` and `span.hi()` both were 0. Fixes #3892 changelog: Fix false positive in `module_name_repetitions` lint
2 parents cafbe7f + 850c24e commit 7a6d5c0

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

clippy_lints/src/attrs.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::reexport::*;
44
use crate::utils::{
5-
in_macro, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
6-
without_block_comments,
5+
in_macro, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg,
6+
span_lint_and_then, without_block_comments,
77
};
88
use if_chain::if_chain;
99
use rustc::hir::*;
@@ -481,20 +481,6 @@ fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
481481
}
482482
}
483483

484-
// If the snippet is empty, it's an attribute that was inserted during macro
485-
// expansion and we want to ignore those, because they could come from external
486-
// sources that the user has no control over.
487-
// For some reason these attributes don't have any expansion info on them, so
488-
// we have to check it this way until there is a better way.
489-
fn is_present_in_source(cx: &LateContext<'_, '_>, span: Span) -> bool {
490-
if let Some(snippet) = snippet_opt(cx, span) {
491-
if snippet.is_empty() {
492-
return false;
493-
}
494-
}
495-
true
496-
}
497-
498484
declare_lint_pass!(DeprecatedCfgAttribute => [DEPRECATED_CFG_ATTR]);
499485

500486
impl EarlyLintPass for DeprecatedCfgAttribute {

clippy_lints/src/enum_variants.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint on enum variants that are prefixed or suffixed by the same characters
22
3-
use crate::utils::{camel_case, in_macro};
3+
use crate::utils::{camel_case, in_macro, is_present_in_source};
44
use crate::utils::{span_help_and_lint, span_lint};
55
use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
66
use rustc::{declare_tool_lint, impl_lint_pass};
@@ -244,7 +244,7 @@ impl EarlyLintPass for EnumVariantNames {
244244
let item_name = item.ident.as_str();
245245
let item_name_chars = item_name.chars().count();
246246
let item_camel = to_camel_case(&item_name);
247-
if !in_macro(item.span) {
247+
if !in_macro(item.span) && is_present_in_source(cx, item.span) {
248248
if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
249249
// constants don't have surrounding modules
250250
if !mod_camel.is_empty() {

clippy_lints/src/utils/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ pub fn in_macro(span: Span) -> bool {
9393
span.ctxt().outer().expn_info().is_some()
9494
}
9595

96+
// If the snippet is empty, it's an attribute that was inserted during macro
97+
// expansion and we want to ignore those, because they could come from external
98+
// sources that the user has no control over.
99+
// For some reason these attributes don't have any expansion info on them, so
100+
// we have to check it this way until there is a better way.
101+
pub fn is_present_in_source<'a, T: LintContext<'a>>(cx: &T, span: Span) -> bool {
102+
if let Some(snippet) = snippet_opt(cx, span) {
103+
if snippet.is_empty() {
104+
return false;
105+
}
106+
}
107+
true
108+
}
109+
96110
/// Checks if type is struct, enum or union type with the given def path.
97111
pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
98112
match ty.sty {

tests/ui/module_name_repetitions.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags: --test
2+
13
#![warn(clippy::module_name_repetitions)]
24
#![allow(dead_code)]
35

@@ -13,4 +15,12 @@ mod foo {
1315
pub struct Foobar;
1416
}
1517

18+
#[cfg(test)]
19+
mod test {
20+
#[test]
21+
fn it_works() {
22+
assert_eq!(2 + 2, 4);
23+
}
24+
}
25+
1626
fn main() {}

tests/ui/module_name_repetitions.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: item name starts with its containing module's name
2-
--> $DIR/module_name_repetitions.rs:6:5
2+
--> $DIR/module_name_repetitions.rs:8:5
33
|
44
LL | pub fn foo_bar() {}
55
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::module-name-repetitions` implied by `-D warnings`
88

99
error: item name ends with its containing module's name
10-
--> $DIR/module_name_repetitions.rs:7:5
10+
--> $DIR/module_name_repetitions.rs:9:5
1111
|
1212
LL | pub fn bar_foo() {}
1313
| ^^^^^^^^^^^^^^^^^^^
1414

1515
error: item name starts with its containing module's name
16-
--> $DIR/module_name_repetitions.rs:8:5
16+
--> $DIR/module_name_repetitions.rs:10:5
1717
|
1818
LL | pub struct FooCake {}
1919
| ^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: item name ends with its containing module's name
22-
--> $DIR/module_name_repetitions.rs:9:5
22+
--> $DIR/module_name_repetitions.rs:11:5
2323
|
2424
LL | pub enum CakeFoo {}
2525
| ^^^^^^^^^^^^^^^^^^^
2626

2727
error: item name starts with its containing module's name
28-
--> $DIR/module_name_repetitions.rs:10:5
28+
--> $DIR/module_name_repetitions.rs:12:5
2929
|
3030
LL | pub struct Foo7Bar;
3131
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)