Skip to content

False positive in map_identity #9122

Closed
@djc

Description

@djc

Summary

With the code below, clippy suggests removing the map_err() call. However, in this case map_err() is the only thing telling the compiler the type of the E in the relevant Result<T, E>, so removing it will result in an error. This lint is machine-applicable, so that generates an error.

I guess this regressed from when #8478 was fixed.

Lint Name

map_identity

Reproducer

I tried this code:

#[test]
fn test() {
    block_on_all(async move { Ok(()) })
        .map_err(|err: std::io::Error| err)
        .unwrap();
}

fn block_on_all<F>(f: F) -> F::Output
where
    F: std::future::Future,
{
    current_thread_runtime().block_on(f)
}

fn current_thread_runtime() -> tokio::runtime::Runtime {
    let mut builder = tokio::runtime::Builder::new_current_thread();

    #[cfg(feature = "aio")]
    builder.enable_io();

    builder.build().unwrap()
}

I saw this happen:

warning: unnecessary map of the identity function
 --> tests/foo.rs:3:40
  |
3 |       block_on_all(async move { Ok(()) })
  |  ________________________________________^
4 | |         .map_err(|err: std::io::Error| err)
  | |___________________________________________^ help: remove the call to `map_err`
  |
  = note: `#[warn(clippy::map_identity)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity

I expected to see this happen: nothing, or potentially it could instead suggest turbofishing the Ok() call, as in Ok::<_, std::io::Error>(()).

Version

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: aarch64-apple-darwin
release: 1.62.0
LLVM version: 14.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions