rust-lang / rust-lang/rust-clippy
`Write::write_all` on vec instead of `extend_from_slice`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
let mut ca_string = b"fixed:r:".to_vec();
ca_string.write_all(b"some stuff").unwrap();
I was writing rust after a bit of a break from it and forgot what extend_from_slice was called, so I wound up abusing .write till I googled it. I feel like Clippy could probably catch this pattern.
Advantage
- Simplifies the code to consistently use reasonable vector operations and not have any unreachable error handling noise.
Drawbacks
Might be annoying for code intended to be generic that just happens to be concretely using a Vec (say, while being written initially); definitely needs to not apply to generic code using Vec as a writer.
Example
let mut ca_string = b"fixed:r:".to_vec();
ca_string.write_all(b"some stuff").unwrap();
Could be written as:
let mut ca_string = b"fixed:r:".to_vec();
ca_string.extend_from_slice(b"some stuff");
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
No file or test path is named. Start by locating existing Clippy lints and tests for Vec methods and Write::write_all, then use the examples to define the concrete-Vec case. Verify that generic code using Vec as a writer is excluded, and that the example is diagnosed with extend_from_slice as the replacement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100