Skip to content

Conversation

IoaNNUwU
Copy link
Contributor

@IoaNNUwU IoaNNUwU commented Sep 2, 2025

Format macro now suggests adding {} if no formatting specifiers are present. It also gives an example:

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");
   |                    ++

When one or more {} are present, it doesn't show 'format specifiers use curly braces: {}' and example, just small hint on how many you missing:

LL |     println!("list: {}", 1, 2, 3);
   |              ----------     ^  ^ argument never used
   |              |              |
   |              |              argument never used
   |              multiple missing formatting specifiers
   |
   = help: consider adding 2 format specifiers

Original issue: #68293
Based on discussion in this PR: #76443

Let me know if something is missing

@rustbot
Copy link
Collaborator

rustbot commented Sep 2, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 2, 2025
@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Sep 8, 2025

r? @estebank though feel free to reassign

@rustbot rustbot assigned estebank and unassigned lcnr Sep 8, 2025
Comment on lines 767 to 790
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,
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
);
}
}
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 {
if unused.len() > 1 {
diag.note(format!("consider adding {} format specifiers", unused.len()));
}
} else {
let original_fmt_str = if fmt_str.len() >= 1 { &fmt_str[..fmt_str.len() - 1] } else { "" };
let msg = if unused.len() == 1 {
"a format specifier".to_string()
} else {
format!("{} format specifiers", unused.len())
};
let sugg = format!("\"{}{}\"", original_fmt_str, "{}".repeat(unused.len()));
let msg = format!("format specifiers use curly braces, consider adding {msg}");
diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);
}
}

Comment on lines 66 to 67
= note: format specifiers use curly braces: `{}`
help: consider adding 2 format specifiers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having both a note and a suggestion talking about the same thing is redundant. Instead, make the message of the suggestion also work as the explanation.

@estebank estebank added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 8, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@IoaNNUwU
Copy link
Contributor Author

IoaNNUwU commented Sep 8, 2025

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 8, 2025
@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Sep 10, 2025

📌 Commit 43a6f56 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 10, 2025
bors added a commit that referenced this pull request Sep 10, 2025
Rollup of 8 pull requests

Successful merges:

 - #145327 (std: make address resolution weirdness local to SGX)
 - #145879 (default auto traits: use default supertraits instead of `Self: Trait` bounds on associated items)
 - #146123 (Suggest examples of format specifiers in error messages)
 - #146311 (Minor symbol comment fixes.)
 - #146322 (Make Barrier RefUnwindSafe again)
 - #146327 (Add tests for deref on pin)
 - #146340 (Strip frontmatter in fewer places)
 - #146342 (Improve C-variadic error messages: part 2)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f48c1d8 into rust-lang:master Sep 11, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Sep 11, 2025
rust-timer added a commit that referenced this pull request Sep 11, 2025
Rollup merge of #146123 - IoaNNUwU:issue-68293, r=estebank

Suggest examples of format specifiers in error messages

Format macro now suggests adding `{}` if no formatting specifiers are present. It also gives an example:
```rust
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");
   |                    ++
```
When one or more `{}` are present, it doesn't show 'format specifiers use curly braces: `{}`' and example, just small hint on how many you missing:
```rust
LL |     println!("list: {}", 1, 2, 3);
   |              ----------     ^  ^ argument never used
   |              |              |
   |              |              argument never used
   |              multiple missing formatting specifiers
   |
   = help: consider adding 2 format specifiers
```

Original issue: #68293
Based on discussion in this PR: #76443

Let me know if something is missing
jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 13, 2025
Fix panic and incorrectly suggested examples in `format_args` macro.

Follow up on rust-lang#146123
Fixes: rust-lang#146446
r? `@estebank`
rust-timer added a commit that referenced this pull request Sep 13, 2025
Rollup merge of #146456 - IoaNNUwU:issue-146446, r=estebank

Fix panic and incorrectly suggested examples in `format_args` macro.

Follow up on #146123
Fixes: #146446
r? `@estebank`
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Sep 14, 2025
Fix panic and incorrectly suggested examples in `format_args` macro.

Follow up on rust-lang/rust#146123
Fixes: rust-lang/rust#146446
r? `@estebank`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants