profile
viewpoint

Ask questionsIncorrect suggestion of `string_lit_as_bytes` lint

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

rust-lang/rust-clippy

Answer questions mahkoh

The lint should instead be removed. The only justification for the lint given is that using byte strings is shorter. &b"string literal"[..] is perl-tier.

useful!

Related questions

manual_mul_add suggests broken code hot 1
clippy::implicit_hasher shouldn't warn on From<Thing> for HashMap<K, V> hot 1
False positive for `boxed_local` with trait default methods hot 1
Error linting project using wasm-bindgen on nighlty hot 1
Unrecognized escape sequence hot 1
Pin<&mut self> triggers "wrong_self_convention" hot 1
clippy::op_ref false positive with deref coercions hot 1
unexpectedly panick when compiling mysql_common hot 1
redundant-clone false-positive for structopt attrs hot 1
source:https://uonfu.com/
Github User Rank List