Rust-clippy: `needless_return` false positive when a temporary borrows a local variable

Created on 2 Aug 2020  路  4Comments  路  Source: rust-lang/rust-clippy

I tried this code:

#![allow(unused)]

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

I expected to see this happen: needless_return does not fire because the call to lock() borrows stdin and the temporary would be destroyed after the borrowed local, resulting in a does not live long enough compiler error.

Instead, this happened: it fired

The same fix that was applied for let_and_return in #5680 should work for this case. The known problems section of the lint description could be updated too.

Meta

  • cargo clippy -V: clippy 0.0.212 (05762e3 2020-08-01)
  • rustc -Vv:
    rustc 1.45.1 (c367798cf 2020-07-26) binary: rustc commit-hash: c367798cfd3817ca6ae908ce675d1d99242af148 commit-date: 2020-07-26 host: x86_64-unknown-linux-gnu release: 1.45.1 LLVM version: 10.0
L-bug good-first-issue

All 4 comments

I'd like to give this issue a try

Don't hesitate to ask for support if you get stuck!

The general idea is to move the lint from early to late lint pass, probably reusing the let_and_return module, adapting the name.

Great, thanks!

Hi @ebroto,
I made the needless_return a late pass and added a borrow checker following fix #5680.
Also, added the mentioned function read_line() to the test cases. All tests are passing locally.
I have read the PR guideline and now I am opening a PR.

Was this page helpful?
0 / 5 - 0 ratings