Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DorianCoding opened this issue May 26, 2024 · 1 comment
Open

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

DorianCoding opened this issue May 26, 2024 · 1 comment
Labels
A-lint Area: New lints

Comments

@DorianCoding
Copy link

DorianCoding commented May 26, 2024

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();
@DorianCoding DorianCoding added the A-lint Area: New lints label May 26, 2024
@DorianCoding DorianCoding changed the title lines_count should warn in case of Err lines().count() should warn in case of Err May 26, 2024
@scottlamb
Copy link

scottlamb commented Mar 27, 2025

+1 to the idea of having a lint on Iterator<Item = Result<_, _>>::count. I'd prefer the suggested alternative be to bubble up the error rather than swallow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants