Skip to content

False positive for 'string_lit_as_bytes' in conjunction with 'env!' #5619

Closed
rust-lang/rust
#72637
@diondokter

Description

@diondokter

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingE-needs-testCall for participation: writing testsI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-macrosType: Issues with macros and macro expansiongood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions