mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 20:28:33 +00:00
Auto merge of #6549 - ThibsG:FixClosureNeedlessReturn, r=phansch
Fix FP with empty return for `needless_return` lint This fixes a false positive in `needless_return` lint, when triggered in a closure using `return` statement without value. Fixes: #6501 changelog: none
This commit is contained in:
commit
40ce9f83b6
@ -131,7 +131,16 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|
|||||||
_: HirId,
|
_: HirId,
|
||||||
) {
|
) {
|
||||||
match kind {
|
match kind {
|
||||||
FnKind::Closure(_) => check_final_expr(cx, &body.value, Some(body.value.span), RetReplacement::Empty),
|
FnKind::Closure(_) => {
|
||||||
|
// when returning without value in closure, replace this `return`
|
||||||
|
// with an empty block to prevent invalid suggestion (see #6501)
|
||||||
|
let replacement = if let ExprKind::Ret(None) = &body.value.kind {
|
||||||
|
RetReplacement::Block
|
||||||
|
} else {
|
||||||
|
RetReplacement::Empty
|
||||||
|
};
|
||||||
|
check_final_expr(cx, &body.value, Some(body.value.span), replacement)
|
||||||
|
},
|
||||||
FnKind::ItemFn(..) | FnKind::Method(..) => {
|
FnKind::ItemFn(..) | FnKind::Method(..) => {
|
||||||
if let ExprKind::Block(ref block, _) = body.value.kind {
|
if let ExprKind::Block(ref block, _) = body.value.kind {
|
||||||
check_block_return(cx, block);
|
check_block_return(cx, block);
|
||||||
|
@ -101,6 +101,19 @@ fn test_return_in_macro() {
|
|||||||
needed_return!(0);
|
needed_return!(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod issue6501 {
|
||||||
|
fn foo(bar: Result<(), ()>) {
|
||||||
|
bar.unwrap_or_else(|_| {})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_closure() {
|
||||||
|
let _ = || {
|
||||||
|
|
||||||
|
};
|
||||||
|
let _ = || {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = test_end_of_fn();
|
let _ = test_end_of_fn();
|
||||||
let _ = test_no_semicolon();
|
let _ = test_no_semicolon();
|
||||||
|
@ -101,6 +101,19 @@ fn test_return_in_macro() {
|
|||||||
needed_return!(0);
|
needed_return!(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod issue6501 {
|
||||||
|
fn foo(bar: Result<(), ()>) {
|
||||||
|
bar.unwrap_or_else(|_| return)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_closure() {
|
||||||
|
let _ = || {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let _ = || return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = test_end_of_fn();
|
let _ = test_end_of_fn();
|
||||||
let _ = test_no_semicolon();
|
let _ = test_no_semicolon();
|
||||||
|
@ -84,5 +84,23 @@ error: unneeded `return` statement
|
|||||||
LL | return String::new();
|
LL | return String::new();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
|
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
|
||||||
|
|
||||||
error: aborting due to 14 previous errors
|
error: unneeded `return` statement
|
||||||
|
--> $DIR/needless_return.rs:106:32
|
||||||
|
|
|
||||||
|
LL | bar.unwrap_or_else(|_| return)
|
||||||
|
| ^^^^^^ help: replace `return` with an empty block: `{}`
|
||||||
|
|
||||||
|
error: unneeded `return` statement
|
||||||
|
--> $DIR/needless_return.rs:111:13
|
||||||
|
|
|
||||||
|
LL | return;
|
||||||
|
| ^^^^^^^ help: remove `return`
|
||||||
|
|
||||||
|
error: unneeded `return` statement
|
||||||
|
--> $DIR/needless_return.rs:113:20
|
||||||
|
|
|
||||||
|
LL | let _ = || return;
|
||||||
|
| ^^^^^^ help: replace `return` with an empty block: `{}`
|
||||||
|
|
||||||
|
error: aborting due to 17 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user