rust-lang / rust-lang/rust-clippy

lines_filter_map_ok: Incorrect suggestion when `Result` is a type alias

Open
#14,128 1 comment 0 reactions 1 assignee View on GitHub

@profetia is already working on this.

Since Feb 12, 2025.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description
Summary

It seems this lint always suggests map_while(Result::ok), but it is incorrect if Result in the current scope is a type alias other than std::io::Result.

Lint Name

lines_filter_map_ok

Reproducer

I tried this code:

#![warn(clippy::lines_filter_map_ok)]

use std::{fs::File, io::*};

pub struct MyError;
pub type Result<T> = std::result::Result<T, MyError>;

fn _f() {
    let _lines = BufReader::new(File::open("some-path").unwrap())
        .lines()
        .filter_map(|res| res.ok());
}

I saw this happen:

warning: `filter_map()` will run forever if the iterator repeatedly produces an `Err`
  --> src/lib.rs:11:10
   |
11 |         .filter_map(|res| res.ok());
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `map_while(Result::ok)`
   |
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
  --> src/lib.rs:9:18
   |
9  |       let _lines = BufReader::new(File::open("some-path").unwrap())
   |  __________________^
10 | |         .lines()
   | |________________^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(clippy::lines_filter_map_ok)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

However, the suggested code (map_while(Result::ok)) cases compile error:

error[E0631]: type mismatch in function arguments
    --> src/lib.rs:11:20
     |
11   |         .map_while(Result::ok);
     |          --------- ^^^^^^^^^^
     |          |         |
     |          |         expected due to this
     |          |         found signature defined here
     |          required by a bound introduced by this call
     |
     = note: expected function signature `fn(std::result::Result<_, std::io::Error>) -> _`
                found function signature `fn(std::result::Result<_, MyError>) -> _`
note: required by a bound in `map_while`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1266:12
     |
1263 |     fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
     |        --------- required by a bound in this associated function
...
1266 |         P: FnMut(Self::Item) -> Option<B>,
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::map_while`
help: consider wrapping the function in a closure
     |
11   |         .map_while(|arg0: std::result::Result<String, std::io::Error>| Result::ok(/* std::result::Result<String, MyError> */));
     |                    +++++++++++++++++++++++++++++++++++++++++++++++++++           ++++++++++++++++++++++++++++++++++++++++++++

I expected to see this happen: suggest map_while(|res| res.ok())

playground

Version
rustc 1.86.0-nightly (854f22563 2025-01-31)
binary: rustc
commit-hash: 854f22563c8daf92709fae18ee6aed52953835cd
commit-date: 2025-01-31
host: aarch64-apple-darwin
release: 1.86.0-nightly
LLVM version: 19.1.7
Additional Labels

@rustbot label +C-bug +I-suggestion-causes-error

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.