From 9a893cc2b82ac6259aead1319758404b80b8a959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 24 Jul 2018 20:46:22 -0700 Subject: [PATCH] Add span label for format str missing specifier --- src/libsyntax_ext/format.rs | 19 +++++++----- src/test/ui/ifmt-bad-arg.stderr | 30 ++++++++++++++----- src/test/ui/macros/format-foreign.stderr | 10 ++++--- .../ui/macros/format-unused-lables.stderr | 4 ++- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index ad05db91770..98de3d80b1e 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -915,12 +915,13 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, errs.push((cx.args[i].span, msg)); } } - if errs.len() > 0 { - let args_used = cx.arg_types.len() - errs.len(); - let args_unused = errs.len(); + let errs_len = errs.len(); + if errs_len > 0 { + let args_used = cx.arg_types.len() - errs_len; + let args_unused = errs_len; let mut diag = { - if errs.len() == 1 { + if errs_len == 1 { let (sp, msg) = errs.into_iter().next().unwrap(); cx.ecx.struct_span_err(sp, msg) } else { @@ -933,6 +934,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, } }; + // Used to ensure we only report translations for *one* kind of foreign format. + let mut found_foreign = false; // Decide if we want to look for foreign formatting directives. if args_used < args_unused { use super::format_foreign as foreign; @@ -941,9 +944,6 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, // with `%d should be written as {}` over and over again. let mut explained = HashSet::new(); - // Used to ensure we only report translations for *one* kind of foreign format. - let mut found_foreign = false; - macro_rules! check_foreign { ($kind:ident) => {{ let mut show_doc_note = false; @@ -987,7 +987,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, } if suggestions.len() > 0 { diag.multipart_suggestion( - "format specifiers in Rust are written using `{}`", + "format specifiers use curly braces", suggestions, ); } @@ -999,6 +999,9 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, check_foreign!(shell); } } + if !found_foreign && errs_len == 1 { + diag.span_label(cx.fmtsp, "formatting specifier missing"); + } diag.emit(); } diff --git a/src/test/ui/ifmt-bad-arg.stderr b/src/test/ui/ifmt-bad-arg.stderr index a126998355e..c8fd8bad19b 100644 --- a/src/test/ui/ifmt-bad-arg.stderr +++ b/src/test/ui/ifmt-bad-arg.stderr @@ -16,7 +16,9 @@ error: argument never used --> $DIR/ifmt-bad-arg.rs:19:20 | LL | format!("{1}", 1); - | ^ + | ----- ^ + | | + | formatting specifier missing error: 2 positional arguments in format string, but no arguments were given --> $DIR/ifmt-bad-arg.rs:23:14 @@ -86,31 +88,41 @@ error: argument never used --> $DIR/ifmt-bad-arg.rs:43:22 | LL | format!("{}", 1, 2); //~ ERROR: argument never used - | ^ + | ---- ^ + | | + | formatting specifier missing error: argument never used --> $DIR/ifmt-bad-arg.rs:44:20 | LL | format!("{1}", 1, 2); //~ ERROR: argument never used - | ^ + | ----- ^ + | | + | formatting specifier missing error: named argument never used --> $DIR/ifmt-bad-arg.rs:45:26 | LL | format!("{}", 1, foo=2); //~ ERROR: named argument never used - | ^ + | ---- ^ + | | + | formatting specifier missing error: argument never used --> $DIR/ifmt-bad-arg.rs:46:22 | LL | format!("{foo}", 1, foo=2); //~ ERROR: argument never used - | ^ + | ------- ^ + | | + | formatting specifier missing error: named argument never used --> $DIR/ifmt-bad-arg.rs:47:21 | LL | format!("", foo=2); //~ ERROR: named argument never used - | ^ + | -- ^ + | | + | formatting specifier missing error: multiple unused formatting arguments --> $DIR/ifmt-bad-arg.rs:48:32 @@ -148,7 +160,9 @@ error: named argument never used --> $DIR/ifmt-bad-arg.rs:55:51 | LL | format!("{valuea} {valueb}", valuea=5, valuec=7); - | ^ + | ------------------- ^ + | | + | formatting specifier missing error: invalid format string: expected `'}'` but string was terminated --> $DIR/ifmt-bad-arg.rs:61:15 @@ -180,7 +194,7 @@ error: argument never used LL | format!("foo %s baz", "bar"); //~ ERROR: argument never used | -- ^^^^^ | | - | help: format specifiers in Rust are written using `{}`: `{}` + | help: format specifiers use curly braces: `{}` | = note: printf formatting not supported; see the documentation for `std::fmt` diff --git a/src/test/ui/macros/format-foreign.stderr b/src/test/ui/macros/format-foreign.stderr index 93e68183b14..5e76c0a322e 100644 --- a/src/test/ui/macros/format-foreign.stderr +++ b/src/test/ui/macros/format-foreign.stderr @@ -7,7 +7,7 @@ LL | println!("%.*3$s %s!/n", "Hello,", "World", 4); //~ ERROR multiple unus | multiple missing formatting specifiers | = note: printf formatting not supported; see the documentation for `std::fmt` -help: format specifiers in Rust are written using `{}` +help: format specifiers use curly braces | LL | println!("{:.2$} {}!/n", "Hello,", "World", 4); //~ ERROR multiple unused formatting arguments | ^^^^^^ ^^ @@ -18,7 +18,7 @@ error: argument never used LL | println!("%1$*2$.*3$f", 123.456); //~ ERROR never used | ----------- ^^^^^^^ | | - | help: format specifiers in Rust are written using `{}`: `{0:1$.2$}` + | help: format specifiers use curly braces: `{0:1$.2$}` | = note: printf formatting not supported; see the documentation for `std::fmt` @@ -34,7 +34,7 @@ LL | | "###, "Hello,", "World", 4); | multiple missing formatting specifiers | = note: printf formatting not supported; see the documentation for `std::fmt` -help: format specifiers in Rust are written using `{}` +help: format specifiers use curly braces | LL | println!(r###"{:.2$} LL | {}!/n @@ -44,7 +44,9 @@ error: argument never used --> $DIR/format-foreign.rs:22:30 | LL | println!("{} %f", "one", 2.0); //~ ERROR never used - | ^^^ + | ------- ^^^ + | | + | formatting specifier missing error: named argument never used --> $DIR/format-foreign.rs:24:39 diff --git a/src/test/ui/macros/format-unused-lables.stderr b/src/test/ui/macros/format-unused-lables.stderr index 0b024facc8e..81171a1ed01 100644 --- a/src/test/ui/macros/format-unused-lables.stderr +++ b/src/test/ui/macros/format-unused-lables.stderr @@ -22,7 +22,9 @@ error: named argument never used --> $DIR/format-unused-lables.rs:21:35 | LL | println!("Some stuff", UNUSED="args"); //~ ERROR named argument never used - | ^^^^^^ + | ------------ ^^^^^^ + | | + | formatting specifier missing error: multiple unused formatting arguments --> $DIR/format-unused-lables.rs:24:9