Skip to content

Commit 5a6dda2

Browse files
Make more functions return fmt::Result and reduce number of .unwrap() calls
1 parent af17d59 commit 5a6dda2

File tree

6 files changed

+100
-98
lines changed

6 files changed

+100
-98
lines changed

src/librustdoc/html/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,9 +1252,9 @@ struct Indent(usize);
12521252

12531253
impl Display for Indent {
12541254
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1255-
(0..self.0).for_each(|_| {
1256-
f.write_char(' ').unwrap();
1257-
});
1255+
for _ in 0..self.0 {
1256+
f.write_char(' ')?;
1257+
}
12581258
Ok(())
12591259
}
12601260
}

src/librustdoc/html/length_limit.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! See [`HtmlWithLimit`].
22
3-
use std::fmt::Write;
3+
use std::fmt::{self, Write};
44
use std::ops::ControlFlow;
55

66
use crate::html::escape::Escape;
@@ -51,7 +51,7 @@ impl HtmlWithLimit {
5151
/// Finish using the buffer and get the written output.
5252
/// This function will close all unclosed tags for you.
5353
pub(super) fn finish(mut self) -> String {
54-
self.close_all_tags();
54+
self.close_all_tags().unwrap();
5555
self.buf
5656
}
5757

@@ -64,7 +64,7 @@ impl HtmlWithLimit {
6464
return ControlFlow::Break(());
6565
}
6666

67-
self.flush_queue();
67+
self.flush_queue().unwrap();
6868
write!(self.buf, "{}", Escape(text)).unwrap();
6969
self.len += text.len();
7070

@@ -84,32 +84,35 @@ impl HtmlWithLimit {
8484
}
8585

8686
/// Close the most recently opened HTML tag.
87-
pub(super) fn close_tag(&mut self) {
87+
pub(super) fn close_tag(&mut self) -> fmt::Result {
8888
if let Some(tag_name) = self.unclosed_tags.pop() {
8989
// Close the most recently opened tag.
90-
write!(self.buf, "</{tag_name}>").unwrap()
90+
write!(self.buf, "</{tag_name}>")?
9191
}
9292
// There are valid cases where `close_tag()` is called without
9393
// there being any tags to close. For example, this occurs when
9494
// a tag is opened after the length limit is exceeded;
9595
// `flush_queue()` will never be called, and thus, the tag will
9696
// not end up being added to `unclosed_tags`.
97+
Ok(())
9798
}
9899

99100
/// Write all queued tags and add them to the `unclosed_tags` list.
100-
fn flush_queue(&mut self) {
101+
fn flush_queue(&mut self) -> fmt::Result {
101102
for tag_name in self.queued_tags.drain(..) {
102-
write!(self.buf, "<{tag_name}>").unwrap();
103+
write!(self.buf, "<{tag_name}>")?;
103104

104105
self.unclosed_tags.push(tag_name);
105106
}
107+
Ok(())
106108
}
107109

108110
/// Close all unclosed tags.
109-
fn close_all_tags(&mut self) {
111+
fn close_all_tags(&mut self) -> fmt::Result {
110112
while !self.unclosed_tags.is_empty() {
111-
self.close_tag();
113+
self.close_tag()?;
112114
}
115+
Ok(())
113116
}
114117
}
115118

src/librustdoc/html/length_limit/tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn basic() {
1212
let _ = buf.push("Hello ");
1313
buf.open_tag("em");
1414
let _ = buf.push("world");
15-
buf.close_tag();
15+
buf.close_tag().unwrap();
1616
let _ = buf.push("!");
1717
assert_eq!(buf.finish(), "Hello <em>world</em>!");
1818
}
@@ -31,7 +31,7 @@ fn limit_0() {
3131
let _ = buf.push("Hello ");
3232
buf.open_tag("em");
3333
let _ = buf.push("world");
34-
buf.close_tag();
34+
buf.close_tag().unwrap();
3535
let _ = buf.push("!");
3636
assert_eq!(buf.finish(), "");
3737
}
@@ -42,7 +42,7 @@ fn exactly_limit() {
4242
let _ = buf.push("Hello ");
4343
buf.open_tag("em");
4444
let _ = buf.push("world");
45-
buf.close_tag();
45+
buf.close_tag().unwrap();
4646
let _ = buf.push("!");
4747
assert_eq!(buf.finish(), "Hello <em>world</em>!");
4848
}
@@ -56,9 +56,9 @@ fn multiple_nested_tags() {
5656
let _ = buf.push("paragraph");
5757
buf.open_tag("strong");
5858
let _ = buf.push("!");
59-
buf.close_tag();
60-
buf.close_tag();
61-
buf.close_tag();
59+
buf.close_tag().unwrap();
60+
buf.close_tag().unwrap();
61+
buf.close_tag().unwrap();
6262
assert_eq!(buf.finish(), "<p>This is a <em>paragraph<strong>!</strong></em></p>");
6363
}
6464

@@ -82,10 +82,10 @@ fn past_the_limit() {
8282
buf.open_tag("strong");
8383
let _ = buf.push("word#")?;
8484
let _ = buf.push(&n.to_string())?;
85-
buf.close_tag();
85+
buf.close_tag().unwrap();
8686
ControlFlow::Continue(())
8787
});
88-
buf.close_tag();
88+
buf.close_tag().unwrap();
8989
assert_eq!(
9090
buf.finish(),
9191
"<p>\
@@ -111,10 +111,10 @@ fn close_too_many() {
111111
let mut buf = HtmlWithLimit::new(60);
112112
buf.open_tag("p");
113113
let _ = buf.push("Hello");
114-
buf.close_tag();
114+
buf.close_tag().unwrap();
115115
// This call does not panic because there are valid cases
116116
// where `close_tag()` is called with no tags left to close.
117117
// So `close_tag()` does nothing in this case.
118-
buf.close_tag();
118+
buf.close_tag().unwrap();
119119
assert_eq!(buf.finish(), "<p>Hello</p>");
120120
}

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ fn markdown_summary_with_limit(
15701570
if r.is_break() {
15711571
stopped_early = true;
15721572
} else {
1573-
buf.close_tag();
1573+
buf.close_tag().unwrap();
15741574
}
15751575
return r;
15761576
}
@@ -1581,7 +1581,7 @@ fn markdown_summary_with_limit(
15811581
_ => {}
15821582
},
15831583
Event::End(tag) => match tag {
1584-
TagEnd::Emphasis | TagEnd::Strong => buf.close_tag(),
1584+
TagEnd::Emphasis | TagEnd::Strong => buf.close_tag().unwrap(),
15851585
TagEnd::Paragraph | TagEnd::Heading(_) => return ControlFlow::Break(()),
15861586
_ => {}
15871587
},

0 commit comments

Comments
 (0)