Rust-clippy: False positive for 'string_lit_as_bytes' in conjunction with 'env!'

Created on 19 May 2020  路  2Comments  路  Source: rust-lang/rust-clippy

Hello all, I've found a false positive for a lint:

warning: calling `as_bytes()` on a string literal
  --> xxx\src\xxx.rs:81:35
   |
81 |             let current_version = env!("CARGO_PKG_VERSION").as_bytes();
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `benv!("CARGO_PKG_VERSION")`
   |
   = note: `#[warn(clippy::string_lit_as_bytes)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes

warning: 1 warning emitted

clippy 0.0.212 (7ebd87a7a 2020-05-08)

A binary literal can't be used with the env! macro and the suggestion to fix it obviously doesn't work. In this case I would say that calling as_bytes() is the proper thing to do.

I've tested whether or not this is a general macro problem, but that doesn't seem to be the case:

macro_rules! test {
    ($x: expr) => {
        $x
    }
}
warning: calling `as_bytes()` on a string literal
  --> rtls-uwb\src\messages\join_request.rs:88:35
   |
88 |             let current_version = test!("CARGO_PKG_VERSION").as_bytes();
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"CARGO_PKG_VERSION"`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes

warning: 3 warnings emitted
L-bug L-suggestion-causes-error P-needs-test T-macros good-first-issue

Most helpful comment

Just posted https://github.com/rust-lang/rust/pull/72637 which fixes this.

All 2 comments

Just posted https://github.com/rust-lang/rust/pull/72637 which fixes this.

Going to re-open the issue so that we don't forget to add a regression test

Was this page helpful?
0 / 5 - 0 ratings