-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #89340 - FabianWolff:issue-89173, r=petrochenkov
Improve error message for `printf`-style format strings Fixes #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 #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``
- Loading branch information
Showing
6 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Regression test for #89173: Make sure a helpful note is issued for | ||
// printf-style format strings using `*` to specify the width. | ||
|
||
fn main() { | ||
let num = 0x0abcde; | ||
let width = 6; | ||
print!("%0*x", width, num); | ||
//~^ ERROR: multiple unused formatting arguments | ||
//~| NOTE: multiple missing formatting specifiers | ||
//~| NOTE: argument never used | ||
//~| NOTE: argument never used | ||
//~| NOTE: format specifiers use curly braces, and you have to use a positional or named parameter for the width | ||
//~| NOTE: printf formatting not supported | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error: multiple unused formatting arguments | ||
--> $DIR/issue-89173.rs:7:20 | ||
| | ||
LL | print!("%0*x", width, num); | ||
| ------ ^^^^^ ^^^ argument never used | ||
| | | | ||
| | argument never used | ||
| multiple missing formatting specifiers | ||
| | ||
note: format specifiers use curly braces, and you have to use a positional or named parameter for the width | ||
--> $DIR/issue-89173.rs:7:13 | ||
| | ||
LL | print!("%0*x", width, num); | ||
| ^^^^ | ||
= note: printf formatting not supported; see the documentation for `std::fmt` | ||
|
||
error: aborting due to previous error | ||
|