Skip to content
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

not all c style formatting specifiers are detected #89173

Closed
lcnr opened this issue Sep 22, 2021 · 4 comments · Fixed by #89340
Closed

not all c style formatting specifiers are detected #89173

lcnr opened this issue Sep 22, 2021 · 4 comments · Fixed by #89340
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented Sep 22, 2021

i've tried

fn main() {
    let num = 0x0abcde;
    let width = 6;
    print!("%0*x", width, num);
}

i expected:

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

the actual error message does not contain such a help message:

error: multiple unused formatting arguments
 --> src/main.rs:4:20
  |
4 |     print!("%0*x", width, num);
  |            ------  ^^^^^  ^^^ argument never used
  |            |       |
  |            |       argument never used
  |            multiple missing formatting specifiers
@lcnr lcnr added the C-bug Category: This is a bug. label Sep 22, 2021
@lcnr
Copy link
Contributor Author

lcnr commented Sep 22, 2021

note that the following does suggest {:06x}

fn main() {
    let num = 0x0abcde;
    print!("%06x", num);
}

@jamesmunns
Copy link
Member

Is there a typo in your snippet?

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

The * looks out of place to me, even for printf formatting. It's also not matched in your "expected" output (the * is not there).

@lcnr
Copy link
Contributor Author

lcnr commented Sep 22, 2021

no, * is used to get the width specifier from the arguments, which is why the equivalent rust specifier would be {1:00$x} here. see https://rust.godbolt.org/z/MvdsPTWTa for working c code

fixed my expected output though, copied a similar example where it does work

@jamesmunns
Copy link
Member

Wow! Thanks for explaining, TIL (in both Rust and C!)

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Sep 24, 2021
ehuss added a commit to ehuss/rust that referenced this issue Sep 30, 2021
…nkov

Improve error message for `printf`-style format strings

Fixes rust-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-dead added a commit to fee1-dead-contrib/rust that referenced this issue Oct 1, 2021
…nkov

Improve error message for `printf`-style format strings

Fixes rust-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``
@bors bors closed this as completed in d388428 Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants