Skip to content

Commit 104985b

Browse files
committed
unreachable_pub lint: grab pub span from HIR rather than inferring it
This is a true fix for #50455, superior to the mere bandage offered in #50476.
1 parent 4ae8912 commit 104985b

File tree

3 files changed

+46
-47
lines changed

3 files changed

+46
-47
lines changed

src/librustc_lint/builtin.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -1386,60 +1386,53 @@ impl LintPass for UnreachablePub {
13861386

13871387
impl UnreachablePub {
13881388
fn perform_lint(&self, cx: &LateContext, what: &str, id: ast::NodeId,
1389-
vis: &hir::Visibility, span: Span, exportable: bool,
1390-
mut applicability: Applicability) {
1391-
if !cx.access_levels.is_reachable(id) && vis.node.is_pub() {
1392-
if span.ctxt().outer().expn_info().is_some() {
1393-
applicability = Applicability::MaybeIncorrect;
1394-
}
1395-
let def_span = cx.tcx.sess.codemap().def_span(span);
1396-
let mut err = cx.struct_span_lint(UNREACHABLE_PUB, def_span,
1397-
&format!("unreachable `pub` {}", what));
1398-
// We are presuming that visibility is token at start of
1399-
// declaration (can be macro variable rather than literal `pub`)
1400-
let pub_span = cx.tcx.sess.codemap().span_until_char(def_span, ' ');
1401-
let replacement = if cx.tcx.features().crate_visibility_modifier {
1402-
"crate"
1403-
} else {
1404-
"pub(crate)"
1405-
}.to_owned();
1406-
err.span_suggestion_with_applicability(pub_span,
1407-
"consider restricting its visibility",
1408-
replacement,
1409-
applicability);
1410-
if exportable {
1411-
err.help("or consider exporting it for use by other crates");
1412-
}
1413-
err.emit();
1389+
vis: &hir::Visibility, span: Span, exportable: bool) {
1390+
let mut applicability = Applicability::MachineApplicable;
1391+
match vis.node {
1392+
hir::VisibilityPublic if !cx.access_levels.is_reachable(id) => {
1393+
if span.ctxt().outer().expn_info().is_some() {
1394+
applicability = Applicability::MaybeIncorrect;
1395+
}
1396+
let def_span = cx.tcx.sess.codemap().def_span(span);
1397+
let mut err = cx.struct_span_lint(UNREACHABLE_PUB, def_span,
1398+
&format!("unreachable `pub` {}", what));
1399+
let replacement = if cx.tcx.features().crate_visibility_modifier {
1400+
"crate"
1401+
} else {
1402+
"pub(crate)"
1403+
}.to_owned();
1404+
1405+
err.span_suggestion_with_applicability(vis.span,
1406+
"consider restricting its visibility",
1407+
replacement,
1408+
applicability);
1409+
if exportable {
1410+
err.help("or consider exporting it for use by other crates");
1411+
}
1412+
err.emit();
1413+
},
1414+
_ => {}
14141415
}
14151416
}
14161417
}
14171418

14181419

14191420
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnreachablePub {
14201421
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
1421-
let applicability = match item.node {
1422-
// suggestion span-manipulation is inadequate for `pub use
1423-
// module::{item}` (Issue #50455)
1424-
hir::ItemUse(..) => Applicability::MaybeIncorrect,
1425-
_ => Applicability::MachineApplicable,
1426-
};
1427-
self.perform_lint(cx, "item", item.id, &item.vis, item.span, true, applicability);
1422+
self.perform_lint(cx, "item", item.id, &item.vis, item.span, true);
14281423
}
14291424

14301425
fn check_foreign_item(&mut self, cx: &LateContext, foreign_item: &hir::ForeignItem) {
14311426
self.perform_lint(cx, "item", foreign_item.id, &foreign_item.vis,
1432-
foreign_item.span, true, Applicability::MachineApplicable);
1427+
foreign_item.span, true);
14331428
}
14341429

14351430
fn check_struct_field(&mut self, cx: &LateContext, field: &hir::StructField) {
1436-
self.perform_lint(cx, "field", field.id, &field.vis, field.span, false,
1437-
Applicability::MachineApplicable);
1431+
self.perform_lint(cx, "field", field.id, &field.vis, field.span, false);
14381432
}
14391433

14401434
fn check_impl_item(&mut self, cx: &LateContext, impl_item: &hir::ImplItem) {
1441-
self.perform_lint(cx, "item", impl_item.id, &impl_item.vis, impl_item.span, false,
1442-
Applicability::MachineApplicable);
1435+
self.perform_lint(cx, "item", impl_item.id, &impl_item.vis, impl_item.span, false);
14431436
}
14441437
}
14451438

src/test/ui/lint/unreachable_pub-pub_crate.stderr

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ warning: unreachable `pub` item
1717
--> $DIR/unreachable_pub-pub_crate.rs:27:24
1818
|
1919
LL | pub use std::env::{Args}; // braced-use has different item spans than unbraced
20-
| ^^^^ help: consider restricting its visibility: `pub(crate)`
20+
| --- ^^^^
21+
| |
22+
| help: consider restricting its visibility: `pub(crate)`
2123
|
2224
= help: or consider exporting it for use by other crates
2325

@@ -121,12 +123,13 @@ warning: unreachable `pub` item
121123
--> $DIR/unreachable_pub-pub_crate.rs:50:47
122124
|
123125
LL | ($visibility: vis, $name: ident) => { $visibility struct $name {} }
124-
| -----------^^^^^^^^^^^^^
125-
| |
126-
| help: consider restricting its visibility: `pub(crate)`
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^
127127
LL | }
128128
LL | define_empty_struct_with_visibility!(pub, Fluorine);
129-
| ---------------------------------------------------- in this macro invocation
129+
| ----------------------------------------------------
130+
| | |
131+
| | help: consider restricting its visibility: `pub(crate)`
132+
| in this macro invocation
130133
|
131134
= help: or consider exporting it for use by other crates
132135

src/test/ui/lint/unreachable_pub.stderr

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ warning: unreachable `pub` item
1717
--> $DIR/unreachable_pub.rs:22:24
1818
|
1919
LL | pub use std::env::{Args}; // braced-use has different item spans than unbraced
20-
| ^^^^ help: consider restricting its visibility: `crate`
20+
| --- ^^^^
21+
| |
22+
| help: consider restricting its visibility: `crate`
2123
|
2224
= help: or consider exporting it for use by other crates
2325

@@ -121,12 +123,13 @@ warning: unreachable `pub` item
121123
--> $DIR/unreachable_pub.rs:45:47
122124
|
123125
LL | ($visibility: vis, $name: ident) => { $visibility struct $name {} }
124-
| -----------^^^^^^^^^^^^^
125-
| |
126-
| help: consider restricting its visibility: `crate`
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^
127127
LL | }
128128
LL | define_empty_struct_with_visibility!(pub, Fluorine);
129-
| ---------------------------------------------------- in this macro invocation
129+
| ----------------------------------------------------
130+
| | |
131+
| | help: consider restricting its visibility: `crate`
132+
| in this macro invocation
130133
|
131134
= help: or consider exporting it for use by other crates
132135

0 commit comments

Comments
 (0)