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

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

Closed
diondokter opened this issue May 19, 2020 · 2 comments · Fixed by rust-lang/rust#72637 or #5672
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing E-needs-test Call for participation: writing tests good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion

Comments

@diondokter
Copy link

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
@phansch phansch self-assigned this May 19, 2020
@phansch phansch added C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels May 19, 2020
@euclio
Copy link
Contributor

euclio commented May 26, 2020

Just posted rust-lang/rust#72637 which fixes this.

@phansch phansch removed their assignment May 27, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue May 30, 2020
@phansch phansch added the E-needs-test Call for participation: writing tests label May 31, 2020
@phansch
Copy link
Member

phansch commented May 31, 2020

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

@phansch phansch reopened this May 31, 2020
@phansch phansch added the good-first-issue These issues are a good way to get started with Clippy label May 31, 2020
@phansch phansch self-assigned this Jun 1, 2020
@bors bors closed this as completed in 153ef1c Jun 2, 2020
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-needs-test Call for participation: writing tests good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion
Projects
None yet
3 participants