rust-lang / rust-lang/rust-clippy

`Write::write_all` on vec instead of `extend_from_slice`

Open
#15,323 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.