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](https://github.com/rust-lang/rust-clippy/pull/10110)
<!-- changelog_checked -->
This commit is contained in:
bors 2023-01-01 13:55:29 +00:00
commit a85e480dd1
4 changed files with 55 additions and 16 deletions

View File

@ -210,22 +210,25 @@ fn check_final_expr<'tcx>(
// if desugar of `do yeet`, don't lint
if let Some(inner_expr) = inner
&& let ExprKind::Call(path_expr, _) = inner_expr.kind
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind {
return;
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind
{
return;
}
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if !borrows {
// check if expr return nothing
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
} else {
peeled_drop_expr.span
};
if !cx.tcx.hir().attrs(expr.hir_id).is_empty() {
return;
}
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if borrows {
return;
}
// check if expr return nothing
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
} else {
peeled_drop_expr.span
};
emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
}
}
emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
},
ExprKind::If(_, then, else_clause_opt) => {
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>)
{
ControlFlow::Break(())
} else {
ControlFlow::Continue(Descend::from(!expr.span.from_expansion()))
ControlFlow::Continue(Descend::from(!e.span.from_expansion()))
}
})
.is_some()

View File

@ -277,4 +277,14 @@ fn issue9947() -> Result<(), String> {
do yeet "hello";
}
// without anyhow, but triggers the same bug I believe
#[expect(clippy::useless_format)]
fn issue10051() -> Result<String, String> {
if true {
Ok(format!("ok!"))
} else {
Err(format!("err!"))
}
}
fn main() {}

View File

@ -287,4 +287,14 @@ fn issue9947() -> Result<(), String> {
do yeet "hello";
}
// without anyhow, but triggers the same bug I believe
#[expect(clippy::useless_format)]
fn issue10051() -> Result<String, String> {
if true {
return Ok(format!("ok!"));
} else {
return Err(format!("err!"));
}
}
fn main() {}

View File

@ -386,5 +386,21 @@ LL | let _ = 42; return;
|
= help: remove `return`
error: aborting due to 46 previous errors
error: unneeded `return` statement
--> $DIR/needless_return.rs:294:9
|
LL | return Ok(format!("ok!"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: remove `return`
error: unneeded `return` statement
--> $DIR/needless_return.rs:296:9
|
LL | return Err(format!("err!"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: remove `return`
error: aborting due to 48 previous errors