rust-lang / rust-lang/rust-clippy

lines().count() should warn in case of Err

Open
#12,858 1 comment 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

As stated here, Lines can enter an infinite loop if nothing like map_while is used when used on directory. Therefore something like lines().count() may enter an infinite loop. Therefore, it would be good if a warn can be added to fix like : lines().map_while(Result::ok).count().

Advantage
  • It will avoid entering a loop when used on directory in certain platforms.
Drawbacks
  • Some users may be interested in getting the count with lines with errors included.
Example
let linecount = BufReader::new(File::open("some-path")?).lines().count();

Could be warned and this used instead :

let linecount = BufReader::new(File::open("some-path")?).lines().map_while(Result::ok).count();

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

Start by reviewing the linked map_while lint documentation and the issue's lines().count() example. Determine how the warning should handle line-reading errors and the case where callers intentionally count them; done means the requested case is covered by a Clippy warning with an appropriate suggested alternative.

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.