Skip to content

Commit de90924

Browse files
committed
Update for PR feedback
1 parent 52208f3 commit de90924

File tree

4 files changed

+189
-85
lines changed

4 files changed

+189
-85
lines changed

clippy_lints/src/redundant_pub_crate.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::utils::span_lint_and_help;
1+
use crate::utils::span_lint_and_then;
2+
use rustc_errors::Applicability;
23
use rustc_hir::{Item, ItemKind, VisibilityKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -43,12 +44,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPubCrate {
4344
if let VisibilityKind::Crate { .. } = item.vis.node {
4445
if !cx.access_levels.is_exported(item.hir_id) {
4546
if let Some(false) = self.is_exported.last() {
46-
span_lint_and_help(
47+
let span = item.span.with_hi(item.ident.span.hi());
48+
span_lint_and_then(
4749
cx,
4850
REDUNDANT_PUB_CRATE,
49-
item.span,
51+
span,
5052
&format!("pub(crate) {} inside private module", item.kind.descr()),
51-
"consider using `pub` instead of `pub(crate)`",
53+
|db| {
54+
db.span_suggestion(
55+
item.vis.span,
56+
"consider using",
57+
"pub".to_string(),
58+
Applicability::MachineApplicable,
59+
);
60+
},
5261
)
5362
}
5463
}

tests/ui/redundant_pub_crate.fixed

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
3+
#![warn(clippy::redundant_pub_crate)]
4+
5+
mod m1 {
6+
fn f() {}
7+
pub fn g() {} // private due to m1
8+
pub fn h() {}
9+
10+
mod m1_1 {
11+
fn f() {}
12+
pub fn g() {} // private due to m1_1 and m1
13+
pub fn h() {}
14+
}
15+
16+
pub mod m1_2 {
17+
// ^ private due to m1
18+
fn f() {}
19+
pub fn g() {} // private due to m1_2 and m1
20+
pub fn h() {}
21+
}
22+
23+
pub mod m1_3 {
24+
fn f() {}
25+
pub fn g() {} // private due to m1
26+
pub fn h() {}
27+
}
28+
}
29+
30+
pub(crate) mod m2 {
31+
fn f() {}
32+
pub fn g() {} // already crate visible due to m2
33+
pub fn h() {}
34+
35+
mod m2_1 {
36+
fn f() {}
37+
pub fn g() {} // private due to m2_1
38+
pub fn h() {}
39+
}
40+
41+
pub mod m2_2 {
42+
// ^ already crate visible due to m2
43+
fn f() {}
44+
pub fn g() {} // already crate visible due to m2_2 and m2
45+
pub fn h() {}
46+
}
47+
48+
pub mod m2_3 {
49+
fn f() {}
50+
pub fn g() {} // already crate visible due to m2
51+
pub fn h() {}
52+
}
53+
}
54+
55+
pub mod m3 {
56+
fn f() {}
57+
pub(crate) fn g() {} // ok: m3 is exported
58+
pub fn h() {}
59+
60+
mod m3_1 {
61+
fn f() {}
62+
pub fn g() {} // private due to m3_1
63+
pub fn h() {}
64+
}
65+
66+
pub(crate) mod m3_2 {
67+
// ^ ok
68+
fn f() {}
69+
pub fn g() {} // already crate visible due to m3_2
70+
pub fn h() {}
71+
}
72+
73+
pub mod m3_3 {
74+
fn f() {}
75+
pub(crate) fn g() {} // ok: m3 and m3_3 are exported
76+
pub fn h() {}
77+
}
78+
}
79+
80+
mod m4 {
81+
fn f() {}
82+
pub fn g() {} // private: not re-exported by `pub use m4::*`
83+
pub fn h() {}
84+
85+
mod m4_1 {
86+
fn f() {}
87+
pub fn g() {} // private due to m4_1
88+
pub fn h() {}
89+
}
90+
91+
pub mod m4_2 {
92+
// ^ private: not re-exported by `pub use m4::*`
93+
fn f() {}
94+
pub fn g() {} // private due to m4_2
95+
pub fn h() {}
96+
}
97+
98+
pub mod m4_3 {
99+
fn f() {}
100+
pub(crate) fn g() {} // ok: m4_3 is re-exported by `pub use m4::*`
101+
pub fn h() {}
102+
}
103+
}
104+
105+
pub use m4::*;
106+
107+
fn main() {}

tests/ui/redundant_pub_crate.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
13
#![warn(clippy::redundant_pub_crate)]
24

35
mod m1 {

tests/ui/redundant_pub_crate.stderr

+67-81
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,132 @@
11
error: pub(crate) function inside private module
2-
--> $DIR/redundant_pub_crate.rs:5:5
2+
--> $DIR/redundant_pub_crate.rs:7:5
33
|
44
LL | pub(crate) fn g() {} // private due to m1
5-
| ^^^^^^^^^^^^^^^^^^^^
5+
| ----------^^^^^
6+
| |
7+
| help: consider using: `pub`
68
|
79
= note: `-D clippy::redundant-pub-crate` implied by `-D warnings`
8-
= help: consider using `pub` instead of `pub(crate)`
910

1011
error: pub(crate) function inside private module
11-
--> $DIR/redundant_pub_crate.rs:10:9
12+
--> $DIR/redundant_pub_crate.rs:12:9
1213
|
1314
LL | pub(crate) fn g() {} // private due to m1_1 and m1
14-
| ^^^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: consider using `pub` instead of `pub(crate)`
15+
| ----------^^^^^
16+
| |
17+
| help: consider using: `pub`
1718

1819
error: pub(crate) module inside private module
19-
--> $DIR/redundant_pub_crate.rs:14:5
20-
|
21-
LL | / pub(crate) mod m1_2 {
22-
LL | | // ^ private due to m1
23-
LL | | fn f() {}
24-
LL | | pub(crate) fn g() {} // private due to m1_2 and m1
25-
LL | | pub fn h() {}
26-
LL | | }
27-
| |_____^
20+
--> $DIR/redundant_pub_crate.rs:16:5
2821
|
29-
= help: consider using `pub` instead of `pub(crate)`
22+
LL | pub(crate) mod m1_2 {
23+
| ----------^^^^^^^^^
24+
| |
25+
| help: consider using: `pub`
3026

3127
error: pub(crate) function inside private module
32-
--> $DIR/redundant_pub_crate.rs:17:9
28+
--> $DIR/redundant_pub_crate.rs:19:9
3329
|
3430
LL | pub(crate) fn g() {} // private due to m1_2 and m1
35-
| ^^^^^^^^^^^^^^^^^^^^
36-
|
37-
= help: consider using `pub` instead of `pub(crate)`
31+
| ----------^^^^^
32+
| |
33+
| help: consider using: `pub`
3834

3935
error: pub(crate) function inside private module
40-
--> $DIR/redundant_pub_crate.rs:23:9
36+
--> $DIR/redundant_pub_crate.rs:25:9
4137
|
4238
LL | pub(crate) fn g() {} // private due to m1
43-
| ^^^^^^^^^^^^^^^^^^^^
44-
|
45-
= help: consider using `pub` instead of `pub(crate)`
39+
| ----------^^^^^
40+
| |
41+
| help: consider using: `pub`
4642

4743
error: pub(crate) function inside private module
48-
--> $DIR/redundant_pub_crate.rs:30:5
44+
--> $DIR/redundant_pub_crate.rs:32:5
4945
|
5046
LL | pub(crate) fn g() {} // already crate visible due to m2
51-
| ^^^^^^^^^^^^^^^^^^^^
52-
|
53-
= help: consider using `pub` instead of `pub(crate)`
47+
| ----------^^^^^
48+
| |
49+
| help: consider using: `pub`
5450

5551
error: pub(crate) function inside private module
56-
--> $DIR/redundant_pub_crate.rs:35:9
52+
--> $DIR/redundant_pub_crate.rs:37:9
5753
|
5854
LL | pub(crate) fn g() {} // private due to m2_1
59-
| ^^^^^^^^^^^^^^^^^^^^
60-
|
61-
= help: consider using `pub` instead of `pub(crate)`
55+
| ----------^^^^^
56+
| |
57+
| help: consider using: `pub`
6258

6359
error: pub(crate) module inside private module
64-
--> $DIR/redundant_pub_crate.rs:39:5
60+
--> $DIR/redundant_pub_crate.rs:41:5
6561
|
66-
LL | / pub(crate) mod m2_2 {
67-
LL | | // ^ already crate visible due to m2
68-
LL | | fn f() {}
69-
LL | | pub(crate) fn g() {} // already crate visible due to m2_2 and m2
70-
LL | | pub fn h() {}
71-
LL | | }
72-
| |_____^
73-
|
74-
= help: consider using `pub` instead of `pub(crate)`
62+
LL | pub(crate) mod m2_2 {
63+
| ----------^^^^^^^^^
64+
| |
65+
| help: consider using: `pub`
7566

7667
error: pub(crate) function inside private module
77-
--> $DIR/redundant_pub_crate.rs:42:9
68+
--> $DIR/redundant_pub_crate.rs:44:9
7869
|
7970
LL | pub(crate) fn g() {} // already crate visible due to m2_2 and m2
80-
| ^^^^^^^^^^^^^^^^^^^^
81-
|
82-
= help: consider using `pub` instead of `pub(crate)`
71+
| ----------^^^^^
72+
| |
73+
| help: consider using: `pub`
8374

8475
error: pub(crate) function inside private module
85-
--> $DIR/redundant_pub_crate.rs:48:9
76+
--> $DIR/redundant_pub_crate.rs:50:9
8677
|
8778
LL | pub(crate) fn g() {} // already crate visible due to m2
88-
| ^^^^^^^^^^^^^^^^^^^^
89-
|
90-
= help: consider using `pub` instead of `pub(crate)`
79+
| ----------^^^^^
80+
| |
81+
| help: consider using: `pub`
9182

9283
error: pub(crate) function inside private module
93-
--> $DIR/redundant_pub_crate.rs:60:9
84+
--> $DIR/redundant_pub_crate.rs:62:9
9485
|
9586
LL | pub(crate) fn g() {} // private due to m3_1
96-
| ^^^^^^^^^^^^^^^^^^^^
97-
|
98-
= help: consider using `pub` instead of `pub(crate)`
87+
| ----------^^^^^
88+
| |
89+
| help: consider using: `pub`
9990

10091
error: pub(crate) function inside private module
101-
--> $DIR/redundant_pub_crate.rs:67:9
92+
--> $DIR/redundant_pub_crate.rs:69:9
10293
|
10394
LL | pub(crate) fn g() {} // already crate visible due to m3_2
104-
| ^^^^^^^^^^^^^^^^^^^^
105-
|
106-
= help: consider using `pub` instead of `pub(crate)`
95+
| ----------^^^^^
96+
| |
97+
| help: consider using: `pub`
10798

10899
error: pub(crate) function inside private module
109-
--> $DIR/redundant_pub_crate.rs:80:5
100+
--> $DIR/redundant_pub_crate.rs:82:5
110101
|
111102
LL | pub(crate) fn g() {} // private: not re-exported by `pub use m4::*`
112-
| ^^^^^^^^^^^^^^^^^^^^
113-
|
114-
= help: consider using `pub` instead of `pub(crate)`
103+
| ----------^^^^^
104+
| |
105+
| help: consider using: `pub`
115106

116107
error: pub(crate) function inside private module
117-
--> $DIR/redundant_pub_crate.rs:85:9
108+
--> $DIR/redundant_pub_crate.rs:87:9
118109
|
119110
LL | pub(crate) fn g() {} // private due to m4_1
120-
| ^^^^^^^^^^^^^^^^^^^^
121-
|
122-
= help: consider using `pub` instead of `pub(crate)`
111+
| ----------^^^^^
112+
| |
113+
| help: consider using: `pub`
123114

124115
error: pub(crate) module inside private module
125-
--> $DIR/redundant_pub_crate.rs:89:5
126-
|
127-
LL | / pub(crate) mod m4_2 {
128-
LL | | // ^ private: not re-exported by `pub use m4::*`
129-
LL | | fn f() {}
130-
LL | | pub(crate) fn g() {} // private due to m4_2
131-
LL | | pub fn h() {}
132-
LL | | }
133-
| |_____^
116+
--> $DIR/redundant_pub_crate.rs:91:5
134117
|
135-
= help: consider using `pub` instead of `pub(crate)`
118+
LL | pub(crate) mod m4_2 {
119+
| ----------^^^^^^^^^
120+
| |
121+
| help: consider using: `pub`
136122

137123
error: pub(crate) function inside private module
138-
--> $DIR/redundant_pub_crate.rs:92:9
124+
--> $DIR/redundant_pub_crate.rs:94:9
139125
|
140126
LL | pub(crate) fn g() {} // private due to m4_2
141-
| ^^^^^^^^^^^^^^^^^^^^
142-
|
143-
= help: consider using `pub` instead of `pub(crate)`
127+
| ----------^^^^^
128+
| |
129+
| help: consider using: `pub`
144130

145131
error: aborting due to 16 previous errors
146132

0 commit comments

Comments
 (0)