Skip to content

questionable write_literal warning: write!(f, "{}", "{integer}") #6768

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

Closed
matthiaskrgr opened this issue Feb 20, 2021 · 3 comments · Fixed by #6821
Closed

questionable write_literal warning: write!(f, "{}", "{integer}") #6768

matthiaskrgr opened this issue Feb 20, 2021 · 3 comments · Fixed by #6821
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@matthiaskrgr
Copy link
Member

Lint name: write_literal

I tried this code:

use std::fmt;

struct S {}

impl fmt::Display for S {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", "{integer}")
    }
}

fn main() {
    println!("{}", S {});
}
warning: literal with an empty format string
 --> src/main.rs:7:25
  |
7 |         write!(f, "{}", "{integer}")
  |                         ^^^^^^^^^^^
  |
  = note: `#[warn(clippy::write_literal)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#write_literal

In this code we use dummy format string {} to insert the string literal "{integer}" because if we had
write!(f, "{ìnteger}"), the {integer} would not be parsed as a raw literal(?):

error: there is no argument named `integer`
 --> src/main.rs:7:20
  |
7 |         write!(f, "{integer}")
  |                    ^^^^^^^^^

This is quite an interesting edge-case :)

I'm not sure what the best solution is here, if we can somehow provide a better suggestion or should just ignore literals that enclose anything in curly braces.

Meta

  • cargo clippy -V: clippy 0.1.52 (9b471a3 2021-02-19)
  • rustc -Vv:

rustc 1.52.0-nightly (9b471a3 2021-02-19)
binary: rustc
commit-hash: 9b471a3
commit-date: 2021-02-19
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't I-false-positive Issue: The lint was triggered on code it shouldn't have and removed I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Feb 20, 2021
@ghost
Copy link

ghost commented Feb 21, 2021

Unless I misunderstood, I can (and prefer to) escape the curly braces instead (playground):

use std::fmt;

struct S {}

impl fmt::Display for S {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{integer}}")
    }
}

fn main() {
    println!("{}", S {});
}

So... please do not "ignore literals that enclose anything in curly braces", at least provide a way to opt-in to the warning.

@matthiaskrgr
Copy link
Member Author

Ah, I forgot about the escaping with double curly braces!
Shall I close this again? :)

@camsteffen
Copy link
Contributor

No, this is still a false positive. I think this is related to pre_expansion_pass.

@camsteffen camsteffen added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label Feb 22, 2021
@bors bors closed this as completed in d695bfc Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants