rust/src/test/ui/fmt/format-string-error.rs
Esteban Küber 8467ceff22 Tweak format string error to point at arguments always
Add secondary span labels with no text to make it clear when there's a
mismatch bewteen the positional arguments in a format string and the
arguments to the macro. This shouldn't affect experienced users, but it
should make it easier for newcomers to more clearly understand how
`format!()` and `println!()` are supposed to be used.

```
error: 2 positional arguments in format string, but there is 1 argument
 --> file8.rs:2:14
  |
2 |     format!("{} {}", 1);
  |              ^^ ^^   -
```

instead of

```
error: 2 positional arguments in format string, but there is 1 argument
 --> file8.rs:2:14
  |
2 |     format!("{} {}", 1);
  |              ^^ ^^
```
2019-10-23 17:32:33 -07:00

53 lines
1.2 KiB
Rust

// ignore-tidy-tab
fn main() {
println!("{");
//~^ ERROR invalid format string: expected `'}'` but string was terminated
println!("{{}}");
println!("}");
//~^ ERROR invalid format string: unmatched `}` found
let _ = format!("{_foo}", _foo = 6usize);
//~^ ERROR invalid format string: invalid argument name `_foo`
let _ = format!("{_}", _ = 6usize);
//~^ ERROR invalid format string: invalid argument name `_`
let _ = format!("{");
//~^ ERROR invalid format string: expected `'}'` but string was terminated
let _ = format!("}");
//~^ ERROR invalid format string: unmatched `}` found
let _ = format!("{\\}");
//~^ ERROR invalid format string: expected `'}'`, found `'\\'`
let _ = format!("\n\n\n{\n\n\n");
//~^ ERROR invalid format string
let _ = format!(r###"
{"###);
//~^ ERROR invalid format string
let _ = format!(r###"
{
"###);
//~^ ERROR invalid format string
let _ = format!(r###"
}
"###);
//~^^^ ERROR invalid format string
let _ = format!(r###"
}
"###);
//~^^^ ERROR invalid format string: unmatched `}` found
println!("{} {} {}", 1, 2);
//~^ ERROR 3 positional arguments in format string, but there are 2 arguments
}