Skip to content

unnecessary_operation: breaks type inference #15173

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::unnecessary_operation

this code:

// Regression test for #88118. Used to ICE.
//@ edition:2021
//@ check-pass

fn foo<MsU>(handler: impl FnOnce() -> MsU + Clone + 'static) {
    Box::new(move |value| {
        (|_| handler.clone()())(value);
        None
    }) as Box<dyn Fn(i32) -> Option<i32>>;
}

fn main() {}

caused the following diagnostics:

    Checking _issue_88118 v0.1.0 (/tmp/icemaker_global_tempdir.EuHmIYit9wj1/icemaker_clippyfix_tempdir.Fu7tu2I8GD2t/_issue_88118)
warning: unnecessary operation
 --> src/main.rs:6:5
  |
6 | /     Box::new(move |value| {
7 | |         (|_| handler.clone()())(value);
8 | |         None
9 | |     }) as Box<dyn Fn(i32) -> Option<i32>>;
  | |__________________________________________^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
  = note: requested on the command line with `--force-warn clippy::unnecessary-operation`
help: statement can be reduced to
  |
6 ~     Box::new(move |value| {
7 +         (|_| handler.clone()())(value);
8 +         None
9 +     });
  |

warning: `_issue_88118` (bin "_issue_88118") generated 1 warning (run `cargo clippy --fix --bin "_issue_88118"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s

However after applying these diagnostics, the resulting code:

// Regression test for #88118. Used to ICE.
//@ edition:2021
//@ check-pass

fn foo<MsU>(handler: impl FnOnce() -> MsU + Clone + 'static) {
    Box::new(move |value| {
        (|_| handler.clone()())(value);
        None
    });
}

fn main() {}

no longer compiled:

    Checking _issue_88118 v0.1.0 (/tmp/icemaker_global_tempdir.EuHmIYit9wj1/icemaker_clippyfix_tempdir.Fu7tu2I8GD2t/_issue_88118)
error[E0282]: type annotations needed
 --> src/main.rs:8:9
  |
8 |         None
  |         ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
  |
help: consider specifying the generic argument
  |
8 |         None::<T>
  |             +++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `_issue_88118` (bin "_issue_88118" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_issue_88118` (bin "_issue_88118") due to 1 previous error

Version:

rustc 1.90.0-nightly (5e749eb66 2025-06-29)
binary: rustc
commit-hash: 5e749eb66f93ee998145399fbdde337e57cd72ef
commit-date: 2025-06-29
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-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