Skip to content

Commit a85e480

Browse files
committed
Auto merge of #10110 - Niki4tap:needless_anyhow_return, r=Alexendoo
Fix FN in `needless_return` Fixes #10051 changelog: Enhancement: [`needless_return`]: Now detects more cases for returns of owned values [#10110](#10110) <!-- changelog_checked -->
2 parents 6ccd4eb + 9ff868c commit a85e480

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

clippy_lints/src/returns.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,25 @@ fn check_final_expr<'tcx>(
210210
// if desugar of `do yeet`, don't lint
211211
if let Some(inner_expr) = inner
212212
&& let ExprKind::Call(path_expr, _) = inner_expr.kind
213-
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind {
214-
return;
213+
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind
214+
{
215+
return;
215216
}
216-
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
217-
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
218-
if !borrows {
219-
// check if expr return nothing
220-
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
221-
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
222-
} else {
223-
peeled_drop_expr.span
224-
};
225-
226-
emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
227-
}
217+
if !cx.tcx.hir().attrs(expr.hir_id).is_empty() {
218+
return;
219+
}
220+
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
221+
if borrows {
222+
return;
228223
}
224+
// check if expr return nothing
225+
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
226+
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
227+
} else {
228+
peeled_drop_expr.span
229+
};
230+
231+
emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
229232
},
230233
ExprKind::If(_, then, else_clause_opt) => {
231234
check_block_return(cx, &then.kind, semi_spans.clone());
@@ -292,7 +295,7 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
292295
{
293296
ControlFlow::Break(())
294297
} else {
295-
ControlFlow::Continue(Descend::from(!expr.span.from_expansion()))
298+
ControlFlow::Continue(Descend::from(!e.span.from_expansion()))
296299
}
297300
})
298301
.is_some()

tests/ui/needless_return.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,14 @@ fn issue9947() -> Result<(), String> {
277277
do yeet "hello";
278278
}
279279

280+
// without anyhow, but triggers the same bug I believe
281+
#[expect(clippy::useless_format)]
282+
fn issue10051() -> Result<String, String> {
283+
if true {
284+
Ok(format!("ok!"))
285+
} else {
286+
Err(format!("err!"))
287+
}
288+
}
289+
280290
fn main() {}

tests/ui/needless_return.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,14 @@ fn issue9947() -> Result<(), String> {
287287
do yeet "hello";
288288
}
289289

290+
// without anyhow, but triggers the same bug I believe
291+
#[expect(clippy::useless_format)]
292+
fn issue10051() -> Result<String, String> {
293+
if true {
294+
return Ok(format!("ok!"));
295+
} else {
296+
return Err(format!("err!"));
297+
}
298+
}
299+
290300
fn main() {}

tests/ui/needless_return.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,21 @@ LL | let _ = 42; return;
386386
|
387387
= help: remove `return`
388388

389-
error: aborting due to 46 previous errors
389+
error: unneeded `return` statement
390+
--> $DIR/needless_return.rs:294:9
391+
|
392+
LL | return Ok(format!("ok!"));
393+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
394+
|
395+
= help: remove `return`
396+
397+
error: unneeded `return` statement
398+
--> $DIR/needless_return.rs:296:9
399+
|
400+
LL | return Err(format!("err!"));
401+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
402+
|
403+
= help: remove `return`
404+
405+
error: aborting due to 48 previous errors
390406

0 commit comments

Comments
 (0)