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

let_and_return false positive #3792

Closed
SuperCuber opened this issue Feb 20, 2019 · 0 comments · Fixed by #5680
Closed

let_and_return false positive #3792

SuperCuber opened this issue Feb 20, 2019 · 0 comments · Fixed by #5680
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 L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@SuperCuber
Copy link

SuperCuber commented Feb 20, 2019

$ cargo clippy -V
0.0.212

The following code:

fn read_line() -> String {
    use std::io::BufRead;
    let stdin = ::std::io::stdin();
    let line = stdin.lock().lines().next().unwrap().unwrap();
    line
}

Triggers the let_and_return lint, however upon applying the suggested fix as so:

fn read_line() -> String {
    use std::io::BufRead;
    let stdin = ::std::io::stdin();
    stdin.lock().lines().next().unwrap().unwrap()
}

The code fails to compile with the following message:

error[E0597]: `stdin` does not live long enough
 --> src\main.rs:8:5
  |
8 |     stdin.lock().lines().next().unwrap().unwrap()
  |     ^^^^^---------------
  |     |
  |     borrowed value does not live long enough
  |     a temporary with access to the borrow is created here ...
9 | }
  | -
  | |
  | `stdin` dropped here while still borrowed
  | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::io::Lines<std::io::StdinLock<'_>>`
  |
  = 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. 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

Basically the compiler suggest I undo the change I just made.

@flip1995 flip1995 added 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 labels Feb 21, 2019
@phansch phansch added the L-suggestion Lint: Improving, adding or fixing lint suggestions label Aug 24, 2019
@bors bors closed this as completed in f947644 Jun 8, 2020
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 L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants