Skip to content

wrong hint about removing .into_iter() in zip #11819

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
borisfaure opened this issue Nov 15, 2023 · 3 comments · Fixed by #14035
Closed

wrong hint about removing .into_iter() in zip #11819

borisfaure opened this issue Nov 15, 2023 · 3 comments · Fixed by #14035
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

@borisfaure
Copy link

Summary

I tried running cargo clippy --fix on https://github.com/TeXitoi/keyberon but I got the following issue.

 cargo clippy --fix --lib -p keyberon                                                                                                                                                                                                                                                                                  
    Checking keyberon v0.2.0 (/home/boris/stuff/kb/keyberon)
warning: failed to automatically apply fixes suggested by rustc to crate `keyberon`

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

  * /home/boris/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/zip.rs
  * src/debounce.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[E0277]: `T` is not an iterator
   --> src/debounce.rs:108:22
    |
108 |                     .zip(self.cur)
    |                      ^^^ `T` is not an iterator
    |
    = note: required for `T` to implement `core::iter::IntoIterator`
help: consider further restricting this bound
    |
39  | impl<T: PartialEq + core::iter::Iterator> Debouncer<T> {
    |                   ++++++++++++++++++++++

error[E0599]: the method `enumerate` exists for struct `Zip<<&T as IntoIterator>::IntoIter, T>`, but its trait bounds were not satisfied
   --> src/debounce.rs:109:22
    |
106 | /                 self.new
107 | |                     .into_iter()
108 | |                     .zip(self.cur)
109 | |                     .enumerate()
    | |                     -^^^^^^^^^ method cannot be called on `Zip<<&T as IntoIterator>::IntoIter, T>` due to unsatisfied trait bounds
    | |_____________________|
    |
    |
   ::: /home/boris/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/zip.rs:13:1
    |
13  |   pub struct Zip<A, B> {
    |   -------------------- doesn't satisfy `_: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `T: core::iter::Iterator`
            which is required by `core::iter::Zip<<&'a T as core::iter::IntoIterator>::IntoIter, T>: core::iter::Iterator`
            `core::iter::Zip<<&'a T as core::iter::IntoIterator>::IntoIter, T>: core::iter::Iterator`
            which is required by `&mut core::iter::Zip<<&'a T as core::iter::IntoIterator>::IntoIter, T>: core::iter::Iterator`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
   --> src/debounce.rs:108:26
    |
108 |                     .zip(self.cur.into_iter())
    |                          ^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `self.cur`
    |
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
   --> /home/boris/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:642:12
    |
642 |         U: IntoIterator,
    |            ^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
    = note: `#[warn(clippy::useless_conversion)]` on by default

warning: `keyberon` (lib) generated 1 warning (run `cargo clippy --fix --lib -p keyberon` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 1.61s

Please note that this warning

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
   --> src/debounce.rs:111:43
    |
111 |                         o.into_iter().zip(n.into_iter()).enumerate().filter_map(
    |                                           ^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `n`
    |
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
   --> /home/boris/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:642:12
    |
642 |         U: IntoIterator,
    |            ^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

warning: `keyberon` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p keyberon` to apply 2 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.55s

is valid and can be fixed with the suggestion.

Lint Name

useless_conversion

Reproducer

A reproducer can be found on:
https://github.com/borisfaure/keyberon/tree/explicit_into_iter

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

@borisfaure borisfaure 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 Nov 15, 2023
@rustbot
Copy link
Collaborator

rustbot commented Nov 15, 2023

Error: Label I-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@Jarcho
Copy link
Contributor

Jarcho commented Nov 15, 2023

In the code self.cur has type T, but only &'a T: IntoIterator. The correct suggestion is &self.cur.

@Jarcho Jarcho added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 15, 2023
@borisfaure
Copy link
Author

A simple test is the following:

mod issue11819 {
    pub struct MyStruct<T> {
        my_field: T,
    }
    impl<T> MyStruct<T> {
        pub fn methodic<'a, U, X>(&'a mut self, iter: X)
        where
            &'a T: IntoIterator<Item = U>,
            X: Iterator<Item = U>,
        {
            let _ = iter.chain(self.my_field.into_iter());
            // Correct fix is:
            //let _ = iter.chain(&self.my_field);
        }
    }
}

github-merge-queue bot pushed a commit that referenced this issue Jan 23, 2025
…er()` calls (#14035)

Fix #11819

changelog: [`useless_conversion`]: add necessary adjustments to
suggestion to remove redundant `.into_iter()` calls
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
3 participants