Skip to content

while_let_on_iterator does not consider the borrow checker #6231

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
jyn514 opened this issue Oct 26, 2020 · 3 comments · Fixed by #6966
Closed

while_let_on_iterator does not consider the borrow checker #6231

jyn514 opened this issue Oct 26, 2020 · 3 comments · Fixed by #6966
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 T-MIR Type: This lint will require working with the MIR

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 26, 2020

This output is from running clippy on https://github.com/rust-lang/stdarch/blob/598e632e8f62ff5054dfe0d0fabb2a03ecee0a3d/crates/stdarch-test/src/disassembly.rs#L102-L116. The relevant snippet is

    while let Some(header) = cached_header.take().or_else(|| lines.next()) {
        while let Some(instruction) = lines.next() {
        }
    }
$ cargo clippy --fix -Z unstable-options
after fixes were automatically applied the compiler reported errors within these files:

  * crates/stdarch-test/src/disassembly.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/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[E0382]: borrow of moved value: `lines`
   --> crates/stdarch-test/src/disassembly.rs:117:59
    |
105 |     let mut lines = output.lines();
    |         --------- move occurs because `lines` has type `std::str::Lines<'_>`, which does not implement the `Copy` trait
...
117 |     while let Some(header) = cached_header.take().or_else(|| lines.next()) {
    |                                                           ^^ ----- borrow occurs due to use in closure
    |                                                           |
    |                                                           value borrowed here after move
...
125 |         for instruction in lines {
    |                            -----
    |                            |
    |                            value moved here, in previous iteration of loop
    |                            help: consider borrowing to avoid moving into the for loop: `&lines`

error: aborting due to previous error

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

warning: this loop could be written as a `for` loop
   --> crates/stdarch-test/src/disassembly.rs:125:9
    |
125 |         while let Some(instruction) = lines.next() {
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for instruction in lines`
    |
    = note: `#[warn(clippy::while_let_on_iterator)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator

warning: this loop could be written as a `for` loop
   --> crates/stdarch-test/src/disassembly.rs:125:9
    |
125 |         while let Some(instruction) = lines.next() {
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for instruction in lines`
    |
    = note: `#[warn(clippy::while_let_on_iterator)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
@jyn514 jyn514 added the C-bug Category: Clippy is not doing the correct thing label Oct 26, 2020
@flip1995 flip1995 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 26, 2020
@matthiaskrgr matthiaskrgr added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 28, 2020
@ThibsG
Copy link
Contributor

ThibsG commented Jan 5, 2021

Related issue: #5844

@rustbot label +T-MIR

@rustbot rustbot added the T-MIR Type: This lint will require working with the MIR label Jan 5, 2021
@jyn514
Copy link
Member Author

jyn514 commented Jan 5, 2021

I think this might be fixed by recommending for line in lines.by_ref() instead, but that's uglier in the common case where you don't need it.

@ThibsG
Copy link
Contributor

ThibsG commented Jan 5, 2021

Maybe for line in &mut lines could work also?

Playground using example given in #5844

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 T-MIR Type: This lint will require working with the MIR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants