Improve suggestion for missing fmt str in println

Avoid using `concat!(fmt, "\n")` to improve the diagnostics being
emitted when the first `println!()` argument isn't a formatting string
literal.
This commit is contained in:
Esteban Küber 2018-07-14 20:50:30 -07:00 committed by Esteban Küber
parent bc14d71622
commit f53c145ef1
14 changed files with 122 additions and 102 deletions

View file

@ -9,5 +9,8 @@
// except according to those terms.
fn main() {
println!(3 + 4); //~ ERROR expected a literal
println!(3 + 4);
//~^ ERROR format argument must be a string literal
println!(3, 4);
//~^ ERROR format argument must be a string literal
}

View file

@ -1,12 +1,22 @@
error: expected a literal
error: format argument must be a string literal
--> $DIR/bad_hello.rs:12:14
|
LL | println!(3 + 4); //~ ERROR expected a literal
LL | println!(3 + 4);
| ^^^^^
help: you might be missing a string literal to format with
|
LL | println!("{}", 3 + 4); //~ ERROR expected a literal
| ^^^^^^^^^^^
LL | println!("{}", 3 + 4);
| ^^^^^
error: aborting due to previous error
error: format argument must be a string literal
--> $DIR/bad_hello.rs:14:14
|
LL | println!(3, 4);
| ^
help: you might be missing a string literal to format with
|
LL | println!("{}, {}", 3, 4);
| ^^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -2,12 +2,13 @@ error: multiple unused formatting arguments
--> $DIR/format-foreign.rs:12:30
|
LL | println!("%.*3$s %s!/n", "Hello,", "World", 4); //~ ERROR multiple unused formatting arguments
| -------------------------^^^^^^^^--^^^^^^^--^-- multiple unused arguments in this statement
| -------------- ^^^^^^^^ ^^^^^^^ ^
| |
| multiple missing formatting arguments
|
= help: `%.*3$s` should be written as `{:.2$}`
= help: `%s` should be written as `{}`
= note: printf formatting not supported; see the documentation for `std::fmt`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: argument never used
--> $DIR/format-foreign.rs:13:29

View file

@ -2,24 +2,21 @@ error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:12:22
|
LL | println!("Test", 123, 456, 789);
| -----------------^^^--^^^--^^^-- multiple unused arguments in this statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
| ------ ^^^ ^^^ ^^^
| |
| multiple missing formatting arguments
error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:16:9
|
LL | / println!("Test2",
LL | | 123, //~ ERROR multiple unused formatting arguments
| | ^^^
LL | | 456,
| | ^^^
LL | | 789
| | ^^^
LL | | );
| |______- multiple unused arguments in this statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
LL | println!("Test2",
| ------- multiple missing formatting arguments
LL | 123, //~ ERROR multiple unused formatting arguments
| ^^^
LL | 456,
| ^^^
LL | 789
| ^^^
error: named argument never used
--> $DIR/format-unused-lables.rs:21:35
@ -30,18 +27,18 @@ LL | println!("Some stuff", UNUSED="args"); //~ ERROR named argument never u
error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:24:9
|
LL | / println!("Some more $STUFF",
LL | | "woo!", //~ ERROR multiple unused formatting arguments
| | ^^^^^^
LL | | STUFF=
LL | | "things"
| | ^^^^^^^^
LL | | , UNUSED="args");
| |_______________________^^^^^^_- multiple unused arguments in this statement
LL | println!("Some more $STUFF",
| ------------------ multiple missing formatting arguments
LL | "woo!", //~ ERROR multiple unused formatting arguments
| ^^^^^^
LL | STUFF=
LL | "things"
| ^^^^^^^^
LL | , UNUSED="args");
| ^^^^^^
|
= help: `$STUFF` should be written as `{STUFF}`
= note: shell formatting not supported; see the documentation for `std::fmt`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to 4 previous errors

View file

@ -5,8 +5,9 @@ LL | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `println! { "Hello, World!" }`
= note: to `print ! ( concat ! ( "Hello, World!" , "/n" ) )`
= note: expanding `print! { concat ! ( "Hello, World!" , "/n" ) }`
= note: to `$crate :: io :: _print ( format_args ! ( concat ! ( "Hello, World!" , "/n" ) )
)`
= note: to `{ print ! ( "Hello, World!" ) ; print ! ( "/n" ) ; }`
= note: expanding `print! { "Hello, World!" }`
= note: to `$crate :: io :: _print ( format_args ! ( "Hello, World!" ) )`
= note: expanding `print! { "/n" }`
= note: to `$crate :: io :: _print ( format_args ! ( "/n" ) )`