Skip to content

Commit 48f38eb

Browse files
committed
needless_return_with_question_mark ignore let-else
1 parent 6d9516a commit 48f38eb

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

clippy_lints/src/returns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lin
22
use clippy_utils::source::{snippet_opt, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
5-
use clippy_utils::{fn_def_id, is_from_proc_macro, path_to_local_id, span_find_starting_semi};
5+
use clippy_utils::{fn_def_id, is_from_proc_macro, is_inside_let_else, path_to_local_id, span_find_starting_semi};
66
use core::ops::ControlFlow;
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
@@ -171,6 +171,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
171171
&& let ItemKind::Fn(_, _, body) = item.kind
172172
&& let block = cx.tcx.hir().body(body).value
173173
&& let ExprKind::Block(block, _) = block.kind
174+
&& !is_inside_let_else(cx.tcx, expr)
174175
&& let [.., final_stmt] = block.stmts
175176
&& final_stmt.hir_id != stmt.hir_id
176177
&& !is_from_proc_macro(cx, expr)

clippy_utils/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,43 @@ pub fn is_else_clause(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
14891489
}
14901490
}
14911491

1492+
/// Checks if the given expression is a part of `let else`
1493+
/// returns `true` for both the `init` and the `else` part
1494+
pub fn is_inside_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
1495+
let mut child_id = expr.hir_id;
1496+
for (parent_id, node) in tcx.hir().parent_iter(child_id) {
1497+
if let Node::Local(Local {
1498+
init: Some(init),
1499+
els: Some(els),
1500+
..
1501+
}) = node
1502+
&& (init.hir_id == child_id || els.hir_id == child_id)
1503+
{
1504+
return true;
1505+
}
1506+
1507+
child_id = parent_id;
1508+
}
1509+
1510+
false
1511+
}
1512+
1513+
/// Checks if the given expression is the else clause of a `let else` expression
1514+
pub fn is_else_clause_in_let_else(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
1515+
let mut child_id = expr.hir_id;
1516+
for (parent_id, node) in tcx.hir().parent_iter(child_id) {
1517+
if let Node::Local(Local { els: Some(els), .. }) = node
1518+
&& els.hir_id == child_id
1519+
{
1520+
return true;
1521+
}
1522+
1523+
child_id = parent_id;
1524+
}
1525+
1526+
false
1527+
}
1528+
14921529
/// Checks whether the given `Expr` is a range equivalent to a `RangeFull`.
14931530
/// For the lower bound, this means that:
14941531
/// - either there is none

tests/ui/needless_return_with_question_mark.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::no_effect,
55
clippy::unit_arg,
66
clippy::useless_conversion,
7+
clippy::diverging_sub_expression,
78
unused
89
)]
910

@@ -35,5 +36,26 @@ fn main() -> Result<(), ()> {
3536
with_span! {
3637
return Err(())?;
3738
}
39+
40+
// Issue #11765
41+
// Should not lint
42+
let Some(s) = Some("") else {
43+
return Err(())?;
44+
};
45+
46+
let Some(s) = Some("") else {
47+
let Some(s) = Some("") else {
48+
return Err(())?;
49+
};
50+
51+
return Err(())?;
52+
};
53+
54+
let Some(_): Option<()> = ({
55+
return Err(())?;
56+
}) else {
57+
panic!();
58+
};
59+
3860
Err(())
3961
}

tests/ui/needless_return_with_question_mark.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::no_effect,
55
clippy::unit_arg,
66
clippy::useless_conversion,
7+
clippy::diverging_sub_expression,
78
unused
89
)]
910

@@ -35,5 +36,26 @@ fn main() -> Result<(), ()> {
3536
with_span! {
3637
return Err(())?;
3738
}
39+
40+
// Issue #11765
41+
// Should not lint
42+
let Some(s) = Some("") else {
43+
return Err(())?;
44+
};
45+
46+
let Some(s) = Some("") else {
47+
let Some(s) = Some("") else {
48+
return Err(())?;
49+
};
50+
51+
return Err(())?;
52+
};
53+
54+
let Some(_): Option<()> = ({
55+
return Err(())?;
56+
}) else {
57+
panic!();
58+
};
59+
3860
Err(())
3961
}

tests/ui/needless_return_with_question_mark.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unneeded `return` statement with `?` operator
2-
--> $DIR/needless_return_with_question_mark.rs:27:5
2+
--> $DIR/needless_return_with_question_mark.rs:28:5
33
|
44
LL | return Err(())?;
55
| ^^^^^^^ help: remove it

0 commit comments

Comments
 (0)