Rust-clippy: write_literal documentation unhelpful

Created on 3 Jul 2018  路  4Comments  路  Source: rust-lang/rust-clippy

This code (playground):

macro_rules! impl_fmt_debug {
    ($id:ident) => {
        impl std::fmt::Debug for $id {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "|{}|", stringify!($id))
            }
        }
    }
}
struct Foo();
impl_fmt_debug!(Foo);
fn main() {}

produces this lint:

warning: writing a literal with an empty format string
  --> src/main.rs:5:35
   |
5  |                 write!(f, "|{}|", stringify!($id))
   |                                   ^^^^^^^^^^^^^^^
...
13 | impl_fmt_debug!(Foo);
   | --------------------- in this macro invocation
   |
   = note: #[warn(write_literal)] on by default
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.211/index.html#write_literal

The lint documentation is pretty unhelpful about how to fix this, that is, how can I interpolate the result of stringify!($id) into the format string for this particular case within a macro?

L-bug

Most helpful comment

The playground code does not trigger this lint any more. (Probably related to https://github.com/rust-lang-nursery/rust-clippy/pull/2949).

All 4 comments

cc @oli-obk @Manishearth this might be a bug

This is documented in the Known Problems section of the lint. Nevertheless it is indeed a bug, which would be nice if it could be fixed!

well.... you could do concat!("|", stringify!($id), "|"). But yea, we should not produce this lint for string literals produced by macros

The playground code does not trigger this lint any more. (Probably related to https://github.com/rust-lang-nursery/rust-clippy/pull/2949).

Was this page helpful?
0 / 5 - 0 ratings