Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Suggest examples of format specifiers in error messages #146123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
IoaNNUwU wants to merge 2 commits into rust-lang:master
base: master
Choose a base branch
Loading
from IoaNNUwU:issue-68293
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions compiler/rustc_builtin_macros/src/format.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ fn make_format_args(
&used,
&args,
&pieces,
&invalid_refs,
detect_foreign_fmt,
str_style,
fmt_str,
Expand Down Expand Up @@ -645,6 +646,7 @@ fn report_missing_placeholders(
used: &[bool],
args: &FormatArguments,
pieces: &[parse::Piece<'_>],
invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
detect_foreign_fmt: bool,
str_style: Option<usize>,
fmt_str: &str,
Expand Down Expand Up @@ -762,6 +764,45 @@ fn report_missing_placeholders(
diag.span_label(fmt_span, "formatting specifier missing");
}

if !found_foreign && invalid_refs.is_empty() {
// Show example if user didn't use any format specifiers
let show_example = used.iter().all(|used| !used);

if !show_example && unused.len() > 1 {
diag.note(format!("consider adding {} format specifiers", unused.len()));
}

let original_fmt_str = if fmt_str.len() >= 1 { &fmt_str[..fmt_str.len() - 1] } else { "" };

if show_example && unused.len() == 1 {
diag.note("format specifiers use curly braces: `{}`");

diag.span_suggestion_verbose(
fmt_span,
"consider adding format specifier",
format!("\"{}{{}}\"", original_fmt_str),
Applicability::MaybeIncorrect,
);
}

if show_example && unused.len() > 1 {
diag.note("format specifiers use curly braces: `{}`");

let mut suggest_fixed_fmt = format!("\"{}", original_fmt_str);
for _ in &unused {
suggest_fixed_fmt.push_str("{}");
}
suggest_fixed_fmt.push('"');

diag.span_suggestion_verbose(
fmt_span,
format!("consider adding {} format specifiers", unused.len()),
suggest_fixed_fmt,
Applicability::MaybeIncorrect,
);
}
}

diag.emit();
}

Expand Down
14 changes: 14 additions & 0 deletions tests/ui/fmt/ifmt-bad-arg.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ LL | format!("", 1, 2);
| | |
| | argument never used
| multiple missing formatting specifiers
|
= note: format specifiers use curly braces: `{}`
help: consider adding 2 format specifiers
|
LL | format!("{}{}", 1, 2);
| ++++

error: argument never used
--> $DIR/ifmt-bad-arg.rs:33:22
Expand Down Expand Up @@ -102,6 +108,12 @@ LL | format!("", foo=2);
| -- ^ named argument never used
| |
| formatting specifier missing
|
= note: format specifiers use curly braces: `{}`
help: consider adding format specifier
|
LL | format!("{}", foo=2);
| ++

error: multiple unused formatting arguments
--> $DIR/ifmt-bad-arg.rs:38:32
Expand All @@ -111,6 +123,8 @@ LL | format!("{} {}", 1, 2, foo=1, bar=2);
| | |
| | named argument never used
| multiple missing formatting specifiers
|
= note: consider adding 2 format specifiers

error: duplicate argument named `foo`
--> $DIR/ifmt-bad-arg.rs:40:29
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/macros/format-unused-lables.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ LL | println!("Test", 123, 456, 789);
| | | argument never used
| | argument never used
| multiple missing formatting specifiers
|
= note: format specifiers use curly braces: `{}`
help: consider adding 3 format specifiers
|
LL | println!("Test{}{}{}", 123, 456, 789);
| ++++++

error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:6:9
Expand All @@ -19,6 +25,12 @@ LL | 456,
| ^^^ argument never used
LL | 789
| ^^^ argument never used
|
= note: format specifiers use curly braces: `{}`
help: consider adding 3 format specifiers
|
LL | println!("Test2{}{}{}",
| ++++++

error: named argument never used
--> $DIR/format-unused-lables.rs:11:35
Expand All @@ -27,6 +39,12 @@ LL | println!("Some stuff", UNUSED="args");
| ------------ ^^^^^^ named argument never used
| |
| formatting specifier missing
|
= note: format specifiers use curly braces: `{}`
help: consider adding format specifier
|
LL | println!("Some stuff{}", UNUSED="args");
| ++

error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:14:9
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/mir/unsized-extern-static.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ LL | println!("C", unsafe { &symbol });
| --- ^^^^^^^^^^^^^^^^^^ argument never used
| |
| formatting specifier missing
|
= note: format specifiers use curly braces: `{}`
help: consider adding format specifier
|
LL | println!("C{}", unsafe { &symbol });
| ++

error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
--> $DIR/unsized-extern-static.rs:6:5
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/suggestions/missing-format-specifiers-issue-68293.rs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fn no_format_specifier_two_unused_args() {
println!("Hello", "World");
//~^ ERROR argument never used
//~| NOTE formatting specifier missing
//~| NOTE format specifiers use curly braces: `{}`
//~| NOTE argument never used
}

fn no_format_specifier_multiple_unused_args() {
println!("list: ", 1, 2, 3);
//~^ ERROR multiple unused formatting arguments
//~| NOTE multiple missing formatting specifiers
//~| NOTE format specifiers use curly braces: `{}`
//~| NOTE argument never used
//~| NOTE argument never used
//~| NOTE argument never used
}

fn missing_format_specifiers_one_unused_arg() {
println!("list: {}, {}", 1, 2, 3);
//~^ ERROR argument never used
//~| NOTE formatting specifier missing
//~| NOTE argument never used
}

fn missing_format_specifiers_multiple_unused_args() {
println!("list: {}", 1, 2, 3);
//~^ ERROR multiple unused formatting arguments
//~| NOTE multiple missing formatting specifiers
//~| NOTE argument never used
//~| NOTE argument never used
//~| NOTE consider adding 2 format specifiers
}

fn main() { }
51 changes: 51 additions & 0 deletions tests/ui/suggestions/missing-format-specifiers-issue-68293.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
error: argument never used
--> $DIR/missing-format-specifiers-issue-68293.rs:2:23
|
LL | println!("Hello", "World");
| ------- ^^^^^^^ argument never used
| |
| formatting specifier missing
|
= note: format specifiers use curly braces: `{}`
help: consider adding format specifier
|
LL | println!("Hello{}", "World");
| ++

error: multiple unused formatting arguments
--> $DIR/missing-format-specifiers-issue-68293.rs:10:24
|
LL | println!("list: ", 1, 2, 3);
| -------- ^ ^ ^ argument never used
| | | |
| | | argument never used
| | argument never used
| multiple missing formatting specifiers
|
= note: format specifiers use curly braces: `{}`
help: consider adding 3 format specifiers
|
LL | println!("list: {}{}{}", 1, 2, 3);
| ++++++

error: argument never used
--> $DIR/missing-format-specifiers-issue-68293.rs:20:36
|
LL | println!("list: {}, {}", 1, 2, 3);
| -------------- ^ argument never used
| |
| formatting specifier missing

error: multiple unused formatting arguments
--> $DIR/missing-format-specifiers-issue-68293.rs:27:29
|
LL | println!("list: {}", 1, 2, 3);
| ---------- ^ ^ argument never used
| | |
| | argument never used
| multiple missing formatting specifiers
|
= note: consider adding 2 format specifiers

error: aborting due to 4 previous errors

Loading

AltStyle によって変換されたページ (->オリジナル) /