Fix clippy::question_mark on let-else with cfg (#15082)

Fixes rust-lang/rust-clippy#13642

changelog: Fix false positive in [`question_mark`] when else branch of
let-else contains `#[cfg]`
This commit is contained in:
Samuel Tardieu 2025-06-19 20:34:05 +00:00 committed by GitHub
commit 59291a75df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View file

@ -142,6 +142,7 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
&& let Some(ret) = find_let_else_ret_expression(els)
&& let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, ret)
&& !span_contains_comment(cx.tcx.sess.source_map(), els.span)
&& !span_contains_cfg(cx, els.span)
{
let mut applicability = Applicability::MaybeIncorrect;
let init_expr_str = Sugg::hir_with_applicability(cx, init_expr, "..", &mut applicability).maybe_paren();

View file

@ -453,3 +453,15 @@ fn const_in_pattern(x: Option<(i32, i32)>) -> Option<()> {
None
}
fn issue_13642(x: Option<i32>) -> Option<()> {
let Some(x) = x else {
#[cfg(false)]
panic!();
#[cfg(true)]
return None;
};
None
}

View file

@ -549,3 +549,15 @@ fn const_in_pattern(x: Option<(i32, i32)>) -> Option<()> {
None
}
fn issue_13642(x: Option<i32>) -> Option<()> {
let Some(x) = x else {
#[cfg(false)]
panic!();
#[cfg(true)]
return None;
};
None
}