about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-07-27 02:26:30 +0000
committerbors <bors@rust-lang.org>2024-07-27 02:26:30 +0000
commita526d7ce45fd2284e0e7c7556ccba2425b9d25e5 (patch)
treef931ea860cd1be69375166a426059c85e243e9f3
parent8b6b8574f6f2fcc71ec500a52d7bf74fdaff0ed6 (diff)
parentcc9da0b2ce36aea494760db78714dc98739949ef (diff)
downloadrust-a526d7ce45fd2284e0e7c7556ccba2425b9d25e5.tar.gz
rust-a526d7ce45fd2284e0e7c7556ccba2425b9d25e5.zip
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.
-rw-r--r--library/core/src/fmt/builders.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/library/core/src/fmt/builders.rs b/library/core/src/fmt/builders.rs
index 36fae1c1596..794ca1851b1 100644
--- a/library/core/src/fmt/builders.rs
+++ b/library/core/src/fmt/builders.rs
@@ -402,6 +402,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
     }
 }
 
+/// A helper used to print list-like items with no special formatting.
 struct DebugInner<'a, 'b: 'a> {
     fmt: &'a mut fmt::Formatter<'b>,
     result: fmt::Result,
@@ -578,7 +579,8 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> {
     /// ```
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn finish(&mut self) -> fmt::Result {
-        self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
+        self.inner.result = self.inner.result.and_then(|_| self.inner.fmt.write_str("}"));
+        self.inner.result
     }
 }
 
@@ -721,7 +723,8 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> {
     /// ```
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn finish(&mut self) -> fmt::Result {
-        self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
+        self.inner.result = self.inner.result.and_then(|_| self.inner.fmt.write_str("]"));
+        self.inner.result
     }
 }
 
@@ -1002,11 +1005,12 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
     /// ```
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn finish(&mut self) -> fmt::Result {
-        self.result.and_then(|_| {
+        self.result = self.result.and_then(|_| {
             assert!(!self.has_key, "attempted to finish a map with a partial entry");
 
             self.fmt.write_str("}")
-        })
+        });
+        self.result
     }
 
     fn is_pretty(&self) -> bool {