Skip to content

let_and_return false positive #3792

Closed
@SuperCuber

Description

@SuperCuber
$ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions