|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_help;
|
2 | 2 | use clippy_utils::source::snippet_with_macro_callsite;
|
3 |
| -use clippy_utils::{higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs}; |
| 3 | +use clippy_utils::{contains_return, higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs}; |
4 | 4 | use if_chain::if_chain;
|
5 | 5 | use rustc_hir::LangItem::{OptionNone, OptionSome};
|
6 |
| -use rustc_hir::{Expr, ExprKind}; |
| 6 | +use rustc_hir::{Expr, ExprKind, Stmt, StmtKind}; |
7 | 7 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
8 | 8 | use rustc_middle::lint::in_external_macro;
|
9 | 9 | use rustc_semver::RustcVersion;
|
@@ -82,6 +82,7 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
|
82 | 82 | if let Some(els_expr) = els_block.expr;
|
83 | 83 | if let ExprKind::Path(ref qpath) = els_expr.kind;
|
84 | 84 | if is_lang_ctor(cx, qpath, OptionNone);
|
| 85 | + if !stmts_contains_early_return(then_block.stmts); |
85 | 86 | then {
|
86 | 87 | let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
|
87 | 88 | let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
|
@@ -114,3 +115,11 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
|
114 | 115 |
|
115 | 116 | extract_msrv_attr!(LateContext);
|
116 | 117 | }
|
| 118 | + |
| 119 | +fn stmts_contains_early_return(stmts: &[Stmt<'_>]) -> bool { |
| 120 | + stmts.iter().any(|stmt| { |
| 121 | + let Stmt { kind: StmtKind::Semi(e), .. } = stmt else { return false }; |
| 122 | + |
| 123 | + contains_return(e) |
| 124 | + }) |
| 125 | +} |
0 commit comments