Skip to content

Commit 4478ecc

Browse files
committed
Don't panic if close_tag() is called without tags to close
This can happen when a tag is opened after the length limit is reached; the tag will not end up being added to `unclosed_tags` because the queue will never be flushed. So, now, if the `unclosed_tags` stack is empty, `close_tag()` does nothing. This change fixes a panic in the `limit_0` unit test.
1 parent d932e62 commit 4478ecc

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/librustdoc/html/length_limit.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ impl HtmlWithLimit {
8686

8787
/// Close the most recently opened HTML tag.
8888
pub(super) fn close_tag(&mut self) {
89-
let tag_name = self.unclosed_tags.pop().unwrap();
90-
write!(self.buf, "</{}>", tag_name).unwrap();
89+
match self.unclosed_tags.pop() {
90+
// Close the most recently opened tag.
91+
Some(tag_name) => write!(self.buf, "</{}>", tag_name).unwrap(),
92+
// There are valid cases where `close_tag()` is called without
93+
// there being any tags to close. For example, this occurs when
94+
// a tag is opened after the length limit is exceeded;
95+
// `flush_queue()` will never be called, and thus, the tag will
96+
// not end up being added to `unclosed_tags`.
97+
None => {}
98+
}
9199
}
92100

93101
/// Write all queued tags and add them to the `unclosed_tags` list.

src/librustdoc/html/length_limit/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ fn quickly_past_the_limit() {
107107
}
108108

109109
#[test]
110-
#[should_panic = "called `Option::unwrap()` on a `None` value"]
111110
fn close_too_many() {
112111
let mut buf = HtmlWithLimit::new(60);
113112
buf.open_tag("p");
114113
buf.push("Hello");
115114
buf.close_tag();
115+
// This call does not panic because there are valid cases
116+
// where `close_tag()` is called with no tags left to close.
117+
// So `close_tag()` does nothing in this case.
116118
buf.close_tag();
119+
assert_eq!(buf.finish(), "<p>Hello</p>");
117120
}

0 commit comments

Comments
 (0)