1 use std::iter::{self, Peekable};
4 use hir::{Adt, HasSource, ModuleDef, Semantics};
5 use ide_db::helpers::{mod_path_to_ast, FamousDefs};
6 use ide_db::RootDatabase;
7 use itertools::Itertools;
8 use syntax::ast::{self, make, AstNode, HasName, MatchArm, MatchArmList, MatchExpr, Pat};
11 utils::{self, render_snippet, Cursor},
12 AssistContext, AssistId, AssistKind, Assists,
15 // Assist: add_missing_match_arms
17 // Adds missing clauses to a `match` expression.
20 // enum Action { Move { distance: u32 }, Stop }
22 // fn handle(action: Action) {
30 // enum Action { Move { distance: u32 }, Stop }
32 // fn handle(action: Action) {
34 // $0Action::Move { distance } => todo!(),
35 // Action::Stop => todo!(),
39 pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
40 let match_expr = ctx.find_node_at_offset_with_descend::<ast::MatchExpr>()?;
41 let match_arm_list = match_expr.match_arm_list()?;
42 let target_range = ctx.sema.original_range(match_expr.syntax()).range;
44 if let None = cursor_at_trivial_match_arm_list(&ctx, &match_expr, &match_arm_list) {
45 let arm_list_range = ctx.sema.original_range(match_arm_list.syntax()).range;
46 let cursor_in_range = arm_list_range.contains_range(ctx.selection_trimmed());
48 cov_mark::hit!(not_applicable_outside_of_range_right);
53 let expr = match_expr.expr()?;
55 let mut arms: Vec<MatchArm> = match_arm_list.arms().collect();
56 if let [arm] = arms.as_slice() {
57 if let Some(Pat::WildcardPat(..)) = arm.pat() {
62 let top_lvl_pats: Vec<_> = arms
64 .filter_map(ast::MatchArm::pat)
65 .flat_map(|pat| match pat {
66 // Special case OrPat as separate top-level pats
67 Pat::OrPat(or_pat) => Either::Left(or_pat.pats()),
68 _ => Either::Right(iter::once(pat)),
70 // Exclude top level wildcards so that they are expanded by this assist, retains status quo in #8129.
71 .filter(|pat| !matches!(pat, Pat::WildcardPat(_)))
74 let module = ctx.sema.scope(expr.syntax()).module()?;
76 let mut missing_pats: Peekable<Box<dyn Iterator<Item = ast::Pat>>> = if let Some(enum_def) =
77 resolve_enum_def(&ctx.sema, &expr)
79 let variants = enum_def.variants(ctx.db());
81 let missing_pats = variants
83 .filter_map(|variant| build_pat(ctx.db(), module, variant))
84 .filter(|variant_pat| is_variant_missing(&top_lvl_pats, variant_pat));
87 FamousDefs(&ctx.sema, Some(module.krate())).core_option_Option().map(lift_enum);
88 let missing_pats: Box<dyn Iterator<Item = _>> = if Some(enum_def) == option_enum {
89 // Match `Some` variant first.
90 cov_mark::hit!(option_order);
91 Box::new(missing_pats.rev())
93 Box::new(missing_pats)
95 missing_pats.peekable()
96 } else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) {
98 let variants_of_enums: Vec<Vec<ExtendedVariant>> = enum_defs
100 .map(|enum_def| enum_def.variants(ctx.db()))
101 .inspect(|variants| n_arms *= variants.len())
104 // When calculating the match arms for a tuple of enums, we want
105 // to create a match arm for each possible combination of enum
106 // values. The `multi_cartesian_product` method transforms
107 // Vec<Vec<EnumVariant>> into Vec<(EnumVariant, .., EnumVariant)>
108 // where each tuple represents a proposed match arm.
110 // A number of arms grows very fast on even a small tuple of large enums.
111 // We skip the assist beyond an arbitrary threshold.
115 let missing_pats = variants_of_enums
117 .multi_cartesian_product()
118 .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation))
121 variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
122 ast::Pat::from(make::tuple_pat(patterns))
124 .filter(|variant_pat| is_variant_missing(&top_lvl_pats, variant_pat));
125 (Box::new(missing_pats) as Box<dyn Iterator<Item = _>>).peekable()
130 if missing_pats.peek().is_none() {
135 AssistId("add_missing_match_arms", AssistKind::QuickFix),
139 let new_match_arm_list = match_arm_list.clone_for_update();
140 let missing_arms = missing_pats
141 .map(|pat| make::match_arm(iter::once(pat), None, make::ext::expr_todo()))
142 .map(|it| it.clone_for_update());
144 let catch_all_arm = new_match_arm_list
146 .find(|arm| matches!(arm.pat(), Some(ast::Pat::WildcardPat(_))));
147 if let Some(arm) = catch_all_arm {
148 let is_empty_expr = arm.expr().map_or(true, |e| match e {
149 ast::Expr::BlockExpr(b) => {
150 b.statements().next().is_none() && b.tail_expr().is_none()
152 ast::Expr::TupleExpr(t) => t.fields().next().is_none(),
158 cov_mark::hit!(add_missing_match_arms_empty_expr);
161 let mut first_new_arm = None;
162 for arm in missing_arms {
163 first_new_arm.get_or_insert_with(|| arm.clone());
164 new_match_arm_list.add_arm(arm);
167 let old_range = ctx.sema.original_range(match_arm_list.syntax()).range;
168 match (first_new_arm, ctx.config.snippet_cap) {
169 (Some(first_new_arm), Some(cap)) => {
172 match first_new_arm.syntax().descendants().find_map(ast::WildcardPat::cast)
175 extend_lifetime = it.syntax().clone();
176 Cursor::Replace(&extend_lifetime)
178 None => Cursor::Before(first_new_arm.syntax()),
180 let snippet = render_snippet(cap, new_match_arm_list.syntax(), cursor);
181 builder.replace_snippet(cap, old_range, snippet);
183 _ => builder.replace(old_range, new_match_arm_list.to_string()),
189 fn cursor_at_trivial_match_arm_list(
191 match_expr: &MatchExpr,
192 match_arm_list: &MatchArmList,
195 if match_arm_list.arms().next() == None {
196 cov_mark::hit!(add_missing_match_arms_empty_body);
204 if let Some(last_arm) = match_arm_list.arms().last() {
205 let last_arm_range = last_arm.syntax().text_range();
206 let match_expr_range = match_expr.syntax().text_range();
207 if last_arm_range.end() <= ctx.offset() && ctx.offset() < match_expr_range.end() {
208 cov_mark::hit!(add_missing_match_arms_end_of_last_arm);
213 // match { _$0 => {...} }
214 let wild_pat = ctx.find_node_at_offset_with_descend::<ast::WildcardPat>()?;
215 let arm = wild_pat.syntax().parent().and_then(ast::MatchArm::cast)?;
216 let arm_match_expr = arm.syntax().ancestors().nth(2).and_then(ast::MatchExpr::cast)?;
217 if arm_match_expr == *match_expr {
218 cov_mark::hit!(add_missing_match_arms_trivial_arm);
225 fn is_variant_missing(existing_pats: &[Pat], var: &Pat) -> bool {
226 !existing_pats.iter().any(|pat| does_pat_match_variant(pat, var))
229 // Fixme: this is still somewhat limited, use hir_ty::diagnostics::match_check?
230 fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
232 (Pat::WildcardPat(_), _) => true,
233 (Pat::TuplePat(tpat), Pat::TuplePat(tvar)) => {
234 tpat.fields().zip(tvar.fields()).all(|(p, v)| does_pat_match_variant(&p, &v))
236 _ => utils::does_pat_match_variant(pat, var),
240 #[derive(Eq, PartialEq, Clone, Copy)]
246 #[derive(Eq, PartialEq, Clone, Copy)]
247 enum ExtendedVariant {
250 Variant(hir::Variant),
253 fn lift_enum(e: hir::Enum) -> ExtendedEnum {
254 ExtendedEnum::Enum(e)
258 fn variants(self, db: &RootDatabase) -> Vec<ExtendedVariant> {
260 ExtendedEnum::Enum(e) => {
261 e.variants(db).into_iter().map(ExtendedVariant::Variant).collect::<Vec<_>>()
263 ExtendedEnum::Bool => {
264 Vec::<ExtendedVariant>::from([ExtendedVariant::True, ExtendedVariant::False])
270 fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ExtendedEnum> {
271 sema.type_of_expr(expr)?.adjusted().autoderef(sema.db).find_map(|ty| match ty.as_adt() {
272 Some(Adt::Enum(e)) => Some(ExtendedEnum::Enum(e)),
273 _ => ty.is_bool().then(|| ExtendedEnum::Bool),
277 fn resolve_tuple_of_enum_def(
278 sema: &Semantics<RootDatabase>,
280 ) -> Option<Vec<ExtendedEnum>> {
281 sema.type_of_expr(expr)?
283 .tuple_fields(sema.db)
286 ty.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
287 Some(Adt::Enum(e)) => Some(lift_enum(e)),
288 // For now we only handle expansion for a tuple of enums. Here
289 // we map non-enum items to None and rely on `collect` to
290 // convert Vec<Option<hir::Enum>> into Option<Vec<hir::Enum>>.
291 _ => ty.is_bool().then(|| ExtendedEnum::Bool),
297 fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> {
299 ExtendedVariant::Variant(var) => {
300 let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
302 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
303 let pat: ast::Pat = match var.source(db)?.value.kind() {
304 ast::StructKind::Tuple(field_list) => {
306 iter::repeat(make::wildcard_pat().into()).take(field_list.fields().count());
307 make::tuple_struct_pat(path, pats).into()
309 ast::StructKind::Record(field_list) => {
310 let pats = field_list
312 .map(|f| make::ext::simple_ident_pat(f.name().unwrap()).into());
313 make::record_pat(path, pats).into()
315 ast::StructKind::Unit => make::path_pat(path),
320 ExtendedVariant::True => Some(ast::Pat::from(make::literal_pat("true"))),
321 ExtendedVariant::False => Some(ast::Pat::from(make::literal_pat("false"))),
328 check_assist, check_assist_not_applicable, check_assist_target, check_assist_unresolved,
331 use super::add_missing_match_arms;
334 fn all_match_arms_provided() {
335 check_assist_not_applicable(
336 add_missing_match_arms,
340 Bs{x:i32, y:Option<i32>},
341 Cs(i32, Option<i32>),
346 A::Bs{x,y:Some(_)} => {}
347 A::Cs(_, Some(_)) => {}
355 fn not_applicable_outside_of_range_left() {
356 check_assist_not_applicable(
357 add_missing_match_arms,
371 fn not_applicable_outside_of_range_right() {
372 cov_mark::check!(not_applicable_outside_of_range_right);
373 check_assist_not_applicable(
374 add_missing_match_arms,
388 fn all_boolean_match_arms_provided() {
389 check_assist_not_applicable(
390 add_missing_match_arms,
403 fn tuple_of_non_enum() {
404 // for now this case is not handled, although it potentially could be
406 check_assist_not_applicable(
407 add_missing_match_arms,
418 fn add_missing_match_arms_boolean() {
420 add_missing_match_arms,
439 fn partial_fill_boolean() {
441 add_missing_match_arms,
461 fn all_boolean_tuple_arms_provided() {
462 check_assist_not_applicable(
463 add_missing_match_arms,
478 fn fill_boolean_tuple() {
480 add_missing_match_arms,
490 $0(true, true) => todo!(),
491 (true, false) => todo!(),
492 (false, true) => todo!(),
493 (false, false) => todo!(),
501 fn partial_fill_boolean_tuple() {
503 add_missing_match_arms,
515 $0(true, true) => todo!(),
516 (true, false) => todo!(),
517 (false, false) => todo!(),
525 fn partial_fill_record_tuple() {
527 add_missing_match_arms,
531 Bs { x: i32, y: Option<i32> },
532 Cs(i32, Option<i32>),
536 A::Bs { x, y: Some(_) } => {}
537 A::Cs(_, Some(_)) => {}
544 Bs { x: i32, y: Option<i32> },
545 Cs(i32, Option<i32>),
549 A::Bs { x, y: Some(_) } => {}
550 A::Cs(_, Some(_)) => {}
559 fn partial_fill_option() {
561 add_missing_match_arms,
574 Some(${0:_}) => todo!(),
582 fn partial_fill_or_pat() {
584 add_missing_match_arms,
586 enum A { As, Bs, Cs(Option<i32>) }
589 A::Cs(_) | A::Bs => {}
594 enum A { As, Bs, Cs(Option<i32>) }
597 A::Cs(_) | A::Bs => {}
608 add_missing_match_arms,
610 enum A { As, Bs, Cs, Ds(String), Es(B) }
615 A::Ds(_value) => { let x = 1; }
621 enum A { As, Bs, Cs, Ds(String), Es(B) }
626 A::Ds(_value) => { let x = 1; }
637 fn partial_fill_bind_pat() {
639 add_missing_match_arms,
641 enum A { As, Bs, Cs(Option<i32>) }
650 enum A { As, Bs, Cs(Option<i32>) }
655 A::Cs(${0:_}) => todo!(),
663 fn add_missing_match_arms_empty_body() {
664 cov_mark::check!(add_missing_match_arms_empty_body);
666 add_missing_match_arms,
668 enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
676 enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
684 A::Ds(_, _) => todo!(),
685 A::Es { x, y } => todo!(),
693 fn add_missing_match_arms_end_of_last_arm() {
694 cov_mark::check!(add_missing_match_arms_end_of_last_arm);
696 add_missing_match_arms,
705 (A::Two, B::One) => {},$0
717 (A::Two, B::One) => {},
718 $0(A::One, B::One) => todo!(),
719 (A::One, B::Two) => todo!(),
720 (A::Two, B::Two) => todo!(),
728 fn add_missing_match_arms_tuple_of_enum() {
730 add_missing_match_arms,
749 $0(A::One, B::One) => todo!(),
750 (A::One, B::Two) => todo!(),
751 (A::Two, B::One) => todo!(),
752 (A::Two, B::Two) => todo!(),
760 fn add_missing_match_arms_tuple_of_enum_ref() {
762 add_missing_match_arms,
781 $0(A::One, B::One) => todo!(),
782 (A::One, B::Two) => todo!(),
783 (A::Two, B::One) => todo!(),
784 (A::Two, B::Two) => todo!(),
792 fn add_missing_match_arms_tuple_of_enum_partial() {
794 add_missing_match_arms,
803 (A::Two, B::One) => {}
815 (A::Two, B::One) => {}
816 $0(A::One, B::One) => todo!(),
817 (A::One, B::Two) => todo!(),
818 (A::Two, B::Two) => todo!(),
826 fn add_missing_match_arms_tuple_of_enum_partial_with_wildcards() {
828 add_missing_match_arms,
836 (None, Some(_)) => {}
846 (None, Some(_)) => {}
847 $0(None, None) => todo!(),
855 fn add_missing_match_arms_partial_with_deep_pattern() {
856 // Fixme: cannot handle deep patterns
857 check_assist_not_applicable(
858 add_missing_match_arms,
872 fn add_missing_match_arms_tuple_of_enum_not_applicable() {
873 check_assist_not_applicable(
874 add_missing_match_arms,
883 (A::Two, B::One) => {}
884 (A::One, B::One) => {}
885 (A::One, B::Two) => {}
886 (A::Two, B::Two) => {}
894 fn add_missing_match_arms_single_element_tuple_of_enum() {
896 add_missing_match_arms,
912 $0(A::One,) => todo!(),
913 (A::Two,) => todo!(),
921 fn test_fill_match_arm_refs() {
923 add_missing_match_arms,
944 add_missing_match_arms,
947 Es { x: usize, y: usize }
957 Es { x: usize, y: usize }
962 $0A::Es { x, y } => todo!(),
970 fn add_missing_match_arms_target_simple() {
972 add_missing_match_arms,
985 fn add_missing_match_arms_target_complex() {
987 add_missing_match_arms,
1004 fn add_missing_match_arms_trivial_arm() {
1005 cov_mark::check!(add_missing_match_arms_trivial_arm);
1007 add_missing_match_arms,
1031 fn wildcard_inside_expression_not_applicable() {
1032 check_assist_not_applicable(
1033 add_missing_match_arms,
1050 fn add_missing_match_arms_qualifies_path() {
1052 add_missing_match_arms,
1054 mod foo { pub enum E { X, Y } }
1064 mod foo { pub enum E { X, Y } }
1070 foo::E::Y => todo!(),
1078 fn add_missing_match_arms_preserves_comments() {
1080 add_missing_match_arms,
1087 // This is where the rest should be
1097 $0A::Two => todo!(),
1098 // This is where the rest should be
1106 fn add_missing_match_arms_preserves_comments_empty() {
1108 add_missing_match_arms,
1121 $0A::One => todo!(),
1131 fn add_missing_match_arms_placeholder() {
1133 add_missing_match_arms,
1135 enum A { One, Two, }
1143 enum A { One, Two, }
1146 $0A::One => todo!(),
1156 cov_mark::check!(option_order);
1158 add_missing_match_arms,
1160 //- minicore: option
1161 fn foo(opt: Option<i32>) {
1167 fn foo(opt: Option<i32>) {
1169 Some(${0:_}) => todo!(),
1178 fn works_inside_macro_call() {
1180 add_missing_match_arms,
1182 macro_rules! m { ($expr:expr) => {$expr}}
1193 macro_rules! m { ($expr:expr) => {$expr}}
1202 $0Test::A => todo!(),
1211 fn lazy_computation() {
1212 // Computing a single missing arm is enough to determine applicability of the assist.
1213 cov_mark::check_count!(add_missing_match_arms_lazy_computation, 1);
1214 check_assist_unresolved(
1215 add_missing_match_arms,
1217 enum A { One, Two, }
1218 fn foo(tuple: (A, A)) {
1226 fn adds_comma_before_new_arms() {
1228 add_missing_match_arms,
1246 fn does_not_add_extra_comma() {
1248 add_missing_match_arms,
1266 fn does_not_remove_catch_all_with_non_empty_expr() {
1267 cov_mark::check!(add_missing_match_arms_empty_expr);
1269 add_missing_match_arms,