rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.28k stars 1.52k forks source link

compilation failure after applying lint `filter_map_identity` #12653

Open antonilol opened 5 months ago

antonilol commented 5 months ago

Summary

title, the following code depends on filter_map to tell rustc that the type is Option<_>, which is removed causing a compilation error

Reproducer

I tried this code:

fn iter() -> impl Iterator<Item = u8> {
    [].into_iter().filter_map(|x| x)
}

I expected to see this happen: no lint (see #9377) or a working one

Instead, this happened: command: cargo clippy --fix --allow-no-vcs

    Checking rust-playground v0.1.0 (/home/antoni/code/rust-playground)
warning: failed to automatically apply fixes suggested by rustc to crate `rust_playground`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:2:8
  |
2 |     [].into_iter().flatten()
  |     -- ^^^^^^^^^
  |     |
  |     type must be known at this point
  |
help: try using a fully qualified path to specify the expected types
  |
2 |     <[_; 0] as std::iter::IntoIterator>::into_iter([]).flatten()
  |     +++++++++++++++++++++++++++++++++++++++++++++++  ~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: use of `filter_map` with an identity function
 --> src/main.rs:2:20
  |
2 |     [].into_iter().filter_map(|x| x)
  |                    ^^^^^^^^^^^^^^^^^ help: try: `flatten()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_identity
  = note: `#[warn(clippy::filter_map_identity)]` on by default

warning: `rust-playground` (bin "rust-playground") generated 1 warning (run `cargo clippy --fix --bin "rust-playground"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `rust_playground`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:2:8
  |
2 |     [].into_iter().flatten()
  |     -- ^^^^^^^^^
  |     |
  |     type must be known at this point
  |
help: try using a fully qualified path to specify the expected types
  |
2 |     <[_; 0] as std::iter::IntoIterator>::into_iter([]).flatten()
  |     +++++++++++++++++++++++++++++++++++++++++++++++  ~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: `rust-playground` (bin "rust-playground" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s

Version

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

antonilol commented 5 months ago

filter_map(identity) is a recommended use of core::convert::identity, see docs, are docs not tested to have warning lints triggered? this one will apply without error