Skip to content

Commit dba7763

Browse files
committed
[if_then_some_else_none]: look into local initializers
1 parent 4932d05 commit dba7763

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::sugg::Sugg;
66
use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
77
use rustc_errors::Applicability;
88
use rustc_hir::LangItem::{OptionNone, OptionSome};
9-
use rustc_hir::{Expr, ExprKind, Stmt, StmtKind};
9+
use rustc_hir::{Expr, ExprKind};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::lint::in_external_macro;
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
8383
&& then_expr.span.ctxt() == ctxt
8484
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
8585
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
86-
&& !stmts_contains_early_return(then_block.stmts)
86+
&& !contains_return(then_block.stmts)
8787
{
8888
let mut app = Applicability::Unspecified;
8989
let cond_snip = Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "[condition]", &mut app).maybe_par().to_string();
@@ -116,17 +116,3 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
116116

117117
extract_msrv_attr!(LateContext);
118118
}
119-
120-
fn stmts_contains_early_return(stmts: &[Stmt<'_>]) -> bool {
121-
stmts.iter().any(|stmt| {
122-
let Stmt {
123-
kind: StmtKind::Semi(e),
124-
..
125-
} = stmt
126-
else {
127-
return false;
128-
};
129-
130-
contains_return(e)
131-
})
132-
}

clippy_utils/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ use rustc_span::source_map::SourceMap;
110110
use rustc_span::symbol::{kw, Ident, Symbol};
111111
use rustc_span::{sym, Span};
112112
use rustc_target::abi::Integer;
113+
use visitors::Visitable;
113114

114115
use crate::consts::{constant, miri_to_const, Constant};
115116
use crate::higher::Range;
@@ -1286,7 +1287,7 @@ pub fn contains_name<'tcx>(name: Symbol, expr: &'tcx Expr<'_>, cx: &LateContext<
12861287
}
12871288

12881289
/// Returns `true` if `expr` contains a return expression
1289-
pub fn contains_return(expr: &hir::Expr<'_>) -> bool {
1290+
pub fn contains_return<'tcx>(expr: impl Visitable<'tcx>) -> bool {
12901291
for_each_expr(expr, |e| {
12911292
if matches!(e.kind, hir::ExprKind::Ret(..)) {
12921293
ControlFlow::Break(())

tests/ui/if_then_some_else_none.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,15 @@ fn f(b: bool, v: Option<()>) -> Option<()> {
117117
None
118118
}
119119
}
120+
121+
fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
122+
let x = if b {
123+
#[allow(clippy::let_unit_value)]
124+
let _ = v?;
125+
Some(())
126+
} else {
127+
None
128+
};
129+
130+
Ok(())
131+
}

0 commit comments

Comments
 (0)