Skip to content

Commit 74204a0

Browse files
Rollup merge of #39740 - jimmycuadra:rustdoc-empty-stability, r=aturon
rustdoc: Only include a stability span if needed. This patch gets rid of the empty stability boxes in docs by only including the span that creates it when the item actually has a stability class. Here are images of the issue on `std::process::Output`: Before: <img width="340" alt="before" src="https://cloud.githubusercontent.com/assets/122457/22853638/ff88d1b2-f010-11e6-90d6-bf3d10e2fffa.png"> After: <img width="333" alt="after" src="https://cloud.githubusercontent.com/assets/122457/22853639/06bfe7cc-f011-11e6-9892-f0ea2cc6ec90.png"> This is my first non-trivial patch to Rust, so I'm sure some of my approach is not idiomatic. Let me know how you'd like me to adjust!
2 parents 0095ec2 + 1fa9dbc commit 74204a0

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,24 @@ impl Item {
327327
}
328328
}
329329

330-
pub fn stability_class(&self) -> String {
331-
self.stability.as_ref().map(|ref s| {
332-
let mut base = match s.level {
333-
stability::Unstable => "unstable".to_string(),
334-
stability::Stable => String::new(),
335-
};
330+
pub fn stability_class(&self) -> Option<String> {
331+
self.stability.as_ref().and_then(|ref s| {
332+
let mut classes = Vec::with_capacity(2);
333+
334+
if s.level == stability::Unstable {
335+
classes.push("unstable");
336+
}
337+
336338
if !s.deprecated_since.is_empty() {
337-
base.push_str(" deprecated");
339+
classes.push("deprecated");
338340
}
339-
base
340-
}).unwrap_or(String::new())
341+
342+
if classes.len() != 0 {
343+
Some(classes.join(" "))
344+
} else {
345+
None
346+
}
347+
})
341348
}
342349

343350
pub fn stable_since(&self) -> Option<&str> {

src/librustdoc/html/render.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
18271827
stab_docs = stab_docs,
18281828
docs = shorter(Some(&Markdown(doc_value).to_string())),
18291829
class = myitem.type_(),
1830-
stab = myitem.stability_class(),
1830+
stab = myitem.stability_class().unwrap_or("".to_string()),
18311831
unsafety_flag = unsafety_flag,
18321832
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
18331833
title_type = myitem.type_(),
@@ -2378,13 +2378,16 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
23782378
write!(w, "<span id='{id}' class='{item_type}'>
23792379
<span id='{ns_id}' class='invisible'>
23802380
<code>{name}: {ty}</code>
2381-
</span></span><span class='stab {stab}'></span>",
2381+
</span></span>",
23822382
item_type = ItemType::StructField,
23832383
id = id,
23842384
ns_id = ns_id,
2385-
stab = field.stability_class(),
23862385
name = field.name.as_ref().unwrap(),
23872386
ty = ty)?;
2387+
if let Some(stability_class) = field.stability_class() {
2388+
write!(w, "<span class='stab {stab}'></span>",
2389+
stab = stability_class)?;
2390+
}
23882391
document(w, cx, field)?;
23892392
}
23902393
}
@@ -2415,11 +2418,14 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
24152418
write!(w, "<h2 class='fields'>Fields</h2>")?;
24162419
for (field, ty) in fields {
24172420
write!(w, "<span id='{shortty}.{name}' class='{shortty}'><code>{name}: {ty}</code>
2418-
</span><span class='stab {stab}'></span>",
2421+
</span>",
24192422
shortty = ItemType::StructField,
2420-
stab = field.stability_class(),
24212423
name = field.name.as_ref().unwrap(),
24222424
ty = ty)?;
2425+
if let Some(stability_class) = field.stability_class() {
2426+
write!(w, "<span class='stab {stab}'></span>",
2427+
stab = stability_class)?;
2428+
}
24232429
document(w, cx, field)?;
24242430
}
24252431
}

0 commit comments

Comments
 (0)