Skip to content

Improve error message for printf-style format strings - #89340

Merged
bors merged 1 commit into
rust-lang:masterfrom
FabianWolff:issue-89173
Oct 1, 2021
Merged

Improve error message for printf-style format strings#89340
bors merged 1 commit into
rust-lang:masterfrom
FabianWolff:issue-89173

Conversation

@FabianWolff

Copy link
Copy Markdown
Contributor

Fixes#89173. The following is actually supported today:

fnmain(){let num = 5;let width = 20;print!("%*2$x", num, width);}
error: multiple unused formatting arguments
--> src/main.rs:4:21
|
4 | print!("%*2$x", num, width);
| ------- ^^^ ^^^^^ argument never used
| || |
| || argument never used
| |help: format specifiers use curly braces: `{:1$x}`
| multiple missing formatting specifiers
|
= note: printf formatting not supported; see the documentation for `std::fmt`

However, as noted in #89173, something like

print!("%0*x", width, num);

does not give a helpful suggestion. I think this is partly intended, because there actually is no Rust equivalent to this; you always have to use a positional or named argument to specify the width (instead of just using the "next" argument, as printf or even .* as a precision specifier in Rust would). Therefore, I have added a note:

[...]
note: format specifiers use curly braces, and you have to use a positional or named parameter for the width
--> t2.rs:4:13
|
4 | print!("%0*x", width, num);
| ^^^^
= note: printf formatting not supported; see the documentation for `std::fmt`

This is not perfect, but it should at least point the user in the right direction, instead of issuing no explanation at all.

cc @lcnr

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @petrochenkov

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 28, 2021
@petrochenkov

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Sep 29, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit 6490ed3 has been approved by petrochenkov

@borsbors 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 29, 2021
ehuss added a commit to ehuss/rust that referenced this pull request Sep 30, 2021
…nkov
Improve error message for `printf`-style format strings
Fixesrust-lang#89173. The following is actually supported today:
```rust
fn main() {
let num = 5;
let width = 20;
print!("%*2$x", num, width);
}
```
```
error: multiple unused formatting arguments
--> src/main.rs:4:21
|
4 | print!("%*2$x", num, width);
| ------- ^^^ ^^^^^ argument never used
| || |
| || argument never used
| |help: format specifiers use curly braces: `{:1$x}`
| multiple missing formatting specifiers
|
= note: printf formatting not supported; see the documentation for `std::fmt`
```
However, as noted in rust-lang#89173, something like
```rust
print!("%0*x", width, num);
```
does not give a helpful suggestion. I think this is partly intended, because there actually _is_ no Rust equivalent to this; you always have to use a positional or named argument to specify the width (instead of just using the "next" argument, as `printf` or even `.*` as a precision specifier in Rust would). Therefore, I have added a note:
```
[...]
note: format specifiers use curly braces, and you have to use a positional or named parameter for the width
--> t2.rs:4:13
|
4 | print!("%0*x", width, num);
| ^^^^
= note: printf formatting not supported; see the documentation for `std::fmt`
```
This is not perfect, but it should at least point the user in the right direction, instead of issuing no explanation at all.
cc `@lcnr`
@ehussehuss mentioned this pull request Sep 30, 2021
fee1-dead added a commit to fee1-dead-contrib/rust that referenced this pull request Oct 1, 2021
…nkov
Improve error message for `printf`-style format strings
Fixesrust-lang#89173. The following is actually supported today:
```rust
fn main() {
let num = 5;
let width = 20;
print!("%*2$x", num, width);
}
```
```
error: multiple unused formatting arguments
--> src/main.rs:4:21
|
4 | print!("%*2$x", num, width);
| ------- ^^^ ^^^^^ argument never used
| || |
| || argument never used
| |help: format specifiers use curly braces: `{:1$x}`
| multiple missing formatting specifiers
|
= note: printf formatting not supported; see the documentation for `std::fmt`
```
However, as noted in rust-lang#89173, something like
```rust
print!("%0*x", width, num);
```
does not give a helpful suggestion. I think this is partly intended, because there actually _is_ no Rust equivalent to this; you always have to use a positional or named argument to specify the width (instead of just using the "next" argument, as `printf` or even `.*` as a precision specifier in Rust would). Therefore, I have added a note:
```
[...]
note: format specifiers use curly braces, and you have to use a positional or named parameter for the width
--> t2.rs:4:13
|
4 | print!("%0*x", width, num);
| ^^^^
= note: printf formatting not supported; see the documentation for `std::fmt`
```
This is not perfect, but it should at least point the user in the right direction, instead of issuing no explanation at all.
cc ``@lcnr``
@fee1-deadfee1-dead mentioned this pull request Oct 1, 2021
@ManishearthManishearth mentioned this pull request Oct 1, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 1, 2021
…arth
Rollup of 6 pull requests
Successful merges:
- rust-lang#87868 (Added -Z randomize-layout flag)
- rust-lang#88820 (Add `pie` as another `relocation-model` value)
- rust-lang#89029 (feat(rustc_parse): recover from pre-RFC-2000 const generics syntax)
- rust-lang#89322 (Reapply "Remove optimization_fuel_crate from Session")
- rust-lang#89340 (Improve error message for `printf`-style format strings)
- rust-lang#89415 (Correct caller/callsite confusion in inliner message)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit d388428 into rust-lang:masterOct 1, 2021
@rustbotrustbot added this to the 1.57.0 milestone Oct 1, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

not all c style formatting specifiers are detected

5 participants

@FabianWolff@rust-highfive@petrochenkov@bors@rustbot