Skip to content

Commit 29d97cd

Browse files
committed
Make some fns return fmt::Result to get rid of a few unwraps
1 parent 28174fc commit 29d97cd

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'tcx> Context<'tcx> {
251251
&self.shared.layout,
252252
&page,
253253
BufDisplay(|buf: &mut String| {
254-
print_sidebar(self, it, buf);
254+
print_sidebar(self, it, buf).unwrap();
255255
}),
256256
content,
257257
&self.shared.style_files,

src/librustdoc/html/render/mod.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ fn document_full_inner(
661661
};
662662

663663
if let clean::ItemKind::FunctionItem(..) | clean::ItemKind::MethodItem(..) = kind {
664-
render_call_locations(f, cx, item);
664+
render_call_locations(f, cx, item)?;
665665
}
666666
Ok(())
667667
})
@@ -2584,11 +2584,15 @@ const MAX_FULL_EXAMPLES: usize = 5;
25842584
const NUM_VISIBLE_LINES: usize = 10;
25852585

25862586
/// Generates the HTML for example call locations generated via the --scrape-examples flag.
2587-
fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean::Item) {
2587+
fn render_call_locations<W: fmt::Write>(
2588+
mut w: W,
2589+
cx: &Context<'_>,
2590+
item: &clean::Item,
2591+
) -> fmt::Result {
25882592
let tcx = cx.tcx();
25892593
let def_id = item.item_id.expect_def_id();
25902594
let key = tcx.def_path_hash(def_id);
2591-
let Some(call_locations) = cx.shared.call_locations.get(&key) else { return };
2595+
let Some(call_locations) = cx.shared.call_locations.get(&key) else { return Ok(()) };
25922596

25932597
// Generate a unique ID so users can link to this section for a given method
25942598
let id = cx.derive_id("scraped-examples");
@@ -2602,8 +2606,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
26022606
</h5>",
26032607
root_path = cx.root_path(),
26042608
id = id
2605-
)
2606-
.unwrap();
2609+
)?;
26072610

26082611
// Create a URL to a particular location in a reverse-dependency's source file
26092612
let link_to_loc = |call_data: &CallData, loc: &CallLocation| -> (String, String) {
@@ -2705,7 +2708,8 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27052708
title: init_title,
27062709
locations: locations_encoded,
27072710
}),
2708-
);
2711+
)
2712+
.unwrap();
27092713

27102714
true
27112715
};
@@ -2761,8 +2765,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27612765
<div class=\"hide-more\">Hide additional examples</div>\
27622766
<div class=\"more-scraped-examples\">\
27632767
<div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>"
2764-
)
2765-
.unwrap();
2768+
)?;
27662769

27672770
// Only generate inline code for MAX_FULL_EXAMPLES number of examples. Otherwise we could
27682771
// make the page arbitrarily huge!
@@ -2774,23 +2777,21 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &Context<'_>, item: &clean
27742777
if it.peek().is_some() {
27752778
w.write_str(
27762779
r#"<div class="example-links">Additional examples can be found in:<br><ul>"#,
2777-
)
2778-
.unwrap();
2779-
it.for_each(|(_, call_data)| {
2780+
)?;
2781+
it.try_for_each(|(_, call_data)| {
27802782
let (url, _) = link_to_loc(call_data, &call_data.locations[0]);
27812783
write!(
27822784
w,
27832785
r#"<li><a href="{url}">{name}</a></li>"#,
27842786
url = url,
27852787
name = call_data.display_name
27862788
)
2787-
.unwrap();
2788-
});
2789-
w.write_str("</ul></div>").unwrap();
2789+
})?;
2790+
w.write_str("</ul></div>")?;
27902791
}
27912792

2792-
w.write_str("</div></details>").unwrap();
2793+
w.write_str("</div></details>")?;
27932794
}
27942795

2795-
w.write_str("</div>").unwrap();
2796+
w.write_str("</div>")
27962797
}

src/librustdoc/html/render/sidebar.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::cmp::Ordering;
3+
use std::fmt;
34

45
use askama::Template;
56
use rustc_data_structures::fx::FxHashSet;
@@ -135,7 +136,11 @@ pub(crate) mod filters {
135136
}
136137
}
137138

138-
pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut String) {
139+
pub(super) fn print_sidebar(
140+
cx: &Context<'_>,
141+
it: &clean::Item,
142+
mut buffer: impl fmt::Write,
143+
) -> fmt::Result {
139144
let mut ids = IdMap::new();
140145
let mut blocks: Vec<LinkBlock<'_>> = docblock_toc(cx, it, &mut ids).into_iter().collect();
141146
let deref_id_map = cx.deref_id_map.borrow();
@@ -195,7 +200,8 @@ pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Str
195200
blocks,
196201
path,
197202
};
198-
sidebar.render_into(buffer).unwrap();
203+
sidebar.render_into(&mut buffer)?;
204+
Ok(())
199205
}
200206

201207
fn get_struct_fields_name<'a>(fields: &'a [clean::Item]) -> Vec<Link<'a>> {

src/librustdoc/html/sources.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ impl SourceCollector<'_, '_> {
252252
&root_path,
253253
&highlight::DecorationInfo::default(),
254254
&source_context,
255-
);
255+
)
256+
.unwrap();
256257
}),
257258
&shared.style_files,
258259
);
@@ -331,7 +332,7 @@ pub(crate) fn print_src(
331332
root_path: &str,
332333
decoration_info: &highlight::DecorationInfo,
333334
source_context: &SourceContext<'_>,
334-
) {
335+
) -> fmt::Result {
335336
let mut lines = s.lines().count();
336337
let line_info = if let SourceContext::Embedded(info) = source_context {
337338
highlight::LineInfo::new_scraped(lines as u32, info.offset as u32)
@@ -367,12 +368,10 @@ pub(crate) fn print_src(
367368
},
368369
max_nb_digits,
369370
}
370-
.render_into(&mut writer)
371-
.unwrap(),
371+
.render_into(&mut writer),
372372
SourceContext::Embedded(info) => {
373-
ScrapedSource { info, code_html: code, max_nb_digits }
374-
.render_into(&mut writer)
375-
.unwrap();
373+
ScrapedSource { info, code_html: code, max_nb_digits }.render_into(&mut writer)
376374
}
377-
};
375+
}?;
376+
Ok(())
378377
}

0 commit comments

Comments
 (0)