Skip to content

compilation failure after applying lint filter_map_identity #12653

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
antonilol opened this issue Apr 9, 2024 · 2 comments · Fixed by #13826
Closed

compilation failure after applying lint filter_map_identity #12653

antonilol opened this issue Apr 9, 2024 · 2 comments · Fixed by #13826
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@antonilol
Copy link
Contributor

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 antonilol added the C-bug Category: Clippy is not doing the correct thing label Apr 9, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 9, 2024
@antonilol
Copy link
Contributor Author

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

github-merge-queue bot pushed a commit that referenced this issue Dec 17, 2024
…ray (#13826)

fix #12653

changelog: [`filter_map_identity`]: don't lint for creating an iterator
from an empty array
@antonilol
Copy link
Contributor Author

To me it looks like #13826 does not fix the general case of this bug, that removing filter_map will remove type hints for the compiler. The lint will now not fire when a pattern like [].some_method().filter_map(...) is seen. (Should be easy to now craft a false negative example, right?)
There are more ways to create iterators that must have their type inferred like [].into_iter() does in my original post. Examples are core::iter::empty(), core::iter::repeat_with(Default::default), core::iter::repeat(123u8).map(Into::into), etc.
Shouldn't the fix be to add a type hint? Specifically that the iterator Item type is Option<_>, because that information is removed by changing filter_map(|x| x) into flatten().
Clippy even suggests this in the output after realizing it does not compile:

...
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,
...
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:2:35
  |
2 |     core::iter::repeat(123u8).map(Into::into).flatten()
  |                                   ^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Into`
  |
help: consider specifying the generic argument
  |
2 |     core::iter::repeat(123u8).map(Into::<T>::into).flatten()
  |                                       +++++
...

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-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