Skip to content

Commit 3975f66

Browse files
committed
Fix FP with empty return for needless_return lint
1 parent a02806e commit 3975f66

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

clippy_lints/src/returns.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ impl<'tcx> LateLintPass<'tcx> for Return {
131131
_: HirId,
132132
) {
133133
match kind {
134-
FnKind::Closure(_) => check_final_expr(cx, &body.value, Some(body.value.span), RetReplacement::Empty),
134+
FnKind::Closure(_) => {
135+
// when returning without value in closure, replace this `return`
136+
// with an empty block to prevent invalid suggestion (see #6501)
137+
let replacement = if let ExprKind::Ret(None) = &body.value.kind {
138+
RetReplacement::Block
139+
} else {
140+
RetReplacement::Empty
141+
};
142+
check_final_expr(cx, &body.value, Some(body.value.span), replacement)
143+
},
135144
FnKind::ItemFn(..) | FnKind::Method(..) => {
136145
if let ExprKind::Block(ref block, _) = body.value.kind {
137146
check_block_return(cx, block);

tests/ui/needless_return.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ fn borrows_but_not_last(value: bool) -> String {
8686
}
8787
}
8888

89+
mod issue6501 {
90+
fn foo(bar: Result<(), ()>) {
91+
bar.unwrap_or_else(|_| {})
92+
}
93+
94+
fn test_closure() {
95+
let _ = || {
96+
97+
};
98+
let _ = || {};
99+
}
100+
}
101+
89102
fn main() {
90103
let _ = test_end_of_fn();
91104
let _ = test_no_semicolon();

tests/ui/needless_return.rs

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ fn borrows_but_not_last(value: bool) -> String {
8686
}
8787
}
8888

89+
mod issue6501 {
90+
fn foo(bar: Result<(), ()>) {
91+
bar.unwrap_or_else(|_| return)
92+
}
93+
94+
fn test_closure() {
95+
let _ = || {
96+
return;
97+
};
98+
let _ = || return;
99+
}
100+
}
101+
89102
fn main() {
90103
let _ = test_end_of_fn();
91104
let _ = test_no_semicolon();

tests/ui/needless_return.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,23 @@ error: unneeded `return` statement
8484
LL | return String::new();
8585
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
8686

87-
error: aborting due to 14 previous errors
87+
error: unneeded `return` statement
88+
--> $DIR/needless_return.rs:91:32
89+
|
90+
LL | bar.unwrap_or_else(|_| return)
91+
| ^^^^^^ help: replace `return` with an empty block: `{}`
92+
93+
error: unneeded `return` statement
94+
--> $DIR/needless_return.rs:96:13
95+
|
96+
LL | return;
97+
| ^^^^^^^ help: remove `return`
98+
99+
error: unneeded `return` statement
100+
--> $DIR/needless_return.rs:98:20
101+
|
102+
LL | let _ = || return;
103+
| ^^^^^^ help: replace `return` with an empty block: `{}`
104+
105+
error: aborting due to 17 previous errors
88106

0 commit comments

Comments
 (0)