rust-lang / rust-lang/rust-clippy
Incorrect suggestion of `string_lit_as_bytes` lint
Nobody has claimed this yet.
- 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
}
Clippy should allow the above case or suggest &b"string literal"[..].
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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