Skip to content

Commit d648cc9

Browse files
authored
Do not trigger redundant_pub_crate in external macros (#13952)
Some widely used crates, such as `pin-project-lite`, make use of a `pub(crate)` construct in a private module inside a public macro. This makes unrelated project trigger the lint. There is also an unfortunate situation for Clippy itself: when a new version of `pin-project-lite` or similar lint-trigerring crates is released, those lints which can be found in hundreds of occurrences in dependent crates will change, and appear as diffs in unrelated Clippy PR because the base lintcheck run will be cached with the ancient release of the crates. We currently have the situation [here](https://github.com/rust-lang/rust-clippy/actions/runs/12635410895?pr=13851#user-content-redundant-pub-crate-removed), which 219 lints removed and 219 lints added because of a `pin-project-lite` version change between runs, and the fact that `redundant_pub_crate` triggers on external macros. Also: - Fix #10636 - Fix #12213 changelog: [`redundant_pub_crate`]: do not trigger on external macros
2 parents 34d109f + 5f75715 commit d648cc9

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

clippy_lints/src/redundant_pub_crate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::source::HasSession;
23
use rustc_errors::Applicability;
34
use rustc_hir::def::{DefKind, Res};
45
use rustc_hir::{Item, ItemKind};
56
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_middle::lint::in_external_macro;
68
use rustc_middle::ty;
79
use rustc_session::impl_lint_pass;
810
use rustc_span::def_id::CRATE_DEF_ID;
@@ -49,6 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
4951
&& !cx.effective_visibilities.is_exported(item.owner_id.def_id)
5052
&& self.is_exported.last() == Some(&false)
5153
&& is_not_macro_export(item)
54+
&& !in_external_macro(cx.sess(), item.span)
5255
{
5356
let span = item.span.with_hi(item.ident.span.hi());
5457
let descr = cx.tcx.def_kind(item.owner_id).descr(item.owner_id.to_def_id());

tests/ui/redundant_pub_crate.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macros.rs
12
#![allow(dead_code)]
23
#![warn(clippy::redundant_pub_crate)]
34

@@ -113,4 +114,10 @@ mod issue_8732 {
113114
pub(crate) use some_macro; // ok: macro exports are exempt
114115
}
115116

117+
proc_macros::external! {
118+
mod priv_mod {
119+
pub(crate) fn dummy() {}
120+
}
121+
}
122+
116123
fn main() {}

tests/ui/redundant_pub_crate.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macros.rs
12
#![allow(dead_code)]
23
#![warn(clippy::redundant_pub_crate)]
34

@@ -113,4 +114,10 @@ mod issue_8732 {
113114
pub(crate) use some_macro; // ok: macro exports are exempt
114115
}
115116

117+
proc_macros::external! {
118+
mod priv_mod {
119+
pub(crate) fn dummy() {}
120+
}
121+
}
122+
116123
fn main() {}

tests/ui/redundant_pub_crate.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: pub(crate) function inside private module
2-
--> tests/ui/redundant_pub_crate.rs:6:5
2+
--> tests/ui/redundant_pub_crate.rs:7:5
33
|
44
LL | pub(crate) fn g() {} // private due to m1
55
| ----------^^^^^
@@ -10,119 +10,119 @@ LL | pub(crate) fn g() {} // private due to m1
1010
= help: to override `-D warnings` add `#[allow(clippy::redundant_pub_crate)]`
1111

1212
error: pub(crate) function inside private module
13-
--> tests/ui/redundant_pub_crate.rs:11:9
13+
--> tests/ui/redundant_pub_crate.rs:12:9
1414
|
1515
LL | pub(crate) fn g() {} // private due to m1_1 and m1
1616
| ----------^^^^^
1717
| |
1818
| help: consider using: `pub`
1919

2020
error: pub(crate) module inside private module
21-
--> tests/ui/redundant_pub_crate.rs:15:5
21+
--> tests/ui/redundant_pub_crate.rs:16:5
2222
|
2323
LL | pub(crate) mod m1_2 {
2424
| ----------^^^^^^^^^
2525
| |
2626
| help: consider using: `pub`
2727

2828
error: pub(crate) function inside private module
29-
--> tests/ui/redundant_pub_crate.rs:18:9
29+
--> tests/ui/redundant_pub_crate.rs:19:9
3030
|
3131
LL | pub(crate) fn g() {} // private due to m1_2 and m1
3232
| ----------^^^^^
3333
| |
3434
| help: consider using: `pub`
3535

3636
error: pub(crate) function inside private module
37-
--> tests/ui/redundant_pub_crate.rs:24:9
37+
--> tests/ui/redundant_pub_crate.rs:25:9
3838
|
3939
LL | pub(crate) fn g() {} // private due to m1
4040
| ----------^^^^^
4141
| |
4242
| help: consider using: `pub`
4343

4444
error: pub(crate) function inside private module
45-
--> tests/ui/redundant_pub_crate.rs:31:5
45+
--> tests/ui/redundant_pub_crate.rs:32:5
4646
|
4747
LL | pub(crate) fn g() {} // already crate visible due to m2
4848
| ----------^^^^^
4949
| |
5050
| help: consider using: `pub`
5151

5252
error: pub(crate) function inside private module
53-
--> tests/ui/redundant_pub_crate.rs:36:9
53+
--> tests/ui/redundant_pub_crate.rs:37:9
5454
|
5555
LL | pub(crate) fn g() {} // private due to m2_1
5656
| ----------^^^^^
5757
| |
5858
| help: consider using: `pub`
5959

6060
error: pub(crate) module inside private module
61-
--> tests/ui/redundant_pub_crate.rs:40:5
61+
--> tests/ui/redundant_pub_crate.rs:41:5
6262
|
6363
LL | pub(crate) mod m2_2 {
6464
| ----------^^^^^^^^^
6565
| |
6666
| help: consider using: `pub`
6767

6868
error: pub(crate) function inside private module
69-
--> tests/ui/redundant_pub_crate.rs:43:9
69+
--> tests/ui/redundant_pub_crate.rs:44:9
7070
|
7171
LL | pub(crate) fn g() {} // already crate visible due to m2_2 and m2
7272
| ----------^^^^^
7373
| |
7474
| help: consider using: `pub`
7575

7676
error: pub(crate) function inside private module
77-
--> tests/ui/redundant_pub_crate.rs:49:9
77+
--> tests/ui/redundant_pub_crate.rs:50:9
7878
|
7979
LL | pub(crate) fn g() {} // already crate visible due to m2
8080
| ----------^^^^^
8181
| |
8282
| help: consider using: `pub`
8383

8484
error: pub(crate) function inside private module
85-
--> tests/ui/redundant_pub_crate.rs:61:9
85+
--> tests/ui/redundant_pub_crate.rs:62:9
8686
|
8787
LL | pub(crate) fn g() {} // private due to m3_1
8888
| ----------^^^^^
8989
| |
9090
| help: consider using: `pub`
9191

9292
error: pub(crate) function inside private module
93-
--> tests/ui/redundant_pub_crate.rs:68:9
93+
--> tests/ui/redundant_pub_crate.rs:69:9
9494
|
9595
LL | pub(crate) fn g() {} // already crate visible due to m3_2
9696
| ----------^^^^^
9797
| |
9898
| help: consider using: `pub`
9999

100100
error: pub(crate) function inside private module
101-
--> tests/ui/redundant_pub_crate.rs:81:5
101+
--> tests/ui/redundant_pub_crate.rs:82:5
102102
|
103103
LL | pub(crate) fn g() {} // private: not re-exported by `pub use m4::*`
104104
| ----------^^^^^
105105
| |
106106
| help: consider using: `pub`
107107

108108
error: pub(crate) function inside private module
109-
--> tests/ui/redundant_pub_crate.rs:86:9
109+
--> tests/ui/redundant_pub_crate.rs:87:9
110110
|
111111
LL | pub(crate) fn g() {} // private due to m4_1
112112
| ----------^^^^^
113113
| |
114114
| help: consider using: `pub`
115115

116116
error: pub(crate) module inside private module
117-
--> tests/ui/redundant_pub_crate.rs:90:5
117+
--> tests/ui/redundant_pub_crate.rs:91:5
118118
|
119119
LL | pub(crate) mod m4_2 {
120120
| ----------^^^^^^^^^
121121
| |
122122
| help: consider using: `pub`
123123

124124
error: pub(crate) function inside private module
125-
--> tests/ui/redundant_pub_crate.rs:93:9
125+
--> tests/ui/redundant_pub_crate.rs:94:9
126126
|
127127
LL | pub(crate) fn g() {} // private due to m4_2
128128
| ----------^^^^^

0 commit comments

Comments
 (0)