@@ -5,7 +5,7 @@ use hir::{
55} ;
66use ide_db:: {
77 assists:: Assist , famous_defs:: FamousDefs , imports:: import_assets:: item_for_path_search,
8- source_change:: SourceChange , FxHashMap ,
8+ source_change:: SourceChange , use_trivial_contructor :: use_trivial_constructor , FxHashMap ,
99} ;
1010use stdx:: format_to;
1111use syntax:: {
@@ -17,51 +17,6 @@ use text_edit::TextEdit;
1717
1818use crate :: { fix, Diagnostic , DiagnosticsContext } ;
1919
20- // FIXME: how to depupicate with `ide-assists/generate_new`
21- fn use_trivial_constructor (
22- db : & ide_db:: RootDatabase ,
23- path : ast:: Path ,
24- ty : & hir:: Type ,
25- ) -> Option < ast:: Expr > {
26- match ty. as_adt ( ) {
27- Some ( hir:: Adt :: Enum ( x) ) => {
28- let variants = x. variants ( db) ;
29-
30- if variants. len ( ) == 1 {
31- let variant = variants[ 0 ] ;
32-
33- if variant. fields ( db) . is_empty ( ) {
34- let path = ast:: make:: path_qualified (
35- path,
36- syntax:: ast:: make:: path_segment ( ast:: make:: name_ref (
37- & variant. name ( db) . to_smol_str ( ) ,
38- ) ) ,
39- ) ;
40-
41- let is_record = variant. kind ( db) == hir:: StructKind :: Record ;
42-
43- return Some ( if is_record {
44- ast:: Expr :: RecordExpr ( syntax:: ast:: make:: record_expr (
45- path,
46- ast:: make:: record_expr_field_list ( std:: iter:: empty ( ) ) ,
47- ) )
48- } else {
49- syntax:: ast:: make:: expr_path ( path)
50- } ) ;
51- }
52- }
53- }
54- Some ( hir:: Adt :: Struct ( x) ) => {
55- if x. fields ( db) . is_empty ( ) {
56- return Some ( syntax:: ast:: make:: expr_path ( path) ) ;
57- }
58- }
59- _ => { }
60- }
61-
62- None
63- }
64-
6520// Diagnostic: missing-fields
6621//
6722// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
@@ -104,8 +59,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
10459 let root = ctx. sema . db . parse_or_expand ( d. file ) ?;
10560
10661 let current_module = match & d. field_list_parent {
107- Either :: Left ( ptr) => ctx. sema . scope ( ptr. to_node ( & root) . syntax ( ) ) . unwrap ( ) . module ( ) ,
108- Either :: Right ( ptr) => ctx. sema . scope ( ptr. to_node ( & root) . syntax ( ) ) . unwrap ( ) . module ( ) ,
62+ Either :: Left ( ptr) => ctx. sema . scope ( ptr. to_node ( & root) . syntax ( ) ) . map ( |it| it . module ( ) ) ,
63+ Either :: Right ( ptr) => ctx. sema . scope ( ptr. to_node ( & root) . syntax ( ) ) . map ( |it| it . module ( ) ) ,
10964 } ;
11065
11166 let build_text_edit = |parent_syntax, new_syntax : & SyntaxNode , old_syntax| {
@@ -166,7 +121,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
166121 let expr = ( || -> Option < ast:: Expr > {
167122 let item_in_ns = hir:: ItemInNs :: from ( hir:: ModuleDef :: from ( ty. as_adt ( ) ?) ) ;
168123
169- let type_path = current_module. find_use_path (
124+ let type_path = current_module? . find_use_path (
170125 ctx. sema . db ,
171126 item_for_path_search ( ctx. sema . db , item_in_ns) ?,
172127 ) ?;
0 commit comments