rust-lang / rust-lang/rust-clippy

Incorrect suggestion of `string_lit_as_bytes` lint

Open
#4,494 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug E-medium I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Clippy version: clippy 0.0.212 (e3cb40e 2019-06-25)

Clippy suggests using a byte string literal b"string literal" instead of "string literal".as_bytes().
The suggestion does not compile in the following case because the type of byte string literal &[u8; N] does not implement std::io::Read.

fn f(_: impl std::io::Read) {
}

fn main() {
    f("string literal".as_bytes());
//    ^^^^^^^^^^^^^ help: consider using a byte string literal instead: `b"string literal"`

    // f(b"string literal");    // does not compile
    // ^ the trait `std::io::Read` is not implemented for `&[u8; 14]`
    
    f(&b"string literal"[..]);  // compiles
}

Playground

Clippy should allow the above case or suggest &b"string literal"[..].

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the string_lit_as_bytes lint and reproduce the provided std::io::Read example. Check how its suggestion is generated for byte string literals, then verify that the revised suggestion either compiles in this case or the lint is not emitted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.