Skip to content

Incorrect needless_borrow #8408

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
ritchie46 opened this issue Feb 10, 2022 · 1 comment · Fixed by #8441
Closed

Incorrect needless_borrow #8408

ritchie46 opened this issue Feb 10, 2022 · 1 comment · Fixed by #8441
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

Comments

@ritchie46
Copy link

Summary

Clippy suggests removing a borrow that would lead to ownership being moved.

Lint Name

needless_borrow

Reproducer

I tried this code:

fn main() {
    let mut iter = vec![1, 2, 3].into_iter();

    let mut sum = 0;
    for v in (&mut iter).enumerate().take(1) {
        sum += v.1;
    }
    
    let b = iter.count();
}

I saw this happen:

warning: this expression borrows a value the compiler would automatically borrow
  --> src/main.rs:65:14
   |
65 |     for v in (&mut iter).enumerate().take(1) {
   |              ^^^^^^^^^^^ help: change this to: `iter`

If we follow the lint:

fn main() {
    let mut iter = vec![1, 2, 3].into_iter();

    let mut sum = 0;
    for v in iter.enumerate().take(1) {
        sum += v.1;
    }

    let b = iter.count();
}

Our code does not compile:

error[E0382]: use of moved value: `iter`
   --> src/main.rs:69:13
    |
62  |     let mut iter = vec![1, 2, 3].into_iter();
    |         -------- move occurs because `iter` has type `std::vec::IntoIter<i32>`, which does not implement the `Copy` trait
...
65  |     for v in iter.enumerate().take(1) {
    |                   ----------- `iter` moved due to this method call
...
69  |     let b = iter.count();
    |             ^^^^ value used here after move
    |
note: this function takes ownership of the receiver `self`, which moves `iter`
   --> /home/ritchie46/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:928:18
    |
928 |     fn enumerate(self) -> Enumerate<Self>
    |                  ^^^

Version

rustc 1.60.0-nightly (e7aca8959 2022-02-09)
binary: rustc
commit-hash: e7aca895980f25f6d2d3c48e10fd04656764d1e4
commit-date: 2022-02-09
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

@ritchie46 ritchie46 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 Feb 10, 2022
@chris-morgan
Copy link
Member

I just observed much the same thing. Fairly sure it’s a recent regression, as I’m pretty sure I would have run Clippy multiple times between when I wrote this code a few weeks back and last rustup updated a few days ago.

impl Type {
    fn method(&mut self,) -> … {
        // …
        self.tar_file.seek(SeekFrom::Start(offset))?;
        let mut archive = Archive::new((&mut self.tar_file).take(size));
        // …
    }
}

std::io::Read::take takes by value. self.take(…) could be needless, but (&mut self.tar_file).take(…) is certainly not.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants