Skip to content

Commit

Permalink
Add span label for format str missing specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jul 25, 2018
1 parent 7bd94e0 commit 9a893cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
19 changes: 11 additions & 8 deletions src/libsyntax_ext/format.rs
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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();
}
Expand Down
30 changes: 22 additions & 8 deletions src/test/ui/ifmt-bad-arg.stderr
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/macros/format-foreign.stderr
Expand Up @@ -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
| ^^^^^^ ^^
Expand All @@ -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`

Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/macros/format-unused-lables.stderr
Expand Up @@ -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
Expand Down

0 comments on commit 9a893cc

Please sign in to comment.