Skip to content

Commit 423a712

Browse files
committed
Fix lints.
1 parent daccecc commit 423a712

File tree

8 files changed

+79
-96
lines changed

8 files changed

+79
-96
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,6 @@ declare_lint! {
486486
pub struct MissingDoc {
487487
/// Stack of whether `#[doc(hidden)]` is set at each level which has lint attributes.
488488
doc_hidden_stack: Vec<bool>,
489-
490-
/// Private traits or trait items that leaked through. Don't check their methods.
491-
private_traits: FxHashSet<hir::HirId>,
492489
}
493490

494491
impl_lint_pass!(MissingDoc => [MISSING_DOCS]);
@@ -519,7 +516,7 @@ fn has_doc(attr: &ast::Attribute) -> bool {
519516

520517
impl MissingDoc {
521518
pub fn new() -> MissingDoc {
522-
MissingDoc { doc_hidden_stack: vec![false], private_traits: FxHashSet::default() }
519+
MissingDoc { doc_hidden_stack: vec![false] }
523520
}
524521

525522
fn doc_hidden(&self) -> bool {
@@ -597,36 +594,16 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
597594

598595
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
599596
match it.kind {
600-
hir::ItemKind::Trait(.., trait_item_refs) => {
597+
hir::ItemKind::Trait(..) => {
601598
// Issue #11592: traits are always considered exported, even when private.
602599
if cx.tcx.visibility(it.def_id)
603600
== ty::Visibility::Restricted(
604601
cx.tcx.parent_module_from_def_id(it.def_id).to_def_id(),
605602
)
606603
{
607-
self.private_traits.insert(it.hir_id());
608-
for trait_item_ref in trait_item_refs {
609-
self.private_traits.insert(trait_item_ref.id.hir_id());
610-
}
611604
return;
612605
}
613606
}
614-
hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), items, .. }) => {
615-
// If the trait is private, add the impl items to `private_traits` so they don't get
616-
// reported for missing docs.
617-
let real_trait = trait_ref.path.res.def_id();
618-
let Some(def_id) = real_trait.as_local() else { return };
619-
if cx.tcx.visibility(def_id)
620-
== ty::Visibility::Restricted(
621-
cx.tcx.parent_module_from_def_id(it.def_id).to_def_id(),
622-
)
623-
{
624-
for impl_item_ref in items {
625-
self.private_traits.insert(impl_item_ref.id.hir_id());
626-
}
627-
}
628-
return;
629-
}
630607
hir::ItemKind::TyAlias(..)
631608
| hir::ItemKind::Fn(..)
632609
| hir::ItemKind::Macro(..)
@@ -646,10 +623,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
646623
}
647624

648625
fn check_trait_item(&mut self, cx: &LateContext<'_>, trait_item: &hir::TraitItem<'_>) {
649-
if self.private_traits.contains(&trait_item.hir_id()) {
650-
return;
651-
}
652-
653626
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
654627

655628
self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
@@ -1389,15 +1362,16 @@ impl UnreachablePub {
13891362
cx: &LateContext<'_>,
13901363
what: &str,
13911364
def_id: LocalDefId,
1365+
span: Span,
13921366
vis_span: Span,
13931367
exportable: bool,
13941368
) {
13951369
let mut applicability = Applicability::MachineApplicable;
1396-
if !cx.access_levels.is_reachable(def_id) {
1370+
if cx.tcx.visibility(def_id).is_public() && !cx.access_levels.is_reachable(def_id) {
13971371
if vis_span.from_expansion() {
13981372
applicability = Applicability::MaybeIncorrect;
13991373
}
1400-
let def_span = cx.tcx.def_span(def_id);
1374+
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
14011375
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
14021376
let mut err = lint.build(&format!("unreachable `pub` {}", what));
14031377
let replacement = if cx.tcx.features().crate_visibility_modifier {
@@ -1424,27 +1398,40 @@ impl UnreachablePub {
14241398

14251399
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14261400
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
1427-
if cx.tcx.visibility(item.def_id).is_public() {
1428-
self.perform_lint(cx, "item", item.def_id, item.vis_span, true);
1401+
// Do not warn for fake `use` statements.
1402+
if let hir::ItemKind::Use(_, hir::UseKind::ListStem) = &item.kind {
1403+
return;
14291404
}
1405+
self.perform_lint(cx, "item", item.def_id, item.span, item.vis_span, true);
14301406
}
14311407

14321408
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
1433-
if cx.tcx.visibility(foreign_item.def_id).is_public() {
1434-
self.perform_lint(cx, "item", foreign_item.def_id, foreign_item.vis_span, true);
1435-
}
1409+
self.perform_lint(
1410+
cx,
1411+
"item",
1412+
foreign_item.def_id,
1413+
foreign_item.span,
1414+
foreign_item.vis_span,
1415+
true,
1416+
);
14361417
}
14371418

14381419
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
14391420
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1440-
if cx.tcx.visibility(def_id).is_public() {
1441-
self.perform_lint(cx, "field", def_id, field.vis_span, false);
1442-
}
1421+
self.perform_lint(cx, "field", def_id, field.span, field.vis_span, false);
14431422
}
14441423

14451424
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
1446-
if cx.tcx.visibility(impl_item.def_id).is_public() {
1447-
self.perform_lint(cx, "item", impl_item.def_id, impl_item.vis_span, false);
1425+
// Only lint inherent impl items.
1426+
if cx.tcx.associated_item(impl_item.def_id).trait_item_def_id.is_none() {
1427+
self.perform_lint(
1428+
cx,
1429+
"item",
1430+
impl_item.def_id,
1431+
impl_item.span,
1432+
impl_item.vis_span,
1433+
false,
1434+
);
14481435
}
14491436
}
14501437
}

compiler/rustc_passes/src/dead.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ struct MarkSymbolVisitor<'tcx> {
4545
live_symbols: FxHashSet<LocalDefId>,
4646
repr_has_repr_c: bool,
4747
in_pat: bool,
48-
inherited_pub_visibility: bool,
49-
pub_visibility: bool,
5048
ignore_variant_stack: Vec<DefId>,
5149
// maps from tuple struct constructors to tuple struct items
5250
struct_constructors: FxHashMap<LocalDefId, LocalDefId>,
@@ -284,33 +282,23 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
284282
}
285283

286284
let had_repr_c = self.repr_has_repr_c;
287-
let had_inherited_pub_visibility = self.inherited_pub_visibility;
288-
let had_pub_visibility = self.pub_visibility;
289285
self.repr_has_repr_c = false;
290-
self.inherited_pub_visibility = false;
291-
self.pub_visibility = false;
292286
match node {
293-
Node::Item(item) => {
294-
self.pub_visibility = self.tcx.visibility(item.def_id).is_public();
287+
Node::Item(item) => match item.kind {
288+
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
289+
let def = self.tcx.adt_def(item.def_id);
290+
self.repr_has_repr_c = def.repr().c();
295291

296-
match item.kind {
297-
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
298-
let def = self.tcx.adt_def(item.def_id);
299-
self.repr_has_repr_c = def.repr().c();
300-
301-
intravisit::walk_item(self, &item);
302-
}
303-
hir::ItemKind::Enum(..) => {
304-
self.inherited_pub_visibility = self.pub_visibility;
305-
306-
intravisit::walk_item(self, &item);
307-
}
308-
hir::ItemKind::ForeignMod { .. } => {}
309-
_ => {
310-
intravisit::walk_item(self, &item);
311-
}
292+
intravisit::walk_item(self, &item);
312293
}
313-
}
294+
hir::ItemKind::Enum(..) => {
295+
intravisit::walk_item(self, &item);
296+
}
297+
hir::ItemKind::ForeignMod { .. } => {}
298+
_ => {
299+
intravisit::walk_item(self, &item);
300+
}
301+
},
314302
Node::TraitItem(trait_item) => {
315303
intravisit::walk_trait_item(self, trait_item);
316304
}
@@ -322,8 +310,6 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
322310
}
323311
_ => {}
324312
}
325-
self.pub_visibility = had_pub_visibility;
326-
self.inherited_pub_visibility = had_inherited_pub_visibility;
327313
self.repr_has_repr_c = had_repr_c;
328314
}
329315

@@ -356,19 +342,14 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
356342
) {
357343
let tcx = self.tcx;
358344
let has_repr_c = self.repr_has_repr_c;
359-
let inherited_pub_visibility = self.inherited_pub_visibility;
360-
let pub_visibility = self.pub_visibility;
361345
let live_fields = def.fields().iter().filter_map(|f| {
362346
let def_id = tcx.hir().local_def_id(f.hir_id);
363347
if has_repr_c {
364348
return Some(def_id);
365349
}
366-
if !pub_visibility {
350+
if !tcx.visibility(f.hir_id.owner).is_public() {
367351
return None;
368352
}
369-
if inherited_pub_visibility {
370-
return Some(def_id);
371-
}
372353
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
373354
});
374355
self.live_symbols.extend(live_fields);
@@ -612,8 +593,6 @@ fn live_symbols_and_ignored_derived_traits<'tcx>(
612593
live_symbols: Default::default(),
613594
repr_has_repr_c: false,
614595
in_pat: false,
615-
inherited_pub_visibility: false,
616-
pub_visibility: false,
617596
ignore_variant_stack: vec![],
618597
struct_constructors,
619598
ignored_derived_traits: FxHashMap::default(),

compiler/rustc_privacy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility {
19821982
// Visibility on them should have no effect, but to avoid the visibility
19831983
// query failing on some items, we provide it for opaque types as well.
19841984
| Node::Item(hir::Item {
1985-
kind: hir::ItemKind::Use(..) | hir::ItemKind::OpaqueTy(..),
1985+
kind: hir::ItemKind::Use(_, hir::UseKind::ListStem) | hir::ItemKind::OpaqueTy(..),
19861986
..
19871987
}) => ty::Visibility::Restricted(tcx.parent_module(hir_id).to_def_id()),
19881988
// Visibilities of trait impl items are inherited from their traits

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
458458
let mut source = module_path.pop().unwrap();
459459
let mut type_ns_only = false;
460460

461+
self.r.visibilities.insert(self.r.local_def_id(id), vis);
462+
if id1 != ast::DUMMY_NODE_ID {
463+
self.r.visibilities.insert(self.r.local_def_id(id1), vis);
464+
}
465+
if id2 != ast::DUMMY_NODE_ID {
466+
self.r.visibilities.insert(self.r.local_def_id(id2), vis);
467+
}
468+
461469
if nested {
462470
// Correctly handle `self`
463471
if source.ident.name == kw::SelfLower {
@@ -564,7 +572,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
564572
additional_ids: (id1, id2),
565573
};
566574

567-
self.r.visibilities.insert(self.r.local_def_id(id), vis);
568575
self.add_import(
569576
module_path,
570577
kind,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ mod private_mod {
2626
pub fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub
2727
pub(crate) fn count_electrons(&self) -> usize { self.electrons }
2828
}
29+
impl Clone for Hydrogen {
30+
fn clone(&self) -> Hydrogen {
31+
Hydrogen { neutrons: self.neutrons, electrons: self.electrons }
32+
}
33+
}
2934

3035
pub enum Helium {} //~ WARNING unreachable_pub
3136
pub union Lithium { c1: usize, c2: u8 } //~ WARNING unreachable_pub

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LL | pub fn count_neutrons(&self) -> usize { self.neutrons }
5050
| help: consider restricting its visibility: `pub(crate)`
5151

5252
warning: unreachable `pub` item
53-
--> $DIR/unreachable_pub-pub_crate.rs:30:5
53+
--> $DIR/unreachable_pub-pub_crate.rs:35:5
5454
|
5555
LL | pub enum Helium {}
5656
| ---^^^^^^^^^^^^
@@ -60,7 +60,7 @@ LL | pub enum Helium {}
6060
= help: or consider exporting it for use by other crates
6161

6262
warning: unreachable `pub` item
63-
--> $DIR/unreachable_pub-pub_crate.rs:31:5
63+
--> $DIR/unreachable_pub-pub_crate.rs:36:5
6464
|
6565
LL | pub union Lithium { c1: usize, c2: u8 }
6666
| ---^^^^^^^^^^^^^^
@@ -70,7 +70,7 @@ LL | pub union Lithium { c1: usize, c2: u8 }
7070
= help: or consider exporting it for use by other crates
7171

7272
warning: unreachable `pub` item
73-
--> $DIR/unreachable_pub-pub_crate.rs:32:5
73+
--> $DIR/unreachable_pub-pub_crate.rs:37:5
7474
|
7575
LL | pub fn beryllium() {}
7676
| ---^^^^^^^^^^^^^^^
@@ -80,7 +80,7 @@ LL | pub fn beryllium() {}
8080
= help: or consider exporting it for use by other crates
8181

8282
warning: unreachable `pub` item
83-
--> $DIR/unreachable_pub-pub_crate.rs:33:5
83+
--> $DIR/unreachable_pub-pub_crate.rs:38:5
8484
|
8585
LL | pub trait Boron {}
8686
| ---^^^^^^^^^^^^
@@ -90,7 +90,7 @@ LL | pub trait Boron {}
9090
= help: or consider exporting it for use by other crates
9191

9292
warning: unreachable `pub` item
93-
--> $DIR/unreachable_pub-pub_crate.rs:34:5
93+
--> $DIR/unreachable_pub-pub_crate.rs:39:5
9494
|
9595
LL | pub const CARBON: usize = 1;
9696
| ---^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -100,7 +100,7 @@ LL | pub const CARBON: usize = 1;
100100
= help: or consider exporting it for use by other crates
101101

102102
warning: unreachable `pub` item
103-
--> $DIR/unreachable_pub-pub_crate.rs:35:5
103+
--> $DIR/unreachable_pub-pub_crate.rs:40:5
104104
|
105105
LL | pub static NITROGEN: usize = 2;
106106
| ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -110,7 +110,7 @@ LL | pub static NITROGEN: usize = 2;
110110
= help: or consider exporting it for use by other crates
111111

112112
warning: unreachable `pub` item
113-
--> $DIR/unreachable_pub-pub_crate.rs:36:5
113+
--> $DIR/unreachable_pub-pub_crate.rs:41:5
114114
|
115115
LL | pub type Oxygen = bool;
116116
| ---^^^^^^^^^^^^^^^^^^^^
@@ -120,7 +120,7 @@ LL | pub type Oxygen = bool;
120120
= help: or consider exporting it for use by other crates
121121

122122
warning: unreachable `pub` item
123-
--> $DIR/unreachable_pub-pub_crate.rs:39:47
123+
--> $DIR/unreachable_pub-pub_crate.rs:44:47
124124
|
125125
LL | ($visibility: vis, $name: ident) => { $visibility struct $name {} }
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -135,7 +135,7 @@ LL | define_empty_struct_with_visibility!(pub, Fluorine);
135135
= note: this warning originates in the macro `define_empty_struct_with_visibility` (in Nightly builds, run with -Z macro-backtrace for more info)
136136

137137
warning: unreachable `pub` item
138-
--> $DIR/unreachable_pub-pub_crate.rs:45:9
138+
--> $DIR/unreachable_pub-pub_crate.rs:50:9
139139
|
140140
LL | pub fn catalyze() -> bool;
141141
| ---^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/lint/unreachable_pub.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ mod private_mod {
2222
pub fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub
2323
crate fn count_electrons(&self) -> usize { self.electrons }
2424
}
25+
impl Clone for Hydrogen {
26+
fn clone(&self) -> Hydrogen {
27+
Hydrogen { neutrons: self.neutrons, electrons: self.electrons }
28+
}
29+
}
2530

2631
pub enum Helium {} //~ WARNING unreachable_pub
2732
pub union Lithium { c1: usize, c2: u8 } //~ WARNING unreachable_pub

0 commit comments

Comments
 (0)