Improve write! and writeln! error when called without destination

This commit is contained in:
arferreira
2026-02-11 20:37:45 -05:00
parent 7057231bd7
commit 0bcec37604
3 changed files with 21 additions and 0 deletions
+6
View File
@@ -611,6 +611,9 @@ macro_rules! write {
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args!($($arg)*))
};
($($arg:tt)*) => {
compile_error!("requires a destination and format arguments, like `write!(dest, \"format string\", args...)`")
};
}
/// Writes formatted data into a buffer, with a newline appended.
@@ -649,6 +652,9 @@ macro_rules! writeln {
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args_nl!($($arg)*))
};
($($arg:tt)*) => {
compile_error!("requires a destination and format arguments, like `writeln!(dest, \"format string\", args...)`")
};
}
/// Indicates unreachable code.
@@ -0,0 +1,7 @@
// Check that `write!` without a destination gives a helpful error message.
// See https://github.com/rust-lang/rust/issues/152493
fn main() {
write!("S");
//~^ ERROR requires a destination and format arguments
}
@@ -0,0 +1,8 @@
error: requires a destination and format arguments, like `write!(dest, "format string", args...)`
--> $DIR/write-missing-destination.rs:5:5
|
LL | write!("S");
| ^^^^^^^^^^^
error: aborting due to 1 previous error