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

Improvement to write! macro with wrong type for string literal argument #86865

Closed
insanitybit opened this issue Jul 4, 2021 · 1 comment · Fixed by #87441
Closed

Improvement to write! macro with wrong type for string literal argument #86865

insanitybit opened this issue Jul 4, 2021 · 1 comment · Fixed by #87441
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@insanitybit
Copy link

insanitybit commented Jul 4, 2021

Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4c5ff2fc026afd1a666633cba4c31865

pub fn example(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, b"Foo{}", "bar")
}

The current output is:

error: format argument must be a string literal
 --> src/lib.rs:4:15
   |
4 |     write!(f, b"Foo{}", "bar")
   |               ^^^^^^^^
   |
help: you might be missing a string literal to format with
   |
4 |     write!(f, "{} {}", b"Foo{}", "bar")

Ideally the output should look like:

 error: format argument must be a string literal, **found byte literal**
  --> src/lib.rs:4:15
   |
 4 |     write!(f, b"Foo{}", "bar")
   |               ^^^^^^^^
   |
 help: you might be missing a string literal to format with
   |
4 |     write!(f, "{} {}", b"Foo{}", "bar")
 help: you may have meant to pass in a string literal instead of a byte literal
   |
 4 |     write!(f, b"Foo{}", "bar")
  |               ^ try removing this 'b'
4 |     write!(f, "Foo{}", "bar")

So this is super trivial, but I just ran into this and I haven't used write! in a while so I sorta did a double take and it took me a second to figure out why it was suggesting what it was suggesting.

I was just refactoring some code that had used a byte literal beforehand. Two things would help:

  1. Telling me that I passed in a byte literal where a string literal was expected (or whatever else - right now it only says what's expected, not what was provided). IMO this alone would have been a big help and I would have spent 0 cycles thinking about what the problem could be.

  2. Since this is, to a human, clearly a case where I intended the byte literal to be the format string, in an ideal world that would be pointed out to me. I realize that this is open to interpretation - is it "clearly" the case because it's a byte literal? Or a byte literal with {}? IDK! Food for thought.

@insanitybit insanitybit added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 4, 2021
@ibraheemdev
Copy link
Member

@rustbot claim

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 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.

2 participants