]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
Merge #10902
[rust.git] / crates / ide_assists / src / handlers / extract_struct_from_enum_variant.rs
1 use std::iter;
2
3 use either::Either;
4 use hir::{Module, ModuleDef, Name, Variant};
5 use ide_db::{
6     defs::Definition,
7     helpers::{
8         insert_use::{insert_use, ImportScope, InsertUseConfig},
9         mod_path_to_ast,
10     },
11     search::FileReference,
12     RootDatabase,
13 };
14 use itertools::Itertools;
15 use rustc_hash::FxHashSet;
16 use syntax::{
17     ast::{
18         self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasAttrs, HasGenericParams,
19         HasName, HasTypeBounds, HasVisibility,
20     },
21     match_ast,
22     ted::{self, Position},
23     SyntaxKind::*,
24     SyntaxNode, T,
25 };
26
27 use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
28
29 // Assist: extract_struct_from_enum_variant
30 //
31 // Extracts a struct from enum variant.
32 //
33 // ```
34 // enum A { $0One(u32, u32) }
35 // ```
36 // ->
37 // ```
38 // struct One(u32, u32);
39 //
40 // enum A { One(One) }
41 // ```
42 pub(crate) fn extract_struct_from_enum_variant(
43     acc: &mut Assists,
44     ctx: &AssistContext,
45 ) -> Option<()> {
46     let variant = ctx.find_node_at_offset::<ast::Variant>()?;
47     let field_list = extract_field_list_if_applicable(&variant)?;
48
49     let variant_name = variant.name()?;
50     let variant_hir = ctx.sema.to_def(&variant)?;
51     if existing_definition(ctx.db(), &variant_name, &variant_hir) {
52         cov_mark::hit!(test_extract_enum_not_applicable_if_struct_exists);
53         return None;
54     }
55
56     let enum_ast = variant.parent_enum();
57     let enum_hir = ctx.sema.to_def(&enum_ast)?;
58     let target = variant.syntax().text_range();
59     acc.add(
60         AssistId("extract_struct_from_enum_variant", AssistKind::RefactorRewrite),
61         "Extract struct from enum variant",
62         target,
63         |builder| {
64             let variant_hir_name = variant_hir.name(ctx.db());
65             let enum_module_def = ModuleDef::from(enum_hir);
66             let usages = Definition::Variant(variant_hir).usages(&ctx.sema).all();
67
68             let mut visited_modules_set = FxHashSet::default();
69             let current_module = enum_hir.module(ctx.db());
70             visited_modules_set.insert(current_module);
71             // record file references of the file the def resides in, we only want to swap to the edited file in the builder once
72             let mut def_file_references = None;
73             for (file_id, references) in usages {
74                 if file_id == ctx.file_id() {
75                     def_file_references = Some(references);
76                     continue;
77                 }
78                 builder.edit_file(file_id);
79                 let processed = process_references(
80                     ctx,
81                     builder,
82                     &mut visited_modules_set,
83                     &enum_module_def,
84                     &variant_hir_name,
85                     references,
86                 );
87                 processed.into_iter().for_each(|(path, node, import)| {
88                     apply_references(ctx.config.insert_use, path, node, import)
89                 });
90             }
91             builder.edit_file(ctx.file_id());
92
93             let variant = builder.make_mut(variant.clone());
94             if let Some(references) = def_file_references {
95                 let processed = process_references(
96                     ctx,
97                     builder,
98                     &mut visited_modules_set,
99                     &enum_module_def,
100                     &variant_hir_name,
101                     references,
102                 );
103                 processed.into_iter().for_each(|(path, node, import)| {
104                     apply_references(ctx.config.insert_use, path, node, import)
105                 });
106             }
107
108             let indent = enum_ast.indent_level();
109             let def = create_struct_def(variant_name.clone(), &variant, &field_list, &enum_ast);
110             def.reindent_to(indent);
111
112             let start_offset = &variant.parent_enum().syntax().clone();
113             ted::insert_all_raw(
114                 ted::Position::before(start_offset),
115                 vec![
116                     def.syntax().clone().into(),
117                     make::tokens::whitespace(&format!("\n\n{}", indent)).into(),
118                 ],
119             );
120
121             update_variant(&variant, enum_ast.generic_param_list());
122         },
123     )
124 }
125
126 fn extract_field_list_if_applicable(
127     variant: &ast::Variant,
128 ) -> Option<Either<ast::RecordFieldList, ast::TupleFieldList>> {
129     match variant.kind() {
130         ast::StructKind::Record(field_list) if field_list.fields().next().is_some() => {
131             Some(Either::Left(field_list))
132         }
133         ast::StructKind::Tuple(field_list) if field_list.fields().count() > 1 => {
134             Some(Either::Right(field_list))
135         }
136         _ => None,
137     }
138 }
139
140 fn existing_definition(db: &RootDatabase, variant_name: &ast::Name, variant: &Variant) -> bool {
141     variant
142         .parent_enum(db)
143         .module(db)
144         .scope(db, None)
145         .into_iter()
146         .filter(|(_, def)| match def {
147             // only check type-namespace
148             hir::ScopeDef::ModuleDef(def) => matches!(
149                 def,
150                 ModuleDef::Module(_)
151                     | ModuleDef::Adt(_)
152                     | ModuleDef::Variant(_)
153                     | ModuleDef::Trait(_)
154                     | ModuleDef::TypeAlias(_)
155                     | ModuleDef::BuiltinType(_)
156             ),
157             _ => false,
158         })
159         .any(|(name, _)| name.to_string() == variant_name.to_string())
160 }
161
162 fn create_struct_def(
163     variant_name: ast::Name,
164     variant: &ast::Variant,
165     field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
166     enum_: &ast::Enum,
167 ) -> ast::Struct {
168     let enum_vis = enum_.visibility();
169
170     let insert_vis = |node: &'_ SyntaxNode, vis: &'_ SyntaxNode| {
171         let vis = vis.clone_for_update();
172         ted::insert(ted::Position::before(node), vis);
173     };
174
175     // for fields without any existing visibility, use visibility of enum
176     let field_list: ast::FieldList = match field_list {
177         Either::Left(field_list) => {
178             let field_list = field_list.clone_for_update();
179
180             if let Some(vis) = &enum_vis {
181                 field_list
182                     .fields()
183                     .filter(|field| field.visibility().is_none())
184                     .filter_map(|field| field.name())
185                     .for_each(|it| insert_vis(it.syntax(), vis.syntax()));
186             }
187
188             field_list.into()
189         }
190         Either::Right(field_list) => {
191             let field_list = field_list.clone_for_update();
192
193             if let Some(vis) = &enum_vis {
194                 field_list
195                     .fields()
196                     .filter(|field| field.visibility().is_none())
197                     .filter_map(|field| field.ty())
198                     .for_each(|it| insert_vis(it.syntax(), vis.syntax()));
199             }
200
201             field_list.into()
202         }
203     };
204
205     field_list.reindent_to(IndentLevel::single());
206
207     // FIXME: This uses all the generic params of the enum, but the variant might not use all of them.
208     let strukt = make::struct_(enum_vis, variant_name, enum_.generic_param_list(), field_list)
209         .clone_for_update();
210
211     // FIXME: Consider making this an actual function somewhere (like in `AttrsOwnerEdit`) after some deliberation
212     let attrs_and_docs = |node: &SyntaxNode| {
213         let mut select_next_ws = false;
214         node.children_with_tokens().filter(move |child| {
215             let accept = match child.kind() {
216                 ATTR | COMMENT => {
217                     select_next_ws = true;
218                     return true;
219                 }
220                 WHITESPACE if select_next_ws => true,
221                 _ => false,
222             };
223             select_next_ws = false;
224
225             accept
226         })
227     };
228
229     // copy attributes & comments from variant
230     let variant_attrs = attrs_and_docs(variant.syntax())
231         .map(|tok| match tok.kind() {
232             WHITESPACE => make::tokens::single_newline().into(),
233             _ => tok,
234         })
235         .collect();
236     ted::insert_all(Position::first_child_of(strukt.syntax()), variant_attrs);
237
238     // copy attributes from enum
239     ted::insert_all(
240         Position::first_child_of(strukt.syntax()),
241         enum_.attrs().map(|it| it.syntax().clone_for_update().into()).collect(),
242     );
243     strukt
244 }
245
246 fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>) -> Option<()> {
247     let name = variant.name()?;
248     let ty = match generic {
249         // FIXME: This uses all the generic params of the enum, but the variant might not use all of them.
250         Some(gpl) => {
251             let gpl = gpl.clone_for_update();
252             gpl.generic_params().for_each(|gp| {
253                 let tbl = match gp {
254                     ast::GenericParam::LifetimeParam(it) => it.type_bound_list(),
255                     ast::GenericParam::TypeParam(it) => it.type_bound_list(),
256                     ast::GenericParam::ConstParam(_) => return,
257                 };
258                 if let Some(tbl) = tbl {
259                     tbl.remove();
260                 }
261             });
262             make::ty(&format!("{}<{}>", name.text(), gpl.generic_params().join(", ")))
263         }
264         None => make::ty(&name.text()),
265     };
266     let tuple_field = make::tuple_field(None, ty);
267     let replacement = make::variant(
268         name,
269         Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))),
270     )
271     .clone_for_update();
272     ted::replace(variant.syntax(), replacement.syntax());
273     Some(())
274 }
275
276 fn apply_references(
277     insert_use_cfg: InsertUseConfig,
278     segment: ast::PathSegment,
279     node: SyntaxNode,
280     import: Option<(ImportScope, hir::ModPath)>,
281 ) {
282     if let Some((scope, path)) = import {
283         insert_use(&scope, mod_path_to_ast(&path), &insert_use_cfg);
284     }
285     // deep clone to prevent cycle
286     let path = make::path_from_segments(iter::once(segment.clone_subtree()), false);
287     ted::insert_raw(ted::Position::before(segment.syntax()), path.clone_for_update().syntax());
288     ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
289     ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
290 }
291
292 fn process_references(
293     ctx: &AssistContext,
294     builder: &mut AssistBuilder,
295     visited_modules: &mut FxHashSet<Module>,
296     enum_module_def: &ModuleDef,
297     variant_hir_name: &Name,
298     refs: Vec<FileReference>,
299 ) -> Vec<(ast::PathSegment, SyntaxNode, Option<(ImportScope, hir::ModPath)>)> {
300     // we have to recollect here eagerly as we are about to edit the tree we need to calculate the changes
301     // and corresponding nodes up front
302     refs.into_iter()
303         .flat_map(|reference| {
304             let (segment, scope_node, module) = reference_to_node(&ctx.sema, reference)?;
305             let segment = builder.make_mut(segment);
306             let scope_node = builder.make_syntax_mut(scope_node);
307             if !visited_modules.contains(&module) {
308                 let mod_path = module.find_use_path_prefixed(
309                     ctx.sema.db,
310                     *enum_module_def,
311                     ctx.config.insert_use.prefix_kind,
312                 );
313                 if let Some(mut mod_path) = mod_path {
314                     mod_path.pop_segment();
315                     mod_path.push_segment(variant_hir_name.clone());
316                     let scope = ImportScope::find_insert_use_container(&scope_node, &ctx.sema)?;
317                     visited_modules.insert(module);
318                     return Some((segment, scope_node, Some((scope, mod_path))));
319                 }
320             }
321             Some((segment, scope_node, None))
322         })
323         .collect()
324 }
325
326 fn reference_to_node(
327     sema: &hir::Semantics<RootDatabase>,
328     reference: FileReference,
329 ) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> {
330     let segment =
331         reference.name.as_name_ref()?.syntax().parent().and_then(ast::PathSegment::cast)?;
332     let parent = segment.parent_path().syntax().parent()?;
333     let expr_or_pat = match_ast! {
334         match parent {
335             ast::PathExpr(_it) => parent.parent()?,
336             ast::RecordExpr(_it) => parent,
337             ast::TupleStructPat(_it) => parent,
338             ast::RecordPat(_it) => parent,
339             _ => return None,
340         }
341     };
342     let module = sema.scope(&expr_or_pat).module()?;
343     Some((segment, expr_or_pat, module))
344 }
345
346 #[cfg(test)]
347 mod tests {
348     use crate::tests::{check_assist, check_assist_not_applicable};
349
350     use super::*;
351
352     #[test]
353     fn test_extract_struct_several_fields_tuple() {
354         check_assist(
355             extract_struct_from_enum_variant,
356             "enum A { $0One(u32, u32) }",
357             r#"struct One(u32, u32);
358
359 enum A { One(One) }"#,
360         );
361     }
362
363     #[test]
364     fn test_extract_struct_several_fields_named() {
365         check_assist(
366             extract_struct_from_enum_variant,
367             "enum A { $0One { foo: u32, bar: u32 } }",
368             r#"struct One{ foo: u32, bar: u32 }
369
370 enum A { One(One) }"#,
371         );
372     }
373
374     #[test]
375     fn test_extract_struct_one_field_named() {
376         check_assist(
377             extract_struct_from_enum_variant,
378             "enum A { $0One { foo: u32 } }",
379             r#"struct One{ foo: u32 }
380
381 enum A { One(One) }"#,
382         );
383     }
384
385     #[test]
386     fn test_extract_struct_carries_over_generics() {
387         check_assist(
388             extract_struct_from_enum_variant,
389             r"enum En<T> { Var { a: T$0 } }",
390             r#"struct Var<T>{ a: T }
391
392 enum En<T> { Var(Var<T>) }"#,
393         );
394     }
395
396     #[test]
397     fn test_extract_struct_carries_over_attributes() {
398         check_assist(
399             extract_struct_from_enum_variant,
400             r#"#[derive(Debug)]
401 #[derive(Clone)]
402 enum Enum { Variant{ field: u32$0 } }"#,
403             r#"#[derive(Debug)]#[derive(Clone)] struct Variant{ field: u32 }
404
405 #[derive(Debug)]
406 #[derive(Clone)]
407 enum Enum { Variant(Variant) }"#,
408         );
409     }
410
411     #[test]
412     fn test_extract_struct_indent_to_parent_enum() {
413         check_assist(
414             extract_struct_from_enum_variant,
415             r#"
416 enum Enum {
417     Variant {
418         field: u32$0
419     }
420 }"#,
421             r#"
422 struct Variant{
423     field: u32
424 }
425
426 enum Enum {
427     Variant(Variant)
428 }"#,
429         );
430     }
431
432     #[test]
433     fn test_extract_struct_indent_to_parent_enum_in_mod() {
434         check_assist(
435             extract_struct_from_enum_variant,
436             r#"
437 mod indenting {
438     enum Enum {
439         Variant {
440             field: u32$0
441         }
442     }
443 }"#,
444             r#"
445 mod indenting {
446     struct Variant{
447         field: u32
448     }
449
450     enum Enum {
451         Variant(Variant)
452     }
453 }"#,
454         );
455     }
456
457     #[test]
458     fn test_extract_struct_keep_comments_and_attrs_one_field_named() {
459         check_assist(
460             extract_struct_from_enum_variant,
461             r#"
462 enum A {
463     $0One {
464         // leading comment
465         /// doc comment
466         #[an_attr]
467         foo: u32
468         // trailing comment
469     }
470 }"#,
471             r#"
472 struct One{
473     // leading comment
474     /// doc comment
475     #[an_attr]
476     foo: u32
477     // trailing comment
478 }
479
480 enum A {
481     One(One)
482 }"#,
483         );
484     }
485
486     #[test]
487     fn test_extract_struct_keep_comments_and_attrs_several_fields_named() {
488         check_assist(
489             extract_struct_from_enum_variant,
490             r#"
491 enum A {
492     $0One {
493         // comment
494         /// doc
495         #[attr]
496         foo: u32,
497         // comment
498         #[attr]
499         /// doc
500         bar: u32
501     }
502 }"#,
503             r#"
504 struct One{
505     // comment
506     /// doc
507     #[attr]
508     foo: u32,
509     // comment
510     #[attr]
511     /// doc
512     bar: u32
513 }
514
515 enum A {
516     One(One)
517 }"#,
518         );
519     }
520
521     #[test]
522     fn test_extract_struct_keep_comments_and_attrs_several_fields_tuple() {
523         check_assist(
524             extract_struct_from_enum_variant,
525             "enum A { $0One(/* comment */ #[attr] u32, /* another */ u32 /* tail */) }",
526             r#"
527 struct One(/* comment */ #[attr] u32, /* another */ u32 /* tail */);
528
529 enum A { One(One) }"#,
530         );
531     }
532
533     #[test]
534     fn test_extract_struct_keep_comments_and_attrs_on_variant_struct() {
535         check_assist(
536             extract_struct_from_enum_variant,
537             r#"
538 enum A {
539     /* comment */
540     // other
541     /// comment
542     #[attr]
543     $0One {
544         a: u32
545     }
546 }"#,
547             r#"
548 /* comment */
549 // other
550 /// comment
551 #[attr]
552 struct One{
553     a: u32
554 }
555
556 enum A {
557     One(One)
558 }"#,
559         );
560     }
561
562     #[test]
563     fn test_extract_struct_keep_comments_and_attrs_on_variant_tuple() {
564         check_assist(
565             extract_struct_from_enum_variant,
566             r#"
567 enum A {
568     /* comment */
569     // other
570     /// comment
571     #[attr]
572     $0One(u32, u32)
573 }"#,
574             r#"
575 /* comment */
576 // other
577 /// comment
578 #[attr]
579 struct One(u32, u32);
580
581 enum A {
582     One(One)
583 }"#,
584         );
585     }
586
587     #[test]
588     fn test_extract_struct_keep_existing_visibility_named() {
589         check_assist(
590             extract_struct_from_enum_variant,
591             "enum A { $0One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 } }",
592             r#"
593 struct One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 }
594
595 enum A { One(One) }"#,
596         );
597     }
598
599     #[test]
600     fn test_extract_struct_keep_existing_visibility_tuple() {
601         check_assist(
602             extract_struct_from_enum_variant,
603             "enum A { $0One(u32, pub(crate) u32, pub(super) u32, u32) }",
604             r#"
605 struct One(u32, pub(crate) u32, pub(super) u32, u32);
606
607 enum A { One(One) }"#,
608         );
609     }
610
611     #[test]
612     fn test_extract_enum_variant_name_value_namespace() {
613         check_assist(
614             extract_struct_from_enum_variant,
615             r#"const One: () = ();
616 enum A { $0One(u32, u32) }"#,
617             r#"const One: () = ();
618 struct One(u32, u32);
619
620 enum A { One(One) }"#,
621         );
622     }
623
624     #[test]
625     fn test_extract_struct_no_visibility() {
626         check_assist(
627             extract_struct_from_enum_variant,
628             "enum A { $0One(u32, u32) }",
629             r#"
630 struct One(u32, u32);
631
632 enum A { One(One) }"#,
633         );
634     }
635
636     #[test]
637     fn test_extract_struct_pub_visibility() {
638         check_assist(
639             extract_struct_from_enum_variant,
640             "pub enum A { $0One(u32, u32) }",
641             r#"
642 pub struct One(pub u32, pub u32);
643
644 pub enum A { One(One) }"#,
645         );
646     }
647
648     #[test]
649     fn test_extract_struct_pub_in_mod_visibility() {
650         check_assist(
651             extract_struct_from_enum_variant,
652             "pub(in something) enum A { $0One{ a: u32, b: u32 } }",
653             r#"
654 pub(in something) struct One{ pub(in something) a: u32, pub(in something) b: u32 }
655
656 pub(in something) enum A { One(One) }"#,
657         );
658     }
659
660     #[test]
661     fn test_extract_struct_pub_crate_visibility() {
662         check_assist(
663             extract_struct_from_enum_variant,
664             "pub(crate) enum A { $0One{ a: u32, b: u32, c: u32 } }",
665             r#"
666 pub(crate) struct One{ pub(crate) a: u32, pub(crate) b: u32, pub(crate) c: u32 }
667
668 pub(crate) enum A { One(One) }"#,
669         );
670     }
671
672     #[test]
673     fn test_extract_struct_with_complex_imports() {
674         check_assist(
675             extract_struct_from_enum_variant,
676             r#"mod my_mod {
677     fn another_fn() {
678         let m = my_other_mod::MyEnum::MyField(1, 1);
679     }
680
681     pub mod my_other_mod {
682         fn another_fn() {
683             let m = MyEnum::MyField(1, 1);
684         }
685
686         pub enum MyEnum {
687             $0MyField(u8, u8),
688         }
689     }
690 }
691
692 fn another_fn() {
693     let m = my_mod::my_other_mod::MyEnum::MyField(1, 1);
694 }"#,
695             r#"use my_mod::my_other_mod::MyField;
696
697 mod my_mod {
698     use self::my_other_mod::MyField;
699
700     fn another_fn() {
701         let m = my_other_mod::MyEnum::MyField(MyField(1, 1));
702     }
703
704     pub mod my_other_mod {
705         fn another_fn() {
706             let m = MyEnum::MyField(MyField(1, 1));
707         }
708
709         pub struct MyField(pub u8, pub u8);
710
711         pub enum MyEnum {
712             MyField(MyField),
713         }
714     }
715 }
716
717 fn another_fn() {
718     let m = my_mod::my_other_mod::MyEnum::MyField(MyField(1, 1));
719 }"#,
720         );
721     }
722
723     #[test]
724     fn extract_record_fix_references() {
725         check_assist(
726             extract_struct_from_enum_variant,
727             r#"
728 enum E {
729     $0V { i: i32, j: i32 }
730 }
731
732 fn f() {
733     let E::V { i, j } = E::V { i: 9, j: 2 };
734 }
735 "#,
736             r#"
737 struct V{ i: i32, j: i32 }
738
739 enum E {
740     V(V)
741 }
742
743 fn f() {
744     let E::V(V { i, j }) = E::V(V { i: 9, j: 2 });
745 }
746 "#,
747         )
748     }
749
750     #[test]
751     fn extract_record_fix_references2() {
752         check_assist(
753             extract_struct_from_enum_variant,
754             r#"
755 enum E {
756     $0V(i32, i32)
757 }
758
759 fn f() {
760     let E::V(i, j) = E::V(9, 2);
761 }
762 "#,
763             r#"
764 struct V(i32, i32);
765
766 enum E {
767     V(V)
768 }
769
770 fn f() {
771     let E::V(V(i, j)) = E::V(V(9, 2));
772 }
773 "#,
774         )
775     }
776
777     #[test]
778     fn test_several_files() {
779         check_assist(
780             extract_struct_from_enum_variant,
781             r#"
782 //- /main.rs
783 enum E {
784     $0V(i32, i32)
785 }
786 mod foo;
787
788 //- /foo.rs
789 use crate::E;
790 fn f() {
791     let e = E::V(9, 2);
792 }
793 "#,
794             r#"
795 //- /main.rs
796 struct V(i32, i32);
797
798 enum E {
799     V(V)
800 }
801 mod foo;
802
803 //- /foo.rs
804 use crate::{E, V};
805 fn f() {
806     let e = E::V(V(9, 2));
807 }
808 "#,
809         )
810     }
811
812     #[test]
813     fn test_several_files_record() {
814         check_assist(
815             extract_struct_from_enum_variant,
816             r#"
817 //- /main.rs
818 enum E {
819     $0V { i: i32, j: i32 }
820 }
821 mod foo;
822
823 //- /foo.rs
824 use crate::E;
825 fn f() {
826     let e = E::V { i: 9, j: 2 };
827 }
828 "#,
829             r#"
830 //- /main.rs
831 struct V{ i: i32, j: i32 }
832
833 enum E {
834     V(V)
835 }
836 mod foo;
837
838 //- /foo.rs
839 use crate::{E, V};
840 fn f() {
841     let e = E::V(V { i: 9, j: 2 });
842 }
843 "#,
844         )
845     }
846
847     #[test]
848     fn test_extract_struct_record_nested_call_exp() {
849         check_assist(
850             extract_struct_from_enum_variant,
851             r#"
852 enum A { $0One { a: u32, b: u32 } }
853
854 struct B(A);
855
856 fn foo() {
857     let _ = B(A::One { a: 1, b: 2 });
858 }
859 "#,
860             r#"
861 struct One{ a: u32, b: u32 }
862
863 enum A { One(One) }
864
865 struct B(A);
866
867 fn foo() {
868     let _ = B(A::One(One { a: 1, b: 2 }));
869 }
870 "#,
871         );
872     }
873
874     #[test]
875     fn test_extract_enum_not_applicable_for_element_with_no_fields() {
876         check_assist_not_applicable(extract_struct_from_enum_variant, r#"enum A { $0One }"#);
877     }
878
879     #[test]
880     fn test_extract_enum_not_applicable_if_struct_exists() {
881         cov_mark::check!(test_extract_enum_not_applicable_if_struct_exists);
882         check_assist_not_applicable(
883             extract_struct_from_enum_variant,
884             r#"
885 struct One;
886 enum A { $0One(u8, u32) }
887 "#,
888         );
889     }
890
891     #[test]
892     fn test_extract_not_applicable_one_field() {
893         check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0One(u32) }");
894     }
895
896     #[test]
897     fn test_extract_not_applicable_no_field_tuple() {
898         check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None() }");
899     }
900
901     #[test]
902     fn test_extract_not_applicable_no_field_named() {
903         check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None {} }");
904     }
905 }