diff --git a/src/librustdoc/html/length_limit.rs b/src/librustdoc/html/length_limit.rs index 28a207a84ba6d..bbdc91c8d2ec8 100644 --- a/src/librustdoc/html/length_limit.rs +++ b/src/librustdoc/html/length_limit.rs @@ -86,8 +86,16 @@ impl HtmlWithLimit { /// Close the most recently opened HTML tag. pub(super) fn close_tag(&mut self) { - let tag_name = self.unclosed_tags.pop().unwrap(); - write!(self.buf, "", tag_name).unwrap(); + match self.unclosed_tags.pop() { + // Close the most recently opened tag. + Some(tag_name) => write!(self.buf, "", tag_name).unwrap(), + // There are valid cases where `close_tag()` is called without + // there being any tags to close. For example, this occurs when + // a tag is opened after the length limit is exceeded; + // `flush_queue()` will never be called, and thus, the tag will + // not end up being added to `unclosed_tags`. + None => {} + } } /// Write all queued tags and add them to the `unclosed_tags` list. diff --git a/src/librustdoc/html/length_limit/tests.rs b/src/librustdoc/html/length_limit/tests.rs index 5a006d44d58c5..2d02b8a16da67 100644 --- a/src/librustdoc/html/length_limit/tests.rs +++ b/src/librustdoc/html/length_limit/tests.rs @@ -107,11 +107,14 @@ fn quickly_past_the_limit() { } #[test] -#[should_panic = "called `Option::unwrap()` on a `None` value"] fn close_too_many() { let mut buf = HtmlWithLimit::new(60); buf.open_tag("p"); buf.push("Hello"); buf.close_tag(); + // This call does not panic because there are valid cases + // where `close_tag()` is called with no tags left to close. + // So `close_tag()` does nothing in this case. buf.close_tag(); + assert_eq!(buf.finish(), "

Hello

"); }