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

emit needless_return on return stmts using borrow #9008

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,12 @@ fn check_final_expr<'tcx>(
// allow `#[cfg(a)] return a; #[cfg(b)] return b;`
let attrs = cx.tcx.hir().attrs(expr.hir_id);
if !attrs.iter().any(attr_is_cfg) {
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if !borrows {
emit_return_lint(
cx,
span.expect("`else return` is not possible"),
inner.as_ref().map(|i| i.span),
replacement,
);
}
emit_return_lint(
cx,
span.expect("`else return` is not possible"),
inner.as_ref().map(|i| i.span),
replacement,
);
}
},
// a whole block? check it!
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn test_nested_match(x: u32) {
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
stdin.lock().lines().next().unwrap().unwrap()
}

fn borrows_but_not_last(value: bool) -> String {
Expand Down Expand Up @@ -200,7 +200,7 @@ async fn async_test_void_match(x: u32) {
async fn async_read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
return stdin.lock().lines().next().unwrap().unwrap();
stdin.lock().lines().next().unwrap().unwrap()
}

async fn async_borrows_but_not_last(value: bool) -> String {
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ error: unneeded `return` statement
LL | _ => return,
| ^^^^^^ help: replace `return` with a unit value: `()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:92:5
|
LL | return stdin.lock().lines().next().unwrap().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `stdin.lock().lines().next().unwrap().unwrap()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:100:9
|
Expand Down Expand Up @@ -204,6 +210,12 @@ error: unneeded `return` statement
LL | _ => return,
| ^^^^^^ help: replace `return` with a unit value: `()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:203:5
|
LL | return stdin.lock().lines().next().unwrap().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `stdin.lock().lines().next().unwrap().unwrap()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:211:9
|
Expand All @@ -222,5 +234,5 @@ error: unneeded `return` statement
LL | return format!("Hello {}", "world!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `format!("Hello {}", "world!")`

error: aborting due to 37 previous errors
error: aborting due to 39 previous errors