Skip to content
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

Another let_and_return false positive #8114

Open
antonok-edm opened this issue Dec 9, 2021 · 0 comments
Open

Another let_and_return false positive #8114

antonok-edm opened this issue Dec 9, 2021 · 0 comments
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

Comments

@antonok-edm
Copy link

Summary

There are already a few issues involving let_and_return false positives with E0597, but all of them are closed. I've found another case that still reproduces on Nightly (very similar to the one fixed by #5680).

Lint Name

let_and_return

Reproducer

I tried this code:

struct HasDrop<'i>(core::marker::PhantomData<&'i ()>);

impl<'i> Drop for HasDrop<'i> {
    fn drop(&mut self) {}
}

struct DropIterator<'i>(&'i ());

impl<'i> Iterator for DropIterator<'i> {
    type Item = HasDrop<'i>;
    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

fn is_valid() -> bool {
    let i = ();
    let mut drop_iter = DropIterator(&i);
    let x = drop_iter.next().is_none();
    x
}

fn main() {
    println!("{}", is_valid());
}

The code runs correctly, printing true to the console. However, there is a single clippy lint reported:

warning: returning the result of a `let` binding from a block
  --> src/main.rs:20:5
   |
19 |     let x = drop_iter.next().is_none();
   |     ----------------------------------- unnecessary `let` binding
20 |     x
   |     ^
   |
   = note: `#[warn(clippy::let_and_return)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
help: return the expression directly
   |
19 ~
20 ~     drop_iter.next().is_none()
   |

After applying the suggestion, the code no longer compiles. The following error is reported:

error[E0597]: `i` does not live long enough
  --> src/main.rs:18:38
   |
18 |     let mut drop_iter = DropIterator(&i);
   |                                      ^^ borrowed value does not live long enough
19 |     drop_iter.next().is_none()
   |     ---------------- a temporary with access to the borrow is created here ...
20 | }
   | -
   | |
   | `i` dropped here while still borrowed
   | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `Option<HasDrop<'_>>`
   |
   = note: the temporary is part of an expression at the end of a block;
           consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
   |
19 |     let x = drop_iter.next().is_none(); x
   |     +++++++                           +++

For more information about this error, try `rustc --explain E0597`.

I expected to see this happen:

The original code should not suggest any clippy lints.

Version

rustc 1.59.0-nightly (db9d361a4 2021-11-28)
binary: rustc
commit-hash: db9d361a4731ca0bb48533fab6297a8fea75696f
commit-date: 2021-11-28
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@antonok-edm antonok-edm 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 Dec 9, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 9, 2021
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
Projects
None yet
Development

No branches or pull requests

2 participants