Skip to content

Commit ec349be

Browse files
Unwrap Visibility fields
There's not really any reason to not have the visibility default to inherited, and this saves us the trouble of checking everywhere for whether we have a visibility or not.
1 parent 04b27ef commit ec349be

File tree

7 files changed

+27
-32
lines changed

7 files changed

+27
-32
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
119119
source: Span::empty(),
120120
name: None,
121121
attrs: Default::default(),
122-
visibility: None,
122+
visibility: Inherited,
123123
def_id: self.cx.next_def_id(param_env_def_id.krate),
124124
stability: None,
125125
deprecation: None,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
9999
source: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
100100
name: None,
101101
attrs: Default::default(),
102-
visibility: None,
102+
visibility: Inherited,
103103
def_id: self.cx.next_def_id(impl_def_id.krate),
104104
stability: None,
105105
deprecation: None,

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn try_inline(
131131
name: Some(name.clean(cx)),
132132
attrs,
133133
inner,
134-
visibility: Some(clean::Public),
134+
visibility: clean::Public,
135135
stability: cx.tcx.lookup_stability(did).clean(cx),
136136
deprecation: cx.tcx.lookup_deprecation(did).clean(cx),
137137
def_id: did,
@@ -418,7 +418,7 @@ pub fn build_impl(cx: &DocContext<'_>, did: DefId, attrs: Option<Attrs<'_>>,
418418
source: tcx.def_span(did).clean(cx),
419419
name: None,
420420
attrs,
421-
visibility: Some(clean::Inherited),
421+
visibility: clean::Inherited,
422422
stability: tcx.lookup_stability(did).clean(cx),
423423
deprecation: tcx.lookup_deprecation(did).clean(cx),
424424
def_id: did,

src/librustdoc/clean/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn krate(mut cx: &mut DocContext<'_>) -> Crate {
187187
source: Span::empty(),
188188
name: Some(prim.to_url_str().to_string()),
189189
attrs: attrs.clone(),
190-
visibility: Some(Public),
190+
visibility: Public,
191191
stability: get_stability(cx, def_id),
192192
deprecation: get_deprecation(cx, def_id),
193193
def_id,
@@ -199,7 +199,7 @@ pub fn krate(mut cx: &mut DocContext<'_>) -> Crate {
199199
source: Span::empty(),
200200
name: Some(kw.clone()),
201201
attrs: attrs,
202-
visibility: Some(Public),
202+
visibility: Public,
203203
stability: get_stability(cx, def_id),
204204
deprecation: get_deprecation(cx, def_id),
205205
def_id,
@@ -361,7 +361,7 @@ pub struct Item {
361361
pub name: Option<String>,
362362
pub attrs: Attributes,
363363
pub inner: ItemEnum,
364-
pub visibility: Option<Visibility>,
364+
pub visibility: Visibility,
365365
pub def_id: DefId,
366366
pub stability: Option<Stability>,
367367
pub deprecation: Option<Deprecation>,
@@ -2311,7 +2311,7 @@ impl Clean<Item> for hir::TraitItem {
23112311
attrs: self.attrs.clean(cx),
23122312
source: self.span.clean(cx),
23132313
def_id: local_did,
2314-
visibility: None,
2314+
visibility: Visibility::Inherited,
23152315
stability: get_stability(cx, local_did),
23162316
deprecation: get_deprecation(cx, local_did),
23172317
inner,
@@ -2496,7 +2496,7 @@ impl Clean<Item> for ty::AssocItem {
24962496

24972497
let visibility = match self.container {
24982498
ty::ImplContainer(_) => self.vis.clean(cx),
2499-
ty::TraitContainer(_) => None,
2499+
ty::TraitContainer(_) => Inherited,
25002500
};
25012501

25022502
Item {
@@ -3293,9 +3293,9 @@ pub enum Visibility {
32933293
Restricted(DefId, Path),
32943294
}
32953295

3296-
impl Clean<Option<Visibility>> for hir::Visibility {
3297-
fn clean(&self, cx: &DocContext<'_>) -> Option<Visibility> {
3298-
Some(match self.node {
3296+
impl Clean<Visibility> for hir::Visibility {
3297+
fn clean(&self, cx: &DocContext<'_>) -> Visibility {
3298+
match self.node {
32993299
hir::VisibilityKind::Public => Visibility::Public,
33003300
hir::VisibilityKind::Inherited => Visibility::Inherited,
33013301
hir::VisibilityKind::Crate(_) => Visibility::Crate,
@@ -3304,13 +3304,13 @@ impl Clean<Option<Visibility>> for hir::Visibility {
33043304
let did = register_res(cx, path.res);
33053305
Visibility::Restricted(did, path)
33063306
}
3307-
})
3307+
}
33083308
}
33093309
}
33103310

3311-
impl Clean<Option<Visibility>> for ty::Visibility {
3312-
fn clean(&self, _: &DocContext<'_>) -> Option<Visibility> {
3313-
Some(if *self == ty::Visibility::Public { Public } else { Inherited })
3311+
impl Clean<Visibility> for ty::Visibility {
3312+
fn clean(&self, _: &DocContext<'_>) -> Visibility {
3313+
if *self == ty::Visibility::Public { Public } else { Inherited }
33143314
}
33153315
}
33163316

@@ -3427,7 +3427,7 @@ impl Clean<Item> for doctree::Variant<'_> {
34273427
name: Some(self.name.clean(cx)),
34283428
attrs: self.attrs.clean(cx),
34293429
source: self.whence.clean(cx),
3430-
visibility: None,
3430+
visibility: Inherited,
34313431
stability: cx.stability(self.id).clean(cx),
34323432
deprecation: cx.deprecation(self.id).clean(cx),
34333433
def_id: cx.tcx.hir().local_def_id(self.id),
@@ -3470,7 +3470,7 @@ impl Clean<Item> for ty::VariantDef {
34703470
name: Some(self.ident.clean(cx)),
34713471
attrs: inline::load_attrs(cx, self.def_id).clean(cx),
34723472
source: cx.tcx.def_span(self.def_id).clean(cx),
3473-
visibility: Some(Inherited),
3473+
visibility: Inherited,
34743474
def_id: self.def_id,
34753475
inner: VariantItem(Variant { kind }),
34763476
stability: get_stability(cx, self.def_id),
@@ -4333,7 +4333,7 @@ impl Clean<Item> for doctree::Macro<'_> {
43334333
name: Some(name.clone()),
43344334
attrs: self.attrs.clean(cx),
43354335
source: self.whence.clean(cx),
4336-
visibility: Some(Public),
4336+
visibility: Public,
43374337
stability: cx.stability(self.hid).clean(cx),
43384338
deprecation: cx.deprecation(self.hid).clean(cx),
43394339
def_id: self.def_id,
@@ -4361,7 +4361,7 @@ impl Clean<Item> for doctree::ProcMacro<'_> {
43614361
name: Some(self.name.clean(cx)),
43624362
attrs: self.attrs.clean(cx),
43634363
source: self.whence.clean(cx),
4364-
visibility: Some(Public),
4364+
visibility: Public,
43654365
stability: cx.stability(self.id).clean(cx),
43664366
deprecation: cx.deprecation(self.id).clean(cx),
43674367
def_id: cx.tcx.hir().local_def_id(self.id),

src/librustdoc/html/format.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Buffer {
111111
/// Helper to render an optional visibility with a space after it (if the
112112
/// visibility is preset)
113113
#[derive(Copy, Clone)]
114-
pub struct VisSpace<'a>(pub &'a Option<clean::Visibility>);
114+
pub struct VisSpace<'a>(pub &'a clean::Visibility);
115115
/// Similarly to VisSpace, this structure is used to render a function style with a
116116
/// space after it.
117117
#[derive(Copy, Clone)]
@@ -1034,11 +1034,7 @@ impl Function<'_> {
10341034

10351035
impl<'a> fmt::Display for VisSpace<'a> {
10361036
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1037-
if let Some(v) = self.0 {
1038-
fmt::Display::fmt(&v.print_with_space(), f)
1039-
} else {
1040-
Ok(())
1041-
}
1037+
fmt::Display::fmt(&self.0.print_with_space(), f)
10421038
}
10431039
}
10441040

src/librustdoc/html/render.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ impl DocFolder for Cache {
15111511
self.paths.insert(item.def_id, (stack, ItemType::Enum));
15121512
}
15131513

1514-
clean::PrimitiveItem(..) if item.visibility.is_some() => {
1514+
clean::PrimitiveItem(..) => {
15151515
self.add_aliases(&item);
15161516
self.paths.insert(item.def_id, (self.stack.clone(),
15171517
item.type_()));
@@ -4306,8 +4306,7 @@ fn get_methods(
43064306
) -> Vec<String> {
43074307
i.items.iter().filter_map(|item| {
43084308
match item.name {
4309-
// Maybe check with clean::Visibility::Public as well?
4310-
Some(ref name) if !name.is_empty() && item.visibility.is_some() && item.is_method() => {
4309+
Some(ref name) if !name.is_empty() && item.is_method() => {
43114310
if !for_deref || should_render_item(item, deref_mut) {
43124311
Some(format!("<a href=\"#{}\">{}</a>",
43134312
get_next_url(used_links, format!("method.{}", name)),

src/librustdoc/passes/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ impl<'a> DocFolder for Stripper<'a> {
185185
}
186186

187187
clean::StructFieldItem(..) => {
188-
if i.visibility != Some(clean::Public) {
188+
if i.visibility != clean::Public {
189189
return StripItem(i).strip();
190190
}
191191
}
192192

193193
clean::ModuleItem(..) => {
194-
if i.def_id.is_local() && i.visibility != Some(clean::Public) {
194+
if i.def_id.is_local() && i.visibility != clean::Public {
195195
debug!("Stripper: stripping module {:?}", i.name);
196196
let old = mem::replace(&mut self.update_retained, false);
197197
let ret = StripItem(self.fold_item_recur(i).unwrap()).strip();
@@ -299,7 +299,7 @@ impl DocFolder for ImportStripper {
299299
fn fold_item(&mut self, i: Item) -> Option<Item> {
300300
match i.inner {
301301
clean::ExternCrateItem(..) | clean::ImportItem(..)
302-
if i.visibility != Some(clean::Public) =>
302+
if i.visibility != clean::Public =>
303303
{
304304
None
305305
}

0 commit comments

Comments
 (0)