Skip to content

Commit 36324e8

Browse files
authored
Don't flag cfg(test) as multiple inherent impl (#16041)
changelog: [`multiple_inherent_impl`]: Don't flag cfg(test) impls as repeats fixes #13040
2 parents 52a3999 + 8539de9 commit 36324e8

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

clippy_lints/src/inherent_impl.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_config::types::InherentImplLintScope;
33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::fulfill_or_allowed;
4+
use clippy_utils::{fulfill_or_allowed, is_cfg_test, is_in_cfg_test};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
77
use rustc_hir::{Item, ItemKind, Node};
@@ -100,7 +100,8 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
100100
},
101101
InherentImplLintScope::Crate => Criterion::Crate,
102102
};
103-
match type_map.entry((impl_ty, criterion)) {
103+
let is_test = is_cfg_test(cx.tcx, hir_id) || is_in_cfg_test(cx.tcx, hir_id);
104+
match type_map.entry((impl_ty, criterion, is_test)) {
104105
Entry::Vacant(e) => {
105106
// Store the id for the first impl block of this type. The span is retrieved lazily.
106107
e.insert(IdOrSpan::Id(impl_id));

tests/ui/multiple_inherent_impl_cfg.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ impl A {}
1313
//~^ multiple_inherent_impl
1414

1515
#[cfg(test)]
16-
impl A {} // false positive
17-
//~^ multiple_inherent_impl
16+
impl A {}
1817

1918
#[cfg(test)]
2019
impl A {}
@@ -25,8 +24,7 @@ struct B;
2524
impl B {}
2625

2726
#[cfg(test)]
28-
impl B {} // false positive
29-
//~^ multiple_inherent_impl
27+
impl B {}
3028

3129
impl B {}
3230
//~^ multiple_inherent_impl

tests/ui/multiple_inherent_impl_cfg.stderr

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,52 @@ LL | #![deny(clippy::multiple_inherent_impl)]
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

1818
error: multiple implementations of this structure
19-
--> tests/ui/multiple_inherent_impl_cfg.rs:16:1
20-
|
21-
LL | impl A {} // false positive
22-
| ^^^^^^^^^
23-
|
24-
note: first implementation here
25-
--> tests/ui/multiple_inherent_impl_cfg.rs:10:1
26-
|
27-
LL | impl A {}
28-
| ^^^^^^^^^
29-
30-
error: multiple implementations of this structure
31-
--> tests/ui/multiple_inherent_impl_cfg.rs:20:1
19+
--> tests/ui/multiple_inherent_impl_cfg.rs:19:1
3220
|
3321
LL | impl A {}
3422
| ^^^^^^^^^
3523
|
3624
note: first implementation here
37-
--> tests/ui/multiple_inherent_impl_cfg.rs:10:1
25+
--> tests/ui/multiple_inherent_impl_cfg.rs:16:1
3826
|
3927
LL | impl A {}
4028
| ^^^^^^^^^
4129

4230
error: multiple implementations of this structure
43-
--> tests/ui/multiple_inherent_impl_cfg.rs:28:1
44-
|
45-
LL | impl B {} // false positive
46-
| ^^^^^^^^^
47-
|
48-
note: first implementation here
49-
--> tests/ui/multiple_inherent_impl_cfg.rs:25:1
50-
|
51-
LL | impl B {}
52-
| ^^^^^^^^^
53-
54-
error: multiple implementations of this structure
55-
--> tests/ui/multiple_inherent_impl_cfg.rs:31:1
31+
--> tests/ui/multiple_inherent_impl_cfg.rs:29:1
5632
|
5733
LL | impl B {}
5834
| ^^^^^^^^^
5935
|
6036
note: first implementation here
61-
--> tests/ui/multiple_inherent_impl_cfg.rs:25:1
37+
--> tests/ui/multiple_inherent_impl_cfg.rs:24:1
6238
|
6339
LL | impl B {}
6440
| ^^^^^^^^^
6541

6642
error: multiple implementations of this structure
67-
--> tests/ui/multiple_inherent_impl_cfg.rs:35:1
43+
--> tests/ui/multiple_inherent_impl_cfg.rs:33:1
6844
|
6945
LL | impl B {}
7046
| ^^^^^^^^^
7147
|
7248
note: first implementation here
73-
--> tests/ui/multiple_inherent_impl_cfg.rs:25:1
49+
--> tests/ui/multiple_inherent_impl_cfg.rs:27:1
7450
|
7551
LL | impl B {}
7652
| ^^^^^^^^^
7753

7854
error: multiple implementations of this structure
79-
--> tests/ui/multiple_inherent_impl_cfg.rs:45:1
55+
--> tests/ui/multiple_inherent_impl_cfg.rs:43:1
8056
|
8157
LL | impl C {}
8258
| ^^^^^^^^^
8359
|
8460
note: first implementation here
85-
--> tests/ui/multiple_inherent_impl_cfg.rs:42:1
61+
--> tests/ui/multiple_inherent_impl_cfg.rs:40:1
8662
|
8763
LL | impl C {}
8864
| ^^^^^^^^^
8965

90-
error: aborting due to 7 previous errors
66+
error: aborting due to 5 previous errors
9167

0 commit comments

Comments
 (0)