Skip to content

Commit 51d65ca

Browse files
committed
Prevent adding useless match arms
1 parent 76285f1 commit 51d65ca

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

crates/ide_assists/src/handlers/fill_match_arms.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use itertools::Itertools;
88
use syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
99

1010
use crate::{
11-
utils::{does_pat_match_variant, render_snippet, Cursor},
11+
utils::{self, render_snippet, Cursor},
1212
AssistContext, AssistId, AssistKind, Assists,
1313
};
1414

@@ -135,14 +135,18 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
135135
}
136136

137137
fn is_variant_missing(existing_pats: &[Pat], var: &Pat) -> bool {
138-
!existing_pats.iter().any(|pat| match (pat, var) {
138+
!existing_pats.iter().any(|pat| does_pat_match_variant(pat, var))
139+
}
140+
141+
// Fixme: this is still somewhat limited, use hir_ty::diagnostics::match_check?
142+
fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
143+
match (pat, var) {
144+
(Pat::WildcardPat(_), _) => true,
139145
(Pat::TuplePat(tpat), Pat::TuplePat(tvar)) => {
140-
// `does_pat_match_variant` gives false positives for tuple patterns
141-
// Fixme: this is still somewhat limited
142146
tpat.fields().zip(tvar.fields()).all(|(p, v)| does_pat_match_variant(&p, &v))
143147
}
144-
_ => does_pat_match_variant(pat, var),
145-
})
148+
_ => utils::does_pat_match_variant(pat, var),
149+
}
146150
}
147151

148152
fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> {
@@ -504,11 +508,6 @@ fn main() {
504508
);
505509
}
506510

507-
// Fixme: This fails with extra useless match arms added.
508-
// To fix, it needs full fledged match exhaustiveness checking from
509-
// hir_ty::diagnostics::match_check
510-
// see https://github.com/rust-analyzer/rust-analyzer/issues/8493
511-
#[ignore]
512511
#[test]
513512
fn fill_match_arms_tuple_of_enum_partial_with_wildcards() {
514513
let ra_fixture = r#"
@@ -538,6 +537,23 @@ fn main() {
538537
);
539538
}
540539

540+
#[test]
541+
fn fill_match_arms_partial_with_deep_pattern() {
542+
// Fixme: cannot handle deep patterns
543+
let ra_fixture = r#"
544+
fn main() {
545+
match $0Some(true) {
546+
Some(true) => {}
547+
None => {}
548+
}
549+
}
550+
"#;
551+
check_assist_not_applicable(
552+
fill_match_arms,
553+
&format!("//- /main.rs crate:main deps:core{}{}", ra_fixture, FamousDefs::FIXTURE),
554+
);
555+
}
556+
541557
#[test]
542558
fn fill_match_arms_tuple_of_enum_not_applicable() {
543559
check_assist_not_applicable(

0 commit comments

Comments
 (0)