rust-lang / rust-lang/rust-clippy

New lint: Bound by BufRead instad of bounding by Read and then instantiating BufReader

Open
#7,061 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

If a generic function takes R: io::Read and then wraps it in io::BufReader warn that this may introduce unnecessary allocation and copying. Recommend io::BufRead bound instead.

Categories (optional)
  • Kind: perf

If the caller already has a type implementing BufRead then wrapping introduces allocation and also additional copying of data between the buffers.

Drawbacks

The change is backwards-incompatible and major version must be bumped if the bound is in public API. All callers that do not have buffered readers need to wrap the reader in BufReader themselves.

Example
fn parse_foo<R: io::Read>(reader: R) -> io::Result<Foo> {
    let mut reader = io::BufReader::new(reader);

    // Rest of the parsing logic ...
}

Could be written as:

fn parse_foo<R: io::BufRead>(mut reader: R) -> io::Result<Foo> {

    // Rest of the parsing logic ...
}

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

The issue provides Rust examples but names no implementation files, tests, or entry point. Start by locating the existing lint implementation and its tests in rust-clippy, then verify the lint detects generic io::Read parameters wrapped in io::BufReader and recommends an io::BufRead bound, including the stated public-API compatibility concern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
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.