about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-03-29 06:02:41 +0200
committerGitHub <noreply@github.com>2023-03-29 06:02:41 +0200
commit23813d872070f3faeec5eded7c43850fbbb149e7 (patch)
tree1516ae2649ff97ce2917df6d2d69304cf914bc5e /tests
parentcdbbce0a9e5c839295cf9a8714798af45d541e20 (diff)
parentff88787ff0238ed5b5d70317e93b95b32bbde4c9 (diff)
downloadrust-23813d872070f3faeec5eded7c43850fbbb149e7.tar.gz
rust-23813d872070f3faeec5eded7c43850fbbb149e7.zip
Rollup merge of #109149 - mj10021:issue-108713-fix, r=compiler-errors,WaffleLapkin
Improve error message when writer is forgotten in write and writeln macro

Modified write! macro error message when writer is forgotten as in issue #108713

Fixes #108713

r? ``@WaffleLapkin``
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/macros/missing-writer.rs17
-rw-r--r--tests/ui/macros/missing-writer.stderr59
-rw-r--r--tests/ui/suggestions/mut-borrow-needed-by-trait.stderr10
3 files changed, 83 insertions, 3 deletions
diff --git a/tests/ui/macros/missing-writer.rs b/tests/ui/macros/missing-writer.rs
new file mode 100644
index 00000000000..7df965c3684
--- /dev/null
+++ b/tests/ui/macros/missing-writer.rs
@@ -0,0 +1,17 @@
+// Check error for missing writer in writeln! and write! macro
+fn main() {
+    let x = 1;
+    let y = 2;
+    write!("{}_{}", x, y);
+    //~^ ERROR format argument must be a string literal
+    //~| HELP you might be missing a string literal to format with
+    //~| ERROR cannot write into `&'static str`
+    //~| NOTE must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
+    //~| HELP a writer is needed before this format string
+    writeln!("{}_{}", x, y);
+    //~^ ERROR format argument must be a string literal
+    //~| HELP you might be missing a string literal to format with
+    //~| ERROR cannot write into `&'static str`
+    //~| NOTE must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
+    //~| HELP a writer is needed before this format string
+}
diff --git a/tests/ui/macros/missing-writer.stderr b/tests/ui/macros/missing-writer.stderr
new file mode 100644
index 00000000000..86dfe7d65ea
--- /dev/null
+++ b/tests/ui/macros/missing-writer.stderr
@@ -0,0 +1,59 @@
+error: format argument must be a string literal
+  --> $DIR/missing-writer.rs:5:21
+   |
+LL |     write!("{}_{}", x, y);
+   |                     ^
+   |
+help: you might be missing a string literal to format with
+   |
+LL |     write!("{}_{}", "{} {}", x, y);
+   |                     ++++++++
+
+error: format argument must be a string literal
+  --> $DIR/missing-writer.rs:11:23
+   |
+LL |     writeln!("{}_{}", x, y);
+   |                       ^
+   |
+help: you might be missing a string literal to format with
+   |
+LL |     writeln!("{}_{}", "{} {}", x, y);
+   |                       ++++++++
+
+error[E0599]: cannot write into `&'static str`
+  --> $DIR/missing-writer.rs:5:12
+   |
+LL |     write!("{}_{}", x, y);
+   |     -------^^^^^^^------- method not found in `&str`
+   |
+note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
+  --> $DIR/missing-writer.rs:5:12
+   |
+LL |     write!("{}_{}", x, y);
+   |            ^^^^^^^
+help: a writer is needed before this format string
+  --> $DIR/missing-writer.rs:5:12
+   |
+LL |     write!("{}_{}", x, y);
+   |            ^
+
+error[E0599]: cannot write into `&'static str`
+  --> $DIR/missing-writer.rs:11:14
+   |
+LL |     writeln!("{}_{}", x, y);
+   |     ---------^^^^^^^------- method not found in `&str`
+   |
+note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
+  --> $DIR/missing-writer.rs:11:14
+   |
+LL |     writeln!("{}_{}", x, y);
+   |              ^^^^^^^
+help: a writer is needed before this format string
+  --> $DIR/missing-writer.rs:11:14
+   |
+LL |     writeln!("{}_{}", x, y);
+   |              ^
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0599`.
diff --git a/tests/ui/suggestions/mut-borrow-needed-by-trait.stderr b/tests/ui/suggestions/mut-borrow-needed-by-trait.stderr
index 6910b77d9bc..94710f4503f 100644
--- a/tests/ui/suggestions/mut-borrow-needed-by-trait.stderr
+++ b/tests/ui/suggestions/mut-borrow-needed-by-trait.stderr
@@ -21,18 +21,22 @@ note: required by a bound in `BufWriter`
   --> $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
 
 error[E0599]: the method `write_fmt` exists for struct `BufWriter<&dyn Write>`, but its trait bounds were not satisfied
-  --> $DIR/mut-borrow-needed-by-trait.rs:21:5
+  --> $DIR/mut-borrow-needed-by-trait.rs:21:14
    |
 LL |     writeln!(fp, "hello world").unwrap();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `BufWriter<&dyn Write>` due to unsatisfied trait bounds
+   |     ---------^^---------------- method cannot be called on `BufWriter<&dyn Write>` due to unsatisfied trait bounds
   --> $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
    |
    = note: doesn't satisfy `BufWriter<&dyn std::io::Write>: std::io::Write`
    |
+note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
+  --> $DIR/mut-borrow-needed-by-trait.rs:21:14
+   |
+LL |     writeln!(fp, "hello world").unwrap();
+   |              ^^
    = note: the following trait bounds were not satisfied:
            `&dyn std::io::Write: std::io::Write`
            which is required by `BufWriter<&dyn std::io::Write>: std::io::Write`
-   = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
 
 error: aborting due to 3 previous errors