Lint against `Vec` deref in `Read::read`

Open
#13,546 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
tooling

Research direction

Start by reproducing the linked Rust Playground example and inspect how rust-clippy implements lints for calls to Read::read. The work is done when implicit Vec dereferencing at reader.read(&mut buf) is diagnosed and the suggestion uses reader.read(buf.as_mut_slice()), with tests covering the intended behavior.

Written by the indexing model from the issue text.

Description

A-lint
What it does

As the title says, lint against implicit Vec deref in calls to Read::read.

Advantage

It is easy to mess up when using Read::read with a Vec as users may think that Read::read takes &mut Vec<u8> and therefore updates the len value of Vec, but instead it will return the number of bytes read. It also points users towards rethinking their usage of Vec, as the example could have the Vec replaced with a stack array for better performance. This will also help catch incorrect usages of Read::read, which use an empty Vec instead zero initializing.

Drawbacks

It may lint on correct code, but it recommends an easier to read version.

Example

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5dcfb936ece9b676f659f388d0e61b5c

The example should lint at reader.read(&mut buf) as buf is being implicitly dereferenced into &mut [u8].
It should suggest replacing the implicit deref with reader.read(buf.as_mut_slice()).

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.