ra_ap_ide_assists/handlers/
add_missing_match_arms.rs

1use std::iter::{self, Peekable};
2
3use either::Either;
4use hir::{Adt, AsAssocItem, Crate, FindPathConfig, HasAttrs, ModuleDef, Semantics, sym};
5use ide_db::RootDatabase;
6use ide_db::assists::ExprFillDefaultMode;
7use ide_db::syntax_helpers::suggest_name;
8use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
9use itertools::Itertools;
10use syntax::ToSmolStr;
11use syntax::ast::edit::IndentLevel;
12use syntax::ast::edit_in_place::Indent;
13use syntax::ast::syntax_factory::SyntaxFactory;
14use syntax::ast::{self, AstNode, MatchArmList, MatchExpr, Pat, make};
15
16use crate::{AssistContext, AssistId, Assists, utils};
17
18// Assist: add_missing_match_arms
19//
20// Adds missing clauses to a `match` expression.
21//
22// ```
23// enum Action { Move { distance: u32 }, Stop }
24//
25// fn handle(action: Action) {
26//     match action {
27//         $0
28//     }
29// }
30// ```
31// ->
32// ```
33// enum Action { Move { distance: u32 }, Stop }
34//
35// fn handle(action: Action) {
36//     match action {
37//         Action::Move { distance } => ${1:todo!()},
38//         Action::Stop => ${2:todo!()},$0
39//     }
40// }
41// ```
42pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
43    let match_expr = ctx.find_node_at_offset_with_descend::<ast::MatchExpr>()?;
44    let match_arm_list = match_expr.match_arm_list()?;
45    let arm_list_range = ctx.sema.original_range_opt(match_arm_list.syntax())?;
46
47    if cursor_at_trivial_match_arm_list(ctx, &match_expr, &match_arm_list).is_none() {
48        let arm_list_range = ctx.sema.original_range(match_arm_list.syntax()).range;
49        let cursor_in_range = arm_list_range.contains_range(ctx.selection_trimmed());
50        if cursor_in_range {
51            cov_mark::hit!(not_applicable_outside_of_range_right);
52            return None;
53        }
54    }
55
56    let expr = match_expr.expr()?;
57
58    let mut has_catch_all_arm = false;
59
60    let top_lvl_pats: Vec<_> = match_arm_list
61        .arms()
62        .filter_map(|arm| Some((arm.pat()?, arm.guard().is_some())))
63        .flat_map(|(pat, has_guard)| {
64            match pat {
65                // Special case OrPat as separate top-level pats
66                Pat::OrPat(or_pat) => Either::Left(or_pat.pats()),
67                _ => Either::Right(iter::once(pat)),
68            }
69            .map(move |pat| (pat, has_guard))
70        })
71        .map(|(pat, has_guard)| {
72            has_catch_all_arm |= !has_guard && matches!(pat, Pat::WildcardPat(_));
73            pat
74        })
75        // Exclude top level wildcards so that they are expanded by this assist, retains status quo in #8129.
76        .filter(|pat| !matches!(pat, Pat::WildcardPat(_)))
77        .collect();
78
79    let make = SyntaxFactory::with_mappings();
80
81    let scope = ctx.sema.scope(expr.syntax())?;
82    let module = scope.module();
83    let cfg = ctx.config.find_path_confg(ctx.sema.is_nightly(scope.krate()));
84    let self_ty = if ctx.config.prefer_self_ty {
85        scope
86            .containing_function()
87            .and_then(|function| function.as_assoc_item(ctx.db())?.implementing_ty(ctx.db()))
88    } else {
89        None
90    };
91    let (mut missing_pats, is_non_exhaustive, has_hidden_variants): (
92        Peekable<Box<dyn Iterator<Item = (ast::Pat, bool)>>>,
93        bool,
94        bool,
95    ) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr, self_ty.as_ref()) {
96        let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db(), module.krate());
97
98        let variants = enum_def.variants(ctx.db());
99
100        let has_hidden_variants =
101            variants.iter().any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
102
103        let missing_pats = variants
104            .into_iter()
105            .filter_map(|variant| {
106                Some((
107                    build_pat(ctx, &make, module, variant, cfg)?,
108                    variant.should_be_hidden(ctx.db(), module.krate()),
109                ))
110            })
111            .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat));
112
113        let option_enum = FamousDefs(&ctx.sema, module.krate()).core_option_Option();
114        let missing_pats: Box<dyn Iterator<Item = _>> = if matches!(enum_def, ExtendedEnum::Enum { enum_: e, .. } if Some(e) == option_enum)
115        {
116            // Match `Some` variant first.
117            cov_mark::hit!(option_order);
118            Box::new(missing_pats.rev())
119        } else {
120            Box::new(missing_pats)
121        };
122        (missing_pats.peekable(), is_non_exhaustive, has_hidden_variants)
123    } else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr, self_ty.as_ref()) {
124        let is_non_exhaustive =
125            enum_defs.iter().any(|enum_def| enum_def.is_non_exhaustive(ctx.db(), module.krate()));
126
127        let mut n_arms = 1;
128        let variants_of_enums: Vec<Vec<ExtendedVariant>> = enum_defs
129            .into_iter()
130            .map(|enum_def| enum_def.variants(ctx.db()))
131            .inspect(|variants| n_arms *= variants.len())
132            .collect();
133
134        // When calculating the match arms for a tuple of enums, we want
135        // to create a match arm for each possible combination of enum
136        // values. The `multi_cartesian_product` method transforms
137        // Vec<Vec<EnumVariant>> into Vec<(EnumVariant, .., EnumVariant)>
138        // where each tuple represents a proposed match arm.
139
140        // A number of arms grows very fast on even a small tuple of large enums.
141        // We skip the assist beyond an arbitrary threshold.
142        if n_arms > 256 {
143            return None;
144        }
145
146        let has_hidden_variants = variants_of_enums
147            .iter()
148            .flatten()
149            .any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
150
151        let missing_pats = variants_of_enums
152            .into_iter()
153            .multi_cartesian_product()
154            .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation))
155            .map(|variants| {
156                let is_hidden = variants
157                    .iter()
158                    .any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
159                let patterns = variants
160                    .into_iter()
161                    .filter_map(|variant| build_pat(ctx, &make, module, variant, cfg));
162
163                (ast::Pat::from(make.tuple_pat(patterns)), is_hidden)
164            })
165            .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat));
166        (
167            (Box::new(missing_pats) as Box<dyn Iterator<Item = _>>).peekable(),
168            is_non_exhaustive,
169            has_hidden_variants,
170        )
171    } else if let Some((enum_def, len)) =
172        resolve_array_of_enum_def(&ctx.sema, &expr, self_ty.as_ref())
173    {
174        let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db(), module.krate());
175        let variants = enum_def.variants(ctx.db());
176
177        if len.pow(variants.len() as u32) > 256 {
178            return None;
179        }
180
181        let has_hidden_variants =
182            variants.iter().any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
183
184        let variants_of_enums = vec![variants; len];
185
186        let missing_pats = variants_of_enums
187            .into_iter()
188            .multi_cartesian_product()
189            .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation))
190            .map(|variants| {
191                let is_hidden = variants
192                    .iter()
193                    .any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
194                let patterns = variants
195                    .into_iter()
196                    .filter_map(|variant| build_pat(ctx, &make, module, variant, cfg));
197
198                (ast::Pat::from(make.slice_pat(patterns)), is_hidden)
199            })
200            .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat));
201        (
202            (Box::new(missing_pats) as Box<dyn Iterator<Item = _>>).peekable(),
203            is_non_exhaustive,
204            has_hidden_variants,
205        )
206    } else {
207        return None;
208    };
209
210    let mut needs_catch_all_arm = is_non_exhaustive && !has_catch_all_arm;
211
212    if !needs_catch_all_arm
213        && ((has_hidden_variants && has_catch_all_arm) || missing_pats.peek().is_none())
214    {
215        return None;
216    }
217
218    acc.add(
219        AssistId::quick_fix("add_missing_match_arms"),
220        "Fill match arms",
221        ctx.sema.original_range(match_expr.syntax()).range,
222        |builder| {
223            // having any hidden variants means that we need a catch-all arm
224            needs_catch_all_arm |= has_hidden_variants;
225
226            let missing_arms = missing_pats
227                .filter(|(_, hidden)| {
228                    // filter out hidden patterns because they're handled by the catch-all arm
229                    !hidden
230                })
231                .map(|(pat, _)| {
232                    make.match_arm(
233                        pat,
234                        None,
235                        match ctx.config.expr_fill_default {
236                            ExprFillDefaultMode::Todo => make::ext::expr_todo(),
237                            ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
238                            ExprFillDefaultMode::Default => make::ext::expr_todo(),
239                        },
240                    )
241                });
242
243            let mut arms: Vec<_> = match_arm_list
244                .arms()
245                .filter(|arm| {
246                    if matches!(arm.pat(), Some(ast::Pat::WildcardPat(_))) {
247                        let is_empty_expr = arm.expr().is_none_or(|e| match e {
248                            ast::Expr::BlockExpr(b) => {
249                                b.statements().next().is_none() && b.tail_expr().is_none()
250                            }
251                            ast::Expr::TupleExpr(t) => t.fields().next().is_none(),
252                            _ => false,
253                        });
254                        if is_empty_expr {
255                            false
256                        } else {
257                            cov_mark::hit!(add_missing_match_arms_empty_expr);
258                            true
259                        }
260                    } else {
261                        true
262                    }
263                })
264                .collect();
265
266            let first_new_arm_idx = arms.len();
267            arms.extend(missing_arms);
268
269            if needs_catch_all_arm && !has_catch_all_arm {
270                cov_mark::hit!(added_wildcard_pattern);
271                let arm = make.match_arm(
272                    make.wildcard_pat().into(),
273                    None,
274                    match ctx.config.expr_fill_default {
275                        ExprFillDefaultMode::Todo => make::ext::expr_todo(),
276                        ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
277                        ExprFillDefaultMode::Default => make::ext::expr_todo(),
278                    },
279                );
280                arms.push(arm);
281            }
282
283            let new_match_arm_list = make.match_arm_list(arms);
284
285            // FIXME: Hack for syntax trees not having great support for macros
286            // Just replace the element that the original range came from
287            let old_place = {
288                // Find the original element
289                let file = ctx.sema.parse(arm_list_range.file_id);
290                let old_place = file.syntax().covering_element(arm_list_range.range);
291
292                match old_place {
293                    syntax::SyntaxElement::Node(it) => it,
294                    syntax::SyntaxElement::Token(it) => {
295                        // If a token is found, it is '{' or '}'
296                        // The parent is `{ ... }`
297                        it.parent().expect("Token must have a parent.")
298                    }
299                }
300            };
301
302            let mut editor = builder.make_editor(&old_place);
303            new_match_arm_list.indent(IndentLevel::from_node(&old_place));
304            editor.replace(old_place, new_match_arm_list.syntax());
305
306            if let Some(cap) = ctx.config.snippet_cap {
307                if let Some(it) = new_match_arm_list
308                    .arms()
309                    .nth(first_new_arm_idx)
310                    .and_then(|arm| arm.syntax().descendants().find_map(ast::WildcardPat::cast))
311                {
312                    editor.add_annotation(it.syntax(), builder.make_placeholder_snippet(cap));
313                }
314
315                for arm in new_match_arm_list.arms().skip(first_new_arm_idx) {
316                    if let Some(expr) = arm.expr() {
317                        editor.add_annotation(expr.syntax(), builder.make_placeholder_snippet(cap));
318                    }
319                }
320
321                if let Some(arm) = new_match_arm_list.arms().skip(first_new_arm_idx).last() {
322                    editor.add_annotation(arm.syntax(), builder.make_tabstop_after(cap));
323                }
324            }
325
326            editor.add_mappings(make.take());
327            builder.add_file_edits(ctx.vfs_file_id(), editor);
328        },
329    )
330}
331
332fn cursor_at_trivial_match_arm_list(
333    ctx: &AssistContext<'_>,
334    match_expr: &MatchExpr,
335    match_arm_list: &MatchArmList,
336) -> Option<()> {
337    // match x { $0 }
338    if match_arm_list.arms().next().is_none() {
339        cov_mark::hit!(add_missing_match_arms_empty_body);
340        return Some(());
341    }
342
343    // match x {
344    //     bar => baz,
345    //     $0
346    // }
347    if let Some(last_arm) = match_arm_list.arms().last() {
348        let last_arm_range = last_arm.syntax().text_range();
349        let match_expr_range = match_expr.syntax().text_range();
350        if last_arm_range.end() <= ctx.offset() && ctx.offset() < match_expr_range.end() {
351            cov_mark::hit!(add_missing_match_arms_end_of_last_arm);
352            return Some(());
353        }
354    }
355
356    // match { _$0 => {...} }
357    let wild_pat = ctx.find_node_at_offset_with_descend::<ast::WildcardPat>()?;
358    let arm = wild_pat.syntax().parent().and_then(ast::MatchArm::cast)?;
359    let arm_match_expr = arm.syntax().ancestors().nth(2).and_then(ast::MatchExpr::cast)?;
360    if arm_match_expr == *match_expr {
361        cov_mark::hit!(add_missing_match_arms_trivial_arm);
362        return Some(());
363    }
364
365    None
366}
367
368fn is_variant_missing(existing_pats: &[Pat], var: &Pat) -> bool {
369    !existing_pats.iter().any(|pat| does_pat_match_variant(pat, var))
370}
371
372// Fixme: this is still somewhat limited, use hir_ty::diagnostics::match_check?
373fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
374    match (pat, var) {
375        (Pat::WildcardPat(_), _) => true,
376        (Pat::SlicePat(spat), Pat::SlicePat(svar)) => {
377            spat.pats().zip(svar.pats()).all(|(p, v)| does_pat_match_variant(&p, &v))
378        }
379        (Pat::TuplePat(tpat), Pat::TuplePat(tvar)) => {
380            tpat.fields().zip(tvar.fields()).all(|(p, v)| does_pat_match_variant(&p, &v))
381        }
382        (Pat::OrPat(opat), _) => opat.pats().any(|p| does_pat_match_variant(&p, var)),
383        _ => utils::does_pat_match_variant(pat, var),
384    }
385}
386
387#[derive(Eq, PartialEq, Clone)]
388enum ExtendedEnum {
389    Bool,
390    Enum { enum_: hir::Enum, use_self: bool },
391}
392
393#[derive(Eq, PartialEq, Clone, Copy, Debug)]
394enum ExtendedVariant {
395    True,
396    False,
397    Variant { variant: hir::Variant, use_self: bool },
398}
399
400impl ExtendedVariant {
401    fn should_be_hidden(self, db: &RootDatabase, krate: Crate) -> bool {
402        match self {
403            ExtendedVariant::Variant { variant: var, .. } => {
404                var.attrs(db).has_doc_hidden() && var.module(db).krate() != krate
405            }
406            _ => false,
407        }
408    }
409}
410
411impl ExtendedEnum {
412    fn enum_(
413        db: &RootDatabase,
414        enum_: hir::Enum,
415        enum_ty: &hir::Type<'_>,
416        self_ty: Option<&hir::Type<'_>>,
417    ) -> Self {
418        ExtendedEnum::Enum {
419            enum_,
420            use_self: self_ty.is_some_and(|self_ty| self_ty.could_unify_with_deeply(db, enum_ty)),
421        }
422    }
423
424    fn is_non_exhaustive(&self, db: &RootDatabase, krate: Crate) -> bool {
425        match self {
426            ExtendedEnum::Enum { enum_: e, .. } => {
427                e.attrs(db).by_key(sym::non_exhaustive).exists() && e.module(db).krate() != krate
428            }
429            _ => false,
430        }
431    }
432
433    fn variants(&self, db: &RootDatabase) -> Vec<ExtendedVariant> {
434        match *self {
435            ExtendedEnum::Enum { enum_: e, use_self } => e
436                .variants(db)
437                .into_iter()
438                .map(|variant| ExtendedVariant::Variant { variant, use_self })
439                .collect::<Vec<_>>(),
440            ExtendedEnum::Bool => {
441                Vec::<ExtendedVariant>::from([ExtendedVariant::True, ExtendedVariant::False])
442            }
443        }
444    }
445}
446
447fn resolve_enum_def(
448    sema: &Semantics<'_, RootDatabase>,
449    expr: &ast::Expr,
450    self_ty: Option<&hir::Type<'_>>,
451) -> Option<ExtendedEnum> {
452    sema.type_of_expr(expr)?.adjusted().autoderef(sema.db).find_map(|ty| match ty.as_adt() {
453        Some(Adt::Enum(e)) => Some(ExtendedEnum::enum_(sema.db, e, &ty, self_ty)),
454        _ => ty.is_bool().then_some(ExtendedEnum::Bool),
455    })
456}
457
458fn resolve_tuple_of_enum_def(
459    sema: &Semantics<'_, RootDatabase>,
460    expr: &ast::Expr,
461    self_ty: Option<&hir::Type<'_>>,
462) -> Option<Vec<ExtendedEnum>> {
463    sema.type_of_expr(expr)?
464        .adjusted()
465        .tuple_fields(sema.db)
466        .iter()
467        .map(|ty| {
468            ty.autoderef(sema.db).find_map(|ty| {
469                match ty.as_adt() {
470                    Some(Adt::Enum(e)) => Some(ExtendedEnum::enum_(sema.db, e, &ty, self_ty)),
471                    // For now we only handle expansion for a tuple of enums. Here
472                    // we map non-enum items to None and rely on `collect` to
473                    // convert Vec<Option<hir::Enum>> into Option<Vec<hir::Enum>>.
474                    _ => ty.is_bool().then_some(ExtendedEnum::Bool),
475                }
476            })
477        })
478        .collect::<Option<Vec<ExtendedEnum>>>()
479        .and_then(|list| if list.is_empty() { None } else { Some(list) })
480}
481
482fn resolve_array_of_enum_def(
483    sema: &Semantics<'_, RootDatabase>,
484    expr: &ast::Expr,
485    self_ty: Option<&hir::Type<'_>>,
486) -> Option<(ExtendedEnum, usize)> {
487    sema.type_of_expr(expr)?.adjusted().as_array(sema.db).and_then(|(ty, len)| {
488        ty.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
489            Some(Adt::Enum(e)) => Some((ExtendedEnum::enum_(sema.db, e, &ty, self_ty), len)),
490            _ => ty.is_bool().then_some((ExtendedEnum::Bool, len)),
491        })
492    })
493}
494
495fn build_pat(
496    ctx: &AssistContext<'_>,
497    make: &SyntaxFactory,
498    module: hir::Module,
499    var: ExtendedVariant,
500    cfg: FindPathConfig,
501) -> Option<ast::Pat> {
502    let db = ctx.db();
503    match var {
504        ExtendedVariant::Variant { variant: var, use_self } => {
505            let edition = module.krate().edition(db);
506            let path = if use_self {
507                make::path_from_segments(
508                    [
509                        make::path_segment(make::name_ref_self_ty()),
510                        make::path_segment(make::name_ref(
511                            &var.name(db).display(db, edition).to_smolstr(),
512                        )),
513                    ],
514                    false,
515                )
516            } else {
517                mod_path_to_ast(&module.find_path(db, ModuleDef::from(var), cfg)?, edition)
518            };
519            let fields = var.fields(db);
520            let pat: ast::Pat = match var.kind(db) {
521                hir::StructKind::Tuple => {
522                    let mut name_generator = suggest_name::NameGenerator::default();
523                    let pats = fields.into_iter().map(|f| {
524                        let name = name_generator.for_type(&f.ty(db), db, edition);
525                        match name {
526                            Some(name) => make::ext::simple_ident_pat(make.name(&name)).into(),
527                            None => make.wildcard_pat().into(),
528                        }
529                    });
530                    make.tuple_struct_pat(path, pats).into()
531                }
532                hir::StructKind::Record => {
533                    let fields = fields
534                        .into_iter()
535                        .map(|f| make.ident_pat(false, false, make.name(f.name(db).as_str())))
536                        .map(|ident| make.record_pat_field_shorthand(ident.into()));
537                    let fields = make.record_pat_field_list(fields, None);
538                    make.record_pat_with_fields(path, fields).into()
539                }
540                hir::StructKind::Unit => make.path_pat(path),
541            };
542            Some(pat)
543        }
544        ExtendedVariant::True => Some(ast::Pat::from(make.literal_pat("true"))),
545        ExtendedVariant::False => Some(ast::Pat::from(make.literal_pat("false"))),
546    }
547}
548
549#[cfg(test)]
550mod tests {
551    use crate::AssistConfig;
552    use crate::tests::{
553        TEST_CONFIG, check_assist, check_assist_not_applicable, check_assist_target,
554        check_assist_unresolved, check_assist_with_config,
555    };
556
557    use super::add_missing_match_arms;
558
559    #[test]
560    fn all_match_arms_provided() {
561        check_assist_not_applicable(
562            add_missing_match_arms,
563            r#"
564enum A {
565    As,
566    Bs{x:i32, y:Option<i32>},
567    Cs(i32, Option<i32>),
568}
569fn main() {
570    match A::As$0 {
571        A::As,
572        A::Bs{x,y:Some(_)} => {}
573        A::Cs(_, Some(_)) => {}
574    }
575}
576            "#,
577        );
578    }
579
580    #[test]
581    fn not_applicable_outside_of_range_left() {
582        check_assist_not_applicable(
583            add_missing_match_arms,
584            r#"
585enum A { X, Y }
586
587fn foo(a: A) {
588    $0 match a {
589        A::X => { }
590    }
591}
592        "#,
593        );
594    }
595
596    #[test]
597    fn not_applicable_outside_of_range_right() {
598        cov_mark::check!(not_applicable_outside_of_range_right);
599        check_assist_not_applicable(
600            add_missing_match_arms,
601            r#"
602enum A { X, Y }
603
604fn foo(a: A) {
605    match a {$0
606        A::X => { }
607    }
608}
609        "#,
610        );
611    }
612
613    #[test]
614    fn all_boolean_match_arms_provided() {
615        check_assist_not_applicable(
616            add_missing_match_arms,
617            r#"
618fn foo(a: bool) {
619    match a$0 {
620        true => {}
621        false => {}
622    }
623}
624"#,
625        )
626    }
627
628    #[test]
629    fn tuple_of_non_enum() {
630        // for now this case is not handled, although it potentially could be
631        // in the future
632        check_assist_not_applicable(
633            add_missing_match_arms,
634            r#"
635fn main() {
636    match (0, false)$0 {
637    }
638}
639"#,
640        );
641    }
642
643    #[test]
644    fn add_missing_match_arms_boolean() {
645        check_assist(
646            add_missing_match_arms,
647            r#"
648fn foo(a: bool) {
649    match a$0 {
650    }
651}
652"#,
653            r#"
654fn foo(a: bool) {
655    match a {
656        true => ${1:todo!()},
657        false => ${2:todo!()},$0
658    }
659}
660"#,
661        )
662    }
663
664    #[test]
665    fn partial_fill_boolean() {
666        check_assist(
667            add_missing_match_arms,
668            r#"
669fn foo(a: bool) {
670    match a$0 {
671        true => {}
672    }
673}
674"#,
675            r#"
676fn foo(a: bool) {
677    match a {
678        true => {}
679        false => ${1:todo!()},$0
680    }
681}
682"#,
683        )
684    }
685
686    #[test]
687    fn all_boolean_tuple_arms_provided() {
688        check_assist_not_applicable(
689            add_missing_match_arms,
690            r#"
691fn foo(a: bool) {
692    match (a, a)$0 {
693        (true | false, true) => {}
694        (true, false) => {}
695        (false, false) => {}
696    }
697}
698"#,
699        );
700
701        check_assist_not_applicable(
702            add_missing_match_arms,
703            r#"
704fn foo(a: bool) {
705    match (a, a)$0 {
706        (true, true) => {}
707        (true, false) => {}
708        (false, true) => {}
709        (false, false) => {}
710    }
711}
712"#,
713        )
714    }
715
716    #[test]
717    fn fill_boolean_tuple() {
718        check_assist(
719            add_missing_match_arms,
720            r#"
721fn foo(a: bool) {
722    match (a, a)$0 {
723    }
724}
725"#,
726            r#"
727fn foo(a: bool) {
728    match (a, a) {
729        (true, true) => ${1:todo!()},
730        (true, false) => ${2:todo!()},
731        (false, true) => ${3:todo!()},
732        (false, false) => ${4:todo!()},$0
733    }
734}
735"#,
736        )
737    }
738
739    #[test]
740    fn fill_boolean_array() {
741        check_assist(
742            add_missing_match_arms,
743            r#"
744fn foo(a: bool) {
745    match [a]$0 {
746    }
747}
748"#,
749            r#"
750fn foo(a: bool) {
751    match [a] {
752        [true] => ${1:todo!()},
753        [false] => ${2:todo!()},$0
754    }
755}
756"#,
757        );
758
759        check_assist(
760            add_missing_match_arms,
761            r#"
762fn foo(a: bool) {
763    match [a,]$0 {
764    }
765}
766"#,
767            r#"
768fn foo(a: bool) {
769    match [a,] {
770        [true] => ${1:todo!()},
771        [false] => ${2:todo!()},$0
772    }
773}
774"#,
775        );
776
777        check_assist(
778            add_missing_match_arms,
779            r#"
780fn foo(a: bool) {
781    match [a, a]$0 {
782        [true, true] => todo!(),
783    }
784}
785"#,
786            r#"
787fn foo(a: bool) {
788    match [a, a] {
789        [true, true] => todo!(),
790        [true, false] => ${1:todo!()},
791        [false, true] => ${2:todo!()},
792        [false, false] => ${3:todo!()},$0
793    }
794}
795"#,
796        );
797
798        check_assist(
799            add_missing_match_arms,
800            r#"
801fn foo(a: bool) {
802    match [a, a]$0 {
803    }
804}
805"#,
806            r#"
807fn foo(a: bool) {
808    match [a, a] {
809        [true, true] => ${1:todo!()},
810        [true, false] => ${2:todo!()},
811        [false, true] => ${3:todo!()},
812        [false, false] => ${4:todo!()},$0
813    }
814}
815"#,
816        )
817    }
818
819    #[test]
820    fn partial_fill_boolean_tuple() {
821        check_assist(
822            add_missing_match_arms,
823            r#"
824fn foo(a: bool) {
825    match (a, a)$0 {
826        (true | false, true) => {}
827    }
828}
829"#,
830            r#"
831fn foo(a: bool) {
832    match (a, a) {
833        (true | false, true) => {}
834        (true, false) => ${1:todo!()},
835        (false, false) => ${2:todo!()},$0
836    }
837}
838"#,
839        );
840
841        check_assist(
842            add_missing_match_arms,
843            r#"
844fn foo(a: bool) {
845    match (a, a)$0 {
846        (false, true) => {}
847    }
848}
849"#,
850            r#"
851fn foo(a: bool) {
852    match (a, a) {
853        (false, true) => {}
854        (true, true) => ${1:todo!()},
855        (true, false) => ${2:todo!()},
856        (false, false) => ${3:todo!()},$0
857    }
858}
859"#,
860        )
861    }
862
863    #[test]
864    fn partial_fill_record_tuple() {
865        check_assist(
866            add_missing_match_arms,
867            r#"
868enum A {
869    As,
870    Bs { x: i32, y: Option<i32> },
871    Cs(i32, Option<i32>),
872}
873fn main() {
874    match A::As$0 {
875        A::Bs { x, y: Some(_) } => {}
876        A::Cs(_, Some(_)) => {}
877    }
878}
879"#,
880            r#"
881enum A {
882    As,
883    Bs { x: i32, y: Option<i32> },
884    Cs(i32, Option<i32>),
885}
886fn main() {
887    match A::As {
888        A::Bs { x, y: Some(_) } => {}
889        A::Cs(_, Some(_)) => {}
890        A::As => ${1:todo!()},$0
891    }
892}
893"#,
894        );
895    }
896
897    #[test]
898    fn partial_fill_option() {
899        check_assist(
900            add_missing_match_arms,
901            r#"
902//- minicore: option
903fn main() {
904    match None$0 {
905        None => {}
906    }
907}
908"#,
909            r#"
910fn main() {
911    match None {
912        None => {}
913        Some(${1:_}) => ${2:todo!()},$0
914    }
915}
916"#,
917        );
918    }
919
920    #[test]
921    fn partial_fill_or_pat() {
922        check_assist(
923            add_missing_match_arms,
924            r#"
925enum A { As, Bs, Cs(Option<i32>) }
926fn main() {
927    match A::As$0 {
928        A::Cs(_) | A::Bs => {}
929    }
930}
931"#,
932            r#"
933enum A { As, Bs, Cs(Option<i32>) }
934fn main() {
935    match A::As {
936        A::Cs(_) | A::Bs => {}
937        A::As => ${1:todo!()},$0
938    }
939}
940"#,
941        );
942    }
943
944    #[test]
945    fn partial_fill() {
946        check_assist(
947            add_missing_match_arms,
948            r#"
949enum A { As, Bs, Cs, Ds(String), Es(B) }
950enum B { Xs, Ys }
951fn main() {
952    match A::As$0 {
953        A::Bs if 0 < 1 => {}
954        A::Ds(_value) => { let x = 1; }
955        A::Es(B::Xs) => (),
956    }
957}
958"#,
959            r#"
960enum A { As, Bs, Cs, Ds(String), Es(B) }
961enum B { Xs, Ys }
962fn main() {
963    match A::As {
964        A::Bs if 0 < 1 => {}
965        A::Ds(_value) => { let x = 1; }
966        A::Es(B::Xs) => (),
967        A::As => ${1:todo!()},
968        A::Cs => ${2:todo!()},$0
969    }
970}
971"#,
972        );
973    }
974
975    #[test]
976    fn partial_fill_bind_pat() {
977        check_assist(
978            add_missing_match_arms,
979            r#"
980enum A { As, Bs, Cs(Option<i32>) }
981fn main() {
982    match A::As$0 {
983        A::As(_) => {}
984        a @ A::Bs(_) => {}
985    }
986}
987"#,
988            r#"
989enum A { As, Bs, Cs(Option<i32>) }
990fn main() {
991    match A::As {
992        A::As(_) => {}
993        a @ A::Bs(_) => {}
994        A::Cs(${1:_}) => ${2:todo!()},$0
995    }
996}
997"#,
998        );
999    }
1000
1001    #[test]
1002    fn add_missing_match_arms_empty_body() {
1003        cov_mark::check!(add_missing_match_arms_empty_body);
1004        check_assist(
1005            add_missing_match_arms,
1006            r#"
1007enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
1008
1009fn main() {
1010    let a = A::As;
1011    match a {$0}
1012}
1013"#,
1014            r#"
1015enum A { As, Bs, Cs(String), Ds(String, String), Es { x: usize, y: usize } }
1016
1017fn main() {
1018    let a = A::As;
1019    match a {
1020        A::As => ${1:todo!()},
1021        A::Bs => ${2:todo!()},
1022        A::Cs(_) => ${3:todo!()},
1023        A::Ds(_, _) => ${4:todo!()},
1024        A::Es { x, y } => ${5:todo!()},$0
1025    }
1026}
1027"#,
1028        );
1029    }
1030
1031    #[test]
1032    fn add_missing_match_arms_end_of_last_arm() {
1033        cov_mark::check!(add_missing_match_arms_end_of_last_arm);
1034        check_assist(
1035            add_missing_match_arms,
1036            r#"
1037enum A { One, Two }
1038enum B { One, Two }
1039
1040fn main() {
1041    let a = A::One;
1042    let b = B::One;
1043    match (a, b) {
1044        (A::Two, B::One) => {},$0
1045    }
1046}
1047"#,
1048            r#"
1049enum A { One, Two }
1050enum B { One, Two }
1051
1052fn main() {
1053    let a = A::One;
1054    let b = B::One;
1055    match (a, b) {
1056        (A::Two, B::One) => {},
1057        (A::One, B::One) => ${1:todo!()},
1058        (A::One, B::Two) => ${2:todo!()},
1059        (A::Two, B::Two) => ${3:todo!()},$0
1060    }
1061}
1062"#,
1063        );
1064    }
1065
1066    #[test]
1067    fn add_missing_match_arms_tuple_of_enum() {
1068        check_assist(
1069            add_missing_match_arms,
1070            r#"
1071enum A { One, Two }
1072enum B { One, Two }
1073
1074fn main() {
1075    let a = A::One;
1076    let b = B::One;
1077    match (a$0, b) {}
1078}
1079"#,
1080            r#"
1081enum A { One, Two }
1082enum B { One, Two }
1083
1084fn main() {
1085    let a = A::One;
1086    let b = B::One;
1087    match (a, b) {
1088        (A::One, B::One) => ${1:todo!()},
1089        (A::One, B::Two) => ${2:todo!()},
1090        (A::Two, B::One) => ${3:todo!()},
1091        (A::Two, B::Two) => ${4:todo!()},$0
1092    }
1093}
1094"#,
1095        );
1096    }
1097
1098    #[test]
1099    fn add_missing_match_arms_tuple_of_enum_ref() {
1100        check_assist(
1101            add_missing_match_arms,
1102            r#"
1103enum A { One, Two }
1104enum B { One, Two }
1105
1106fn main() {
1107    let a = A::One;
1108    let b = B::One;
1109    match (&a$0, &b) {}
1110}
1111"#,
1112            r#"
1113enum A { One, Two }
1114enum B { One, Two }
1115
1116fn main() {
1117    let a = A::One;
1118    let b = B::One;
1119    match (&a, &b) {
1120        (A::One, B::One) => ${1:todo!()},
1121        (A::One, B::Two) => ${2:todo!()},
1122        (A::Two, B::One) => ${3:todo!()},
1123        (A::Two, B::Two) => ${4:todo!()},$0
1124    }
1125}
1126"#,
1127        );
1128    }
1129
1130    #[test]
1131    fn add_missing_match_arms_tuple_of_enum_partial() {
1132        check_assist(
1133            add_missing_match_arms,
1134            r#"
1135enum A { One, Two }
1136enum B { One, Two }
1137
1138fn main() {
1139    let a = A::One;
1140    let b = B::One;
1141    match (a$0, b) {
1142        (A::Two, B::One) => {}
1143    }
1144}
1145"#,
1146            r#"
1147enum A { One, Two }
1148enum B { One, Two }
1149
1150fn main() {
1151    let a = A::One;
1152    let b = B::One;
1153    match (a, b) {
1154        (A::Two, B::One) => {}
1155        (A::One, B::One) => ${1:todo!()},
1156        (A::One, B::Two) => ${2:todo!()},
1157        (A::Two, B::Two) => ${3:todo!()},$0
1158    }
1159}
1160"#,
1161        );
1162
1163        check_assist(
1164            add_missing_match_arms,
1165            r#"
1166enum E { A, B, C }
1167fn main() {
1168    use E::*;
1169    match (A, B, C)$0 {
1170        (A | B , A, A | B | C) => (),
1171        (A | B | C , B | C, A | B | C) => (),
1172    }
1173}
1174"#,
1175            r#"
1176enum E { A, B, C }
1177fn main() {
1178    use E::*;
1179    match (A, B, C) {
1180        (A | B , A, A | B | C) => (),
1181        (A | B | C , B | C, A | B | C) => (),
1182        (C, A, A) => ${1:todo!()},
1183        (C, A, B) => ${2:todo!()},
1184        (C, A, C) => ${3:todo!()},$0
1185    }
1186}
1187"#,
1188        )
1189    }
1190
1191    #[test]
1192    fn add_missing_match_arms_tuple_of_enum_partial_with_wildcards() {
1193        check_assist(
1194            add_missing_match_arms,
1195            r#"
1196//- minicore: option
1197fn main() {
1198    let a = Some(1);
1199    let b = Some(());
1200    match (a$0, b) {
1201        (Some(_), _) => {}
1202        (None, Some(_)) => {}
1203    }
1204}
1205"#,
1206            r#"
1207fn main() {
1208    let a = Some(1);
1209    let b = Some(());
1210    match (a, b) {
1211        (Some(_), _) => {}
1212        (None, Some(_)) => {}
1213        (None, None) => ${1:todo!()},$0
1214    }
1215}
1216"#,
1217        );
1218    }
1219
1220    #[test]
1221    fn add_missing_match_arms_partial_with_deep_pattern() {
1222        // Fixme: cannot handle deep patterns
1223        check_assist_not_applicable(
1224            add_missing_match_arms,
1225            r#"
1226//- minicore: option
1227fn main() {
1228    match $0Some(true) {
1229        Some(true) => {}
1230        None => {}
1231    }
1232}
1233"#,
1234        );
1235    }
1236
1237    #[test]
1238    fn add_missing_match_arms_tuple_of_enum_not_applicable() {
1239        check_assist_not_applicable(
1240            add_missing_match_arms,
1241            r#"
1242enum A { One, Two }
1243enum B { One, Two }
1244
1245fn main() {
1246    let a = A::One;
1247    let b = B::One;
1248    match (a$0, b) {
1249        (A::Two, B::One) => {}
1250        (A::One, B::One) => {}
1251        (A::One, B::Two) => {}
1252        (A::Two, B::Two) => {}
1253    }
1254}
1255"#,
1256        );
1257    }
1258
1259    #[test]
1260    fn add_missing_match_arms_single_element_tuple_of_enum() {
1261        check_assist(
1262            add_missing_match_arms,
1263            r#"
1264enum A { One, Two }
1265
1266fn main() {
1267    let a = A::One;
1268    match (a$0, ) {
1269    }
1270}
1271"#,
1272            r#"
1273enum A { One, Two }
1274
1275fn main() {
1276    let a = A::One;
1277    match (a, ) {
1278        (A::One,) => ${1:todo!()},
1279        (A::Two,) => ${2:todo!()},$0
1280    }
1281}
1282"#,
1283        );
1284    }
1285
1286    #[test]
1287    fn test_fill_match_arm_refs() {
1288        check_assist(
1289            add_missing_match_arms,
1290            r#"
1291enum A { As }
1292
1293fn foo(a: &A) {
1294    match a$0 {
1295    }
1296}
1297"#,
1298            r#"
1299enum A { As }
1300
1301fn foo(a: &A) {
1302    match a {
1303        A::As => ${1:todo!()},$0
1304    }
1305}
1306"#,
1307        );
1308
1309        check_assist(
1310            add_missing_match_arms,
1311            r#"
1312enum A {
1313    Es { x: usize, y: usize }
1314}
1315
1316fn foo(a: &mut A) {
1317    match a$0 {
1318    }
1319}
1320"#,
1321            r#"
1322enum A {
1323    Es { x: usize, y: usize }
1324}
1325
1326fn foo(a: &mut A) {
1327    match a {
1328        A::Es { x, y } => ${1:todo!()},$0
1329    }
1330}
1331"#,
1332        );
1333    }
1334
1335    #[test]
1336    fn add_missing_match_arms_target_simple() {
1337        check_assist_target(
1338            add_missing_match_arms,
1339            r#"
1340enum E { X, Y }
1341
1342fn main() {
1343    match E::X$0 {}
1344}
1345"#,
1346            "match E::X {}",
1347        );
1348    }
1349
1350    #[test]
1351    fn add_missing_match_arms_target_complex() {
1352        check_assist_target(
1353            add_missing_match_arms,
1354            r#"
1355enum E { X, Y }
1356
1357fn main() {
1358    match E::X$0 {
1359        E::X => {}
1360    }
1361}
1362"#,
1363            "match E::X {
1364        E::X => {}
1365    }",
1366        );
1367    }
1368
1369    #[test]
1370    fn add_missing_match_arms_trivial_arm() {
1371        cov_mark::check!(add_missing_match_arms_trivial_arm);
1372        check_assist(
1373            add_missing_match_arms,
1374            r#"
1375enum E { X, Y }
1376
1377fn main() {
1378    match E::X {
1379        $0_ => {}
1380    }
1381}
1382"#,
1383            r#"
1384enum E { X, Y }
1385
1386fn main() {
1387    match E::X {
1388        E::X => ${1:todo!()},
1389        E::Y => ${2:todo!()},$0
1390    }
1391}
1392"#,
1393        );
1394    }
1395
1396    #[test]
1397    fn wildcard_inside_expression_not_applicable() {
1398        check_assist_not_applicable(
1399            add_missing_match_arms,
1400            r#"
1401enum E { X, Y }
1402
1403fn foo(e : E) {
1404    match e {
1405        _ => {
1406            println!("1");$0
1407            println!("2");
1408        }
1409    }
1410}
1411"#,
1412        );
1413    }
1414
1415    #[test]
1416    fn add_missing_match_arms_qualifies_path() {
1417        check_assist(
1418            add_missing_match_arms,
1419            r#"
1420mod foo { pub enum E { X, Y } }
1421use foo::E::X;
1422
1423fn main() {
1424    match X {
1425        $0
1426    }
1427}
1428"#,
1429            r#"
1430mod foo { pub enum E { X, Y } }
1431use foo::E::X;
1432
1433fn main() {
1434    match X {
1435        X => ${1:todo!()},
1436        foo::E::Y => ${2:todo!()},$0
1437    }
1438}
1439"#,
1440        );
1441    }
1442
1443    // FIXME: Preserving comments is quite hard in the current transitional syntax editing model.
1444    // Once we migrate to new trivia model addressed in #6854, remove the ignore attribute.
1445    #[ignore]
1446    #[test]
1447    fn add_missing_match_arms_preserves_comments() {
1448        check_assist(
1449            add_missing_match_arms,
1450            r#"
1451enum A { One, Two }
1452fn foo(a: A) {
1453    match a $0 {
1454        // foo bar baz
1455        A::One => {}
1456        // This is where the rest should be
1457    }
1458}
1459"#,
1460            r#"
1461enum A { One, Two }
1462fn foo(a: A) {
1463    match a  {
1464        // foo bar baz
1465        A::One => {}
1466        A::Two => ${1:todo!()},$0
1467        // This is where the rest should be
1468    }
1469}
1470"#,
1471        );
1472    }
1473
1474    // FIXME: Preserving comments is quite hard in the current transitional syntax editing model.
1475    // Once we migrate to new trivia model addressed in #6854, remove the ignore attribute.
1476    #[ignore]
1477    #[test]
1478    fn add_missing_match_arms_preserves_comments_empty() {
1479        check_assist(
1480            add_missing_match_arms,
1481            r#"
1482enum A { One, Two }
1483fn foo(a: A) {
1484    match a {
1485        // foo bar baz$0
1486    }
1487}
1488"#,
1489            r#"
1490enum A { One, Two }
1491fn foo(a: A) {
1492    match a {
1493        A::One => ${1:todo!()},
1494        A::Two => ${2:todo!()},$0
1495        // foo bar baz
1496    }
1497}
1498"#,
1499        );
1500    }
1501
1502    #[test]
1503    fn add_missing_match_arms_placeholder() {
1504        check_assist(
1505            add_missing_match_arms,
1506            r#"
1507enum A { One, Two, }
1508fn foo(a: A) {
1509    match a$0 {
1510        _ => (),
1511    }
1512}
1513"#,
1514            r#"
1515enum A { One, Two, }
1516fn foo(a: A) {
1517    match a {
1518        A::One => ${1:todo!()},
1519        A::Two => ${2:todo!()},$0
1520    }
1521}
1522"#,
1523        );
1524    }
1525
1526    #[test]
1527    fn option_order() {
1528        cov_mark::check!(option_order);
1529        check_assist(
1530            add_missing_match_arms,
1531            r#"
1532//- minicore: option
1533fn foo(opt: Option<i32>) {
1534    match opt$0 {
1535    }
1536}
1537"#,
1538            r#"
1539fn foo(opt: Option<i32>) {
1540    match opt {
1541        Some(${1:_}) => ${2:todo!()},
1542        None => ${3:todo!()},$0
1543    }
1544}
1545"#,
1546        );
1547    }
1548
1549    #[test]
1550    fn works_inside_macro_call() {
1551        check_assist(
1552            add_missing_match_arms,
1553            r#"
1554macro_rules! m { ($expr:expr) => {$expr}}
1555enum Test {
1556    A,
1557    B,
1558    C,
1559}
1560
1561fn foo(t: Test) {
1562    m!(match t$0 {});
1563}"#,
1564            r#"
1565macro_rules! m { ($expr:expr) => {$expr}}
1566enum Test {
1567    A,
1568    B,
1569    C,
1570}
1571
1572fn foo(t: Test) {
1573    m!(match t {
1574        Test::A => ${1:todo!()},
1575        Test::B => ${2:todo!()},
1576        Test::C => ${3:todo!()},$0
1577    });
1578}"#,
1579        );
1580    }
1581
1582    #[test]
1583    fn lazy_computation() {
1584        // Computing a single missing arm is enough to determine applicability of the assist.
1585        cov_mark::check_count!(add_missing_match_arms_lazy_computation, 1);
1586        check_assist_unresolved(
1587            add_missing_match_arms,
1588            r#"
1589enum A { One, Two, }
1590fn foo(tuple: (A, A)) {
1591    match $0tuple {};
1592}
1593"#,
1594        );
1595    }
1596
1597    #[test]
1598    fn adds_comma_before_new_arms() {
1599        check_assist(
1600            add_missing_match_arms,
1601            r#"
1602fn foo(t: bool) {
1603    match $0t {
1604        true => 1 + 2
1605    }
1606}"#,
1607            r#"
1608fn foo(t: bool) {
1609    match t {
1610        true => 1 + 2,
1611        false => ${1:todo!()},$0
1612    }
1613}"#,
1614        );
1615    }
1616
1617    #[test]
1618    fn does_not_add_extra_comma() {
1619        check_assist(
1620            add_missing_match_arms,
1621            r#"
1622fn foo(t: bool) {
1623    match $0t {
1624        true => 1 + 2,
1625    }
1626}"#,
1627            r#"
1628fn foo(t: bool) {
1629    match t {
1630        true => 1 + 2,
1631        false => ${1:todo!()},$0
1632    }
1633}"#,
1634        );
1635    }
1636
1637    #[test]
1638    fn does_not_remove_catch_all_with_non_empty_expr() {
1639        cov_mark::check!(add_missing_match_arms_empty_expr);
1640        check_assist(
1641            add_missing_match_arms,
1642            r#"
1643fn foo(t: bool) {
1644    match $0t {
1645        _ => 1 + 2,
1646    }
1647}"#,
1648            r#"
1649fn foo(t: bool) {
1650    match t {
1651        _ => 1 + 2,
1652        true => ${1:todo!()},
1653        false => ${2:todo!()},$0
1654    }
1655}"#,
1656        );
1657    }
1658
1659    #[test]
1660    fn does_not_fill_hidden_variants() {
1661        cov_mark::check!(added_wildcard_pattern);
1662        check_assist(
1663            add_missing_match_arms,
1664            r#"
1665//- /main.rs crate:main deps:e
1666fn foo(t: ::e::E) {
1667    match $0t {
1668    }
1669}
1670//- /e.rs crate:e
1671pub enum E { A, #[doc(hidden)] B, }
1672"#,
1673            r#"
1674fn foo(t: ::e::E) {
1675    match t {
1676        e::E::A => ${1:todo!()},
1677        _ => ${2:todo!()},$0
1678    }
1679}
1680"#,
1681        );
1682    }
1683
1684    #[test]
1685    fn does_not_fill_hidden_variants_tuple() {
1686        cov_mark::check!(added_wildcard_pattern);
1687        check_assist(
1688            add_missing_match_arms,
1689            r#"
1690//- /main.rs crate:main deps:e
1691fn foo(t: (bool, ::e::E)) {
1692    match $0t {
1693    }
1694}
1695//- /e.rs crate:e
1696pub enum E { A, #[doc(hidden)] B, }
1697"#,
1698            r#"
1699fn foo(t: (bool, ::e::E)) {
1700    match t {
1701        (true, e::E::A) => ${1:todo!()},
1702        (false, e::E::A) => ${2:todo!()},
1703        _ => ${3:todo!()},$0
1704    }
1705}
1706"#,
1707        );
1708    }
1709
1710    #[test]
1711    fn fills_wildcard_with_only_hidden_variants() {
1712        cov_mark::check!(added_wildcard_pattern);
1713        check_assist(
1714            add_missing_match_arms,
1715            r#"
1716//- /main.rs crate:main deps:e
1717fn foo(t: ::e::E) {
1718    match $0t {
1719    }
1720}
1721//- /e.rs crate:e
1722pub enum E { #[doc(hidden)] A, }
1723"#,
1724            r#"
1725fn foo(t: ::e::E) {
1726    match t {
1727        ${1:_} => ${2:todo!()},$0
1728    }
1729}
1730"#,
1731        );
1732    }
1733
1734    #[test]
1735    fn does_not_fill_wildcard_when_hidden_variants_are_explicit() {
1736        check_assist_not_applicable(
1737            add_missing_match_arms,
1738            r#"
1739//- /main.rs crate:main deps:e
1740fn foo(t: ::e::E) {
1741    match $0t {
1742        e::E::A => todo!(),
1743    }
1744}
1745//- /e.rs crate:e
1746pub enum E { #[doc(hidden)] A, }
1747"#,
1748        );
1749    }
1750
1751    #[test]
1752    fn does_not_fill_wildcard_with_wildcard() {
1753        check_assist_not_applicable(
1754            add_missing_match_arms,
1755            r#"
1756//- /main.rs crate:main deps:e
1757fn foo(t: ::e::E) {
1758    match $0t {
1759        _ => todo!(),
1760    }
1761}
1762//- /e.rs crate:e
1763pub enum E { #[doc(hidden)] A, }
1764"#,
1765        );
1766    }
1767
1768    #[test]
1769    fn fills_wildcard_on_non_exhaustive_with_explicit_matches() {
1770        cov_mark::check!(added_wildcard_pattern);
1771        check_assist(
1772            add_missing_match_arms,
1773            r#"
1774//- /main.rs crate:main deps:e
1775fn foo(t: ::e::E) {
1776    match $0t {
1777        e::E::A => todo!(),
1778    }
1779}
1780//- /e.rs crate:e
1781#[non_exhaustive]
1782pub enum E { A, }
1783"#,
1784            r#"
1785fn foo(t: ::e::E) {
1786    match t {
1787        e::E::A => todo!(),
1788        ${1:_} => ${2:todo!()},$0
1789    }
1790}
1791"#,
1792        );
1793    }
1794
1795    #[test]
1796    fn fills_wildcard_on_non_exhaustive_without_matches() {
1797        cov_mark::check!(added_wildcard_pattern);
1798        check_assist(
1799            add_missing_match_arms,
1800            r#"
1801//- /main.rs crate:main deps:e
1802fn foo(t: ::e::E) {
1803    match $0t {
1804    }
1805}
1806//- /e.rs crate:e
1807#[non_exhaustive]
1808pub enum E { A, }
1809"#,
1810            r#"
1811fn foo(t: ::e::E) {
1812    match t {
1813        e::E::A => ${1:todo!()},
1814        _ => ${2:todo!()},$0
1815    }
1816}
1817"#,
1818        );
1819    }
1820
1821    #[test]
1822    fn fills_wildcard_on_non_exhaustive_with_doc_hidden() {
1823        cov_mark::check!(added_wildcard_pattern);
1824        check_assist(
1825            add_missing_match_arms,
1826            r#"
1827//- /main.rs crate:main deps:e
1828fn foo(t: ::e::E) {
1829    match $0t {
1830    }
1831}
1832//- /e.rs crate:e
1833#[non_exhaustive]
1834pub enum E { A, #[doc(hidden)] B }"#,
1835            r#"
1836fn foo(t: ::e::E) {
1837    match t {
1838        e::E::A => ${1:todo!()},
1839        _ => ${2:todo!()},$0
1840    }
1841}
1842"#,
1843        );
1844    }
1845
1846    #[test]
1847    fn fills_wildcard_on_non_exhaustive_with_doc_hidden_with_explicit_arms() {
1848        cov_mark::check!(added_wildcard_pattern);
1849        check_assist(
1850            add_missing_match_arms,
1851            r#"
1852//- /main.rs crate:main deps:e
1853fn foo(t: ::e::E) {
1854    match $0t {
1855        e::E::A => todo!(),
1856    }
1857}
1858//- /e.rs crate:e
1859#[non_exhaustive]
1860pub enum E { A, #[doc(hidden)] B }"#,
1861            r#"
1862fn foo(t: ::e::E) {
1863    match t {
1864        e::E::A => todo!(),
1865        ${1:_} => ${2:todo!()},$0
1866    }
1867}
1868"#,
1869        );
1870    }
1871
1872    #[test]
1873    fn fill_wildcard_with_partial_wildcard() {
1874        cov_mark::check!(added_wildcard_pattern);
1875        check_assist(
1876            add_missing_match_arms,
1877            r#"
1878//- /main.rs crate:main deps:e
1879fn foo(t: ::e::E, b: bool) {
1880    match $0t {
1881        _ if b => todo!(),
1882    }
1883}
1884//- /e.rs crate:e
1885pub enum E { #[doc(hidden)] A, }"#,
1886            r#"
1887fn foo(t: ::e::E, b: bool) {
1888    match t {
1889        _ if b => todo!(),
1890        ${1:_} => ${2:todo!()},$0
1891    }
1892}
1893"#,
1894        );
1895    }
1896
1897    #[test]
1898    fn does_not_fill_wildcard_with_partial_wildcard_and_wildcard() {
1899        check_assist_not_applicable(
1900            add_missing_match_arms,
1901            r#"
1902//- /main.rs crate:main deps:e
1903fn foo(t: ::e::E, b: bool) {
1904    match $0t {
1905        _ if b => todo!(),
1906        _ => todo!(),
1907    }
1908}
1909//- /e.rs crate:e
1910pub enum E { #[doc(hidden)] A, }"#,
1911        );
1912    }
1913
1914    #[test]
1915    fn non_exhaustive_doc_hidden_tuple_fills_wildcard() {
1916        cov_mark::check!(added_wildcard_pattern);
1917        check_assist(
1918            add_missing_match_arms,
1919            r#"
1920//- /main.rs crate:main deps:e
1921fn foo(t: ::e::E) {
1922    match $0t {
1923    }
1924}
1925//- /e.rs crate:e
1926#[non_exhaustive]
1927pub enum E { A, #[doc(hidden)] B, }"#,
1928            r#"
1929fn foo(t: ::e::E) {
1930    match t {
1931        e::E::A => ${1:todo!()},
1932        _ => ${2:todo!()},$0
1933    }
1934}
1935"#,
1936        );
1937    }
1938
1939    #[test]
1940    fn ignores_doc_hidden_for_crate_local_enums() {
1941        check_assist(
1942            add_missing_match_arms,
1943            r#"
1944enum E { A, #[doc(hidden)] B, }
1945
1946fn foo(t: E) {
1947    match $0t {
1948    }
1949}"#,
1950            r#"
1951enum E { A, #[doc(hidden)] B, }
1952
1953fn foo(t: E) {
1954    match t {
1955        E::A => ${1:todo!()},
1956        E::B => ${2:todo!()},$0
1957    }
1958}"#,
1959        );
1960    }
1961
1962    #[test]
1963    fn ignores_non_exhaustive_for_crate_local_enums() {
1964        check_assist(
1965            add_missing_match_arms,
1966            r#"
1967#[non_exhaustive]
1968enum E { A, B, }
1969
1970fn foo(t: E) {
1971    match $0t {
1972    }
1973}"#,
1974            r#"
1975#[non_exhaustive]
1976enum E { A, B, }
1977
1978fn foo(t: E) {
1979    match t {
1980        E::A => ${1:todo!()},
1981        E::B => ${2:todo!()},$0
1982    }
1983}"#,
1984        );
1985    }
1986
1987    #[test]
1988    fn ignores_doc_hidden_and_non_exhaustive_for_crate_local_enums() {
1989        check_assist(
1990            add_missing_match_arms,
1991            r#"
1992#[non_exhaustive]
1993enum E { A, #[doc(hidden)] B, }
1994
1995fn foo(t: E) {
1996    match $0t {
1997    }
1998}"#,
1999            r#"
2000#[non_exhaustive]
2001enum E { A, #[doc(hidden)] B, }
2002
2003fn foo(t: E) {
2004    match t {
2005        E::A => ${1:todo!()},
2006        E::B => ${2:todo!()},$0
2007    }
2008}"#,
2009        );
2010    }
2011
2012    #[test]
2013    fn not_applicable_when_match_arm_list_cannot_be_upmapped() {
2014        check_assist_not_applicable(
2015            add_missing_match_arms,
2016            r#"
2017macro_rules! foo {
2018    ($($t:tt)*) => {
2019        $($t)* {}
2020    }
2021}
2022
2023enum E { A }
2024
2025fn main() {
2026    foo!(match E::A$0);
2027}
2028"#,
2029        );
2030    }
2031
2032    /// See [`discussion`](https://github.com/rust-lang/rust-analyzer/pull/15594#discussion_r1322960614)
2033    #[test]
2034    fn missing_field_name() {
2035        check_assist(
2036            add_missing_match_arms,
2037            r#"
2038enum A {
2039    A,
2040    Missing { a: u32, : u32, c: u32 }
2041}
2042
2043fn a() {
2044    let b = A::A;
2045    match b$0 {}
2046}"#,
2047            r#"
2048enum A {
2049    A,
2050    Missing { a: u32, : u32, c: u32 }
2051}
2052
2053fn a() {
2054    let b = A::A;
2055    match b {
2056        A::A => ${1:todo!()},
2057        A::Missing { a, u32, c } => ${2:todo!()},$0
2058    }
2059}"#,
2060        )
2061    }
2062
2063    #[test]
2064    fn suggest_name_for_tuple_struct_patterns() {
2065        // single tuple struct
2066        check_assist(
2067            add_missing_match_arms,
2068            r#"
2069struct S;
2070
2071pub enum E {
2072    A
2073    B(S),
2074}
2075
2076fn f() {
2077    let value = E::A;
2078    match value {
2079        $0
2080    }
2081}
2082"#,
2083            r#"
2084struct S;
2085
2086pub enum E {
2087    A
2088    B(S),
2089}
2090
2091fn f() {
2092    let value = E::A;
2093    match value {
2094        E::A => ${1:todo!()},
2095        E::B(s) => ${2:todo!()},$0
2096    }
2097}
2098"#,
2099        );
2100
2101        // multiple tuple struct patterns
2102        check_assist(
2103            add_missing_match_arms,
2104            r#"
2105struct S1;
2106struct S2;
2107
2108pub enum E {
2109    A
2110    B(S1, S2),
2111}
2112
2113fn f() {
2114    let value = E::A;
2115    match value {
2116        $0
2117    }
2118}
2119"#,
2120            r#"
2121struct S1;
2122struct S2;
2123
2124pub enum E {
2125    A
2126    B(S1, S2),
2127}
2128
2129fn f() {
2130    let value = E::A;
2131    match value {
2132        E::A => ${1:todo!()},
2133        E::B(s1, s2) => ${2:todo!()},$0
2134    }
2135}
2136"#,
2137        );
2138    }
2139
2140    #[test]
2141    fn prefer_self() {
2142        check_assist_with_config(
2143            add_missing_match_arms,
2144            AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
2145            r#"
2146enum Foo {
2147    Bar,
2148    Baz,
2149}
2150
2151impl Foo {
2152    fn qux(&self) {
2153        match self {
2154            $0_ => {}
2155        }
2156    }
2157}
2158            "#,
2159            r#"
2160enum Foo {
2161    Bar,
2162    Baz,
2163}
2164
2165impl Foo {
2166    fn qux(&self) {
2167        match self {
2168            Self::Bar => ${1:todo!()},
2169            Self::Baz => ${2:todo!()},$0
2170        }
2171    }
2172}
2173            "#,
2174        );
2175    }
2176
2177    #[test]
2178    fn prefer_self_with_generics() {
2179        check_assist_with_config(
2180            add_missing_match_arms,
2181            AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
2182            r#"
2183enum Foo<T> {
2184    Bar(T),
2185    Baz,
2186}
2187
2188impl<T> Foo<T> {
2189    fn qux(&self) {
2190        match self {
2191            $0_ => {}
2192        }
2193    }
2194}
2195            "#,
2196            r#"
2197enum Foo<T> {
2198    Bar(T),
2199    Baz,
2200}
2201
2202impl<T> Foo<T> {
2203    fn qux(&self) {
2204        match self {
2205            Self::Bar(${1:_}) => ${2:todo!()},
2206            Self::Baz => ${3:todo!()},$0
2207        }
2208    }
2209}
2210            "#,
2211        );
2212        check_assist_with_config(
2213            add_missing_match_arms,
2214            AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
2215            r#"
2216enum Foo<T> {
2217    Bar(T),
2218    Baz,
2219}
2220
2221impl<T> Foo<T> {
2222    fn qux(v: Foo<i32>) {
2223        match v {
2224            $0_ => {}
2225        }
2226    }
2227}
2228            "#,
2229            r#"
2230enum Foo<T> {
2231    Bar(T),
2232    Baz,
2233}
2234
2235impl<T> Foo<T> {
2236    fn qux(v: Foo<i32>) {
2237        match v {
2238            Foo::Bar(${1:_}) => ${2:todo!()},
2239            Foo::Baz => ${3:todo!()},$0
2240        }
2241    }
2242}
2243            "#,
2244        );
2245    }
2246}