Skip to content

False positive in map_identity #9122

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

Closed
djc opened this issue Jul 5, 2022 · 0 comments · Fixed by #11521
Closed

False positive in map_identity #9122

djc opened this issue Jul 5, 2022 · 0 comments · Fixed by #11521
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@djc
Copy link
Contributor

djc commented Jul 5, 2022

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

@djc djc added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 5, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 5, 2022
@bors bors closed this as completed in e230f19 Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants