Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::lines_filter_map_ok: Checks for usage of lines.filter_map(Result::ok) or lines.flat_map(Result::ok) when lines has type std::io::Lines
What it meansChecks for usage of lines.filter_map(Result::ok) or lines.flat_map(Result::ok) when lines has type std::io::Lines. It is in the suspicious group and warns by default.
Does it matter?
Lines instances might produce a never-ending stream of Err, in which case filter_map(Result::ok) will enter an infinite loop while waiting for an Ok variant. Calling next() once is sufficient to enter the infinite loop, even in the absence of explicit loops in the user code. This situation can arise when working with user-provided paths. On some platforms, std::fs::File::open(path) might return Ok(fs) even when path is a directory, but any later attempt to read from fs will return an error.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let mut lines = BufReader::new(File::open("some-path")?).lines().filter_map(Result::ok);
// If "some-path" points to a directory, the next statement never terminates:
let first_line: Option<String> = lines.next();
Write this instead
let mut lines = BufReader::new(File::open("some-path")?).lines().map_while(Result::ok);
let first_line: Option<String> = lines.next();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::lines_filter_map_ok)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
lines_filter_map_ok = "allow"One run
cargo clippy -- -A clippy::lines-filter-map-okThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for usage of lines.filter_map(Result::ok) or lines.flat_map(Result::ok)
when lines has type std::io::Lines.
Why is this bad?
Lines instances might produce a never-ending stream of Err, in which case
filter_map(Result::ok) will enter an infinite loop while waiting for an
Ok variant. Calling next() once is sufficient to enter the infinite loop,
even in the absence of explicit loops in the user code.
This situation can arise when working with user-provided paths. On some platforms,
std::fs::File::open(path) might return Ok(fs) even when path is a directory,
but any later attempt to read from fs will return an error.
Known problems
This lint suggests replacing filter_map() or flat_map() applied to a Lines
instance in all cases. There are two cases where the suggestion might not be
appropriate or necessary:
- If the
Linesinstance can never produce any error, or if an error is produced only once just before terminating the iterator, usingmap_while()is not necessary but will not do any harm. - If the
Linesinstance can produce intermittent errors then recover and produce successful results, usingmap_while()would stop at the first error.
Example
let mut lines = BufReader::new(File::open("some-path")?).lines().filter_map(Result::ok);
// If "some-path" points to a directory, the next statement never terminates:
let first_line: Option<String> = lines.next();
Use instead:
let mut lines = BufReader::new(File::open("some-path")?).lines().map_while(Result::ok);
let first_line: Option<String> = lines.next();
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- iter_out_of_boundsthis `.take()` call takes more items than the iterator will produce
- join_absolute_pathsChecks for calls to Path::join that start with a path separator (\\ or /)
- let_underscore_futurenon-binding `let` on a future
- macro_metavars_in_unsafeLooks for macros that expand metavariables in an unsafe block
- manual_unwrap_or_defaultmatch can be simplified with `.unwrap_or_default()`
- mismatched_bit_width_typepossible buggy implementation of `bit_width`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::iter_out_of_boundsthis `.take()` call takes more items than the iterator will produceRead it next →Did this answer your question?