Skip to content

Commit b719f99

Browse files
authored
Consider consts in patterns as refutable (#14887)
changelog: none
2 parents 32a3744 + 843a45f commit b719f99

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

clippy_utils/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,10 +1565,10 @@ pub fn is_ctor_or_promotable_const_function(cx: &LateContext<'_>, expr: &Expr<'_
15651565
/// Returns `true` if a pattern is refutable.
15661566
// TODO: should be implemented using rustc/mir_build/thir machinery
15671567
pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
1568-
fn is_enum_variant(cx: &LateContext<'_>, qpath: &QPath<'_>, id: HirId) -> bool {
1569-
matches!(
1568+
fn is_qpath_refutable(cx: &LateContext<'_>, qpath: &QPath<'_>, id: HirId) -> bool {
1569+
!matches!(
15701570
cx.qpath_res(qpath, id),
1571-
Res::Def(DefKind::Variant, ..) | Res::Def(DefKind::Ctor(def::CtorOf::Variant, _), _)
1571+
Res::Def(DefKind::Struct, ..) | Res::Def(DefKind::Ctor(def::CtorOf::Struct, _), _)
15721572
)
15731573
}
15741574

@@ -1585,16 +1585,18 @@ pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
15851585
kind: PatExprKind::Path(qpath),
15861586
hir_id,
15871587
..
1588-
}) => is_enum_variant(cx, qpath, *hir_id),
1588+
}) => is_qpath_refutable(cx, qpath, *hir_id),
15891589
PatKind::Or(pats) => {
15901590
// TODO: should be the honest check, that pats is exhaustive set
15911591
are_refutable(cx, pats)
15921592
},
15931593
PatKind::Tuple(pats, _) => are_refutable(cx, pats),
15941594
PatKind::Struct(ref qpath, fields, _) => {
1595-
is_enum_variant(cx, qpath, pat.hir_id) || are_refutable(cx, fields.iter().map(|field| field.pat))
1595+
is_qpath_refutable(cx, qpath, pat.hir_id) || are_refutable(cx, fields.iter().map(|field| field.pat))
1596+
},
1597+
PatKind::TupleStruct(ref qpath, pats, _) => {
1598+
is_qpath_refutable(cx, qpath, pat.hir_id) || are_refutable(cx, pats)
15961599
},
1597-
PatKind::TupleStruct(ref qpath, pats, _) => is_enum_variant(cx, qpath, pat.hir_id) || are_refutable(cx, pats),
15981600
PatKind::Slice(head, middle, tail) => {
15991601
match &cx.typeck_results().node_type(pat.hir_id).kind() {
16001602
rustc_ty::Slice(..) => {

tests/ui/question_mark.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,13 @@ fn issue_14615(a: MutexGuard<Option<u32>>) -> Option<String> {
443443
//~^^^ question_mark
444444
Some(format!("{a}"))
445445
}
446+
447+
fn const_in_pattern(x: Option<(i32, i32)>) -> Option<()> {
448+
const N: i32 = 0;
449+
450+
let Some((x, N)) = x else {
451+
return None;
452+
};
453+
454+
None
455+
}

tests/ui/question_mark.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,13 @@ fn issue_14615(a: MutexGuard<Option<u32>>) -> Option<String> {
539539
//~^^^ question_mark
540540
Some(format!("{a}"))
541541
}
542+
543+
fn const_in_pattern(x: Option<(i32, i32)>) -> Option<()> {
544+
const N: i32 = 0;
545+
546+
let Some((x, N)) = x else {
547+
return None;
548+
};
549+
550+
None
551+
}

0 commit comments

Comments
 (0)