Skip to content

Commit a526d7c

Browse files
committed
Auto merge of #127946 - tgross35:fmt-builders-set-result, r=cuviper
Always set `result` during `finish()` in debug builders Most functions for format builders set `self.result` after writing strings. This ensures that any further writing fails immediately rather than trying to write again. A few `.finish()` methods and the `.finish_non_exhaustive` did have this same behavior, so update the remaining `.finish()` methods to make it consistent here.
2 parents 8b6b857 + cc9da0b commit a526d7c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

library/core/src/fmt/builders.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
402402
}
403403
}
404404

405+
/// A helper used to print list-like items with no special formatting.
405406
struct DebugInner<'a, 'b: 'a> {
406407
fmt: &'a mut fmt::Formatter<'b>,
407408
result: fmt::Result,
@@ -578,7 +579,8 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
578579
/// ```
579580
#[stable(feature = "debug_builders", since = "1.2.0")]
580581
pub fn finish(&mut self) -> fmt::Result {
581-
self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
582+
self.inner.result = self.inner.result.and_then(|_| self.inner.fmt.write_str("}"));
583+
self.inner.result
582584
}
583585
}
584586

@@ -721,7 +723,8 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> {
721723
/// ```
722724
#[stable(feature = "debug_builders", since = "1.2.0")]
723725
pub fn finish(&mut self) -> fmt::Result {
724-
self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
726+
self.inner.result = self.inner.result.and_then(|_| self.inner.fmt.write_str("]"));
727+
self.inner.result
725728
}
726729
}
727730

@@ -1002,11 +1005,12 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
10021005
/// ```
10031006
#[stable(feature = "debug_builders", since = "1.2.0")]
10041007
pub fn finish(&mut self) -> fmt::Result {
1005-
self.result.and_then(|_| {
1008+
self.result = self.result.and_then(|_| {
10061009
assert!(!self.has_key, "attempted to finish a map with a partial entry");
10071010

10081011
self.fmt.write_str("}")
1009-
})
1012+
});
1013+
self.result
10101014
}
10111015

10121016
fn is_pretty(&self) -> bool {

0 commit comments

Comments
 (0)