Skip to content

Incorrect suggestion of string_lit_as_bytes lint #4494

Open
@ordovicia

Description

@ordovicia

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"[..].

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions