Skip to content

Commit 4ba1928

Browse files
Improve code for DocFragment rework
1 parent 0ab6c90 commit 4ba1928

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/librustdoc/clean/types.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ crate enum DocFragmentKind {
486486
Include { filename: Symbol },
487487
}
488488

489+
// The goal of this function is to apply the `DocFragment` transformations that are required when
490+
// transforming into the final markdown. So the transformations in here are:
491+
//
492+
// * Applying the computed indent to each lines in each doc fragment (a `DocFragment` can contain
493+
// multiple lines in case of `#[doc = ""]`).
494+
// * Adding backlines between `DocFragment`s and adding an extra one if required (stored in the
495+
// `need_backline` field).
489496
fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
490497
let s = frag.doc.as_str();
491498
let mut iter = s.lines().peekable();
@@ -792,11 +799,14 @@ impl Attributes {
792799
if out.is_empty() { None } else { Some(out) }
793800
}
794801

802+
/// Return the doc-comments on this item, grouped by the module they came from.
803+
///
804+
/// The module can be different if this is a re-export with added documentation.
795805
crate fn collapsed_doc_value_by_module_level(&self) -> FxHashMap<Option<DefId>, String> {
796806
let mut ret = FxHashMap::default();
797807

798808
for new_frag in self.doc_strings.iter() {
799-
let out = ret.entry(new_frag.parent_module).or_insert_with(|| String::new());
809+
let out = ret.entry(new_frag.parent_module).or_default();
800810
add_doc_fragment(out, &new_frag);
801811
}
802812
ret
@@ -805,8 +815,7 @@ impl Attributes {
805815
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
806816
/// with newlines.
807817
crate fn collapsed_doc_value(&self) -> Option<String> {
808-
let s: String = self.doc_strings.iter().collect();
809-
if s.is_empty() { None } else { Some(s) }
818+
if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) }
810819
}
811820

812821
/// Gets links as a vector

src/librustdoc/formats/cache.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,9 @@ impl DocFolder for Cache {
314314
ty: item.type_(),
315315
name: s.to_string(),
316316
path: path.join("::"),
317-
desc: item.doc_value().map_or_else(
318-
|| String::new(),
319-
|x| short_markdown_summary(&x.as_str()),
320-
),
317+
desc: item
318+
.doc_value()
319+
.map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
321320
parent,
322321
parent_idx: None,
323322
search_type: get_index_search_type(&item),

src/librustdoc/html/render/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ impl SharedContext<'_> {
198198
/// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the
199199
/// `collapsed_doc_value` of the given item.
200200
crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
201-
if self.collapsed {
202-
item.collapsed_doc_value().map(|s| s.to_string())
203-
} else {
204-
item.doc_value().map(|s| s.to_string())
205-
}
201+
if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
206202
}
207203
}
208204

@@ -2196,7 +2192,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
21962192
let stab = myitem.stability_class(cx.tcx());
21972193
let add = if stab.is_some() { " " } else { "" };
21982194

2199-
let doc_value = myitem.doc_value().unwrap_or_else(String::new);
2195+
let doc_value = myitem.doc_value().unwrap_or_default();
22002196
write!(
22012197
w,
22022198
"<tr class=\"{stab}{add}module-item\">\

src/librustdoc/passes/calculate_doc_coverage.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
235235
let mut tests = Tests { found_tests: 0 };
236236

237237
find_testable_code(
238-
&i.attrs
239-
.doc_strings
240-
.iter()
241-
.map(|d| d.doc.to_string())
242-
.collect::<Vec<_>>()
243-
.join("\n"),
238+
&i.attrs.collapsed_doc_value().unwrap_or_default(),
244239
&mut tests,
245240
ErrorCodes::No,
246241
false,

0 commit comments

Comments
 (0)