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