@@ -5,7 +5,7 @@ use hir::{
5
5
} ;
6
6
use ide_db:: {
7
7
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 ,
9
9
} ;
10
10
use stdx:: format_to;
11
11
use syntax:: {
@@ -17,51 +17,6 @@ use text_edit::TextEdit;
17
17
18
18
use crate :: { fix, Diagnostic , DiagnosticsContext } ;
19
19
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
-
65
20
// Diagnostic: missing-fields
66
21
//
67
22
// 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
104
59
let root = ctx. sema . db . parse_or_expand ( d. file ) ?;
105
60
106
61
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 ( ) ) ,
109
64
} ;
110
65
111
66
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
166
121
let expr = ( || -> Option < ast:: Expr > {
167
122
let item_in_ns = hir:: ItemInNs :: from ( hir:: ModuleDef :: from ( ty. as_adt ( ) ?) ) ;
168
123
169
- let type_path = current_module. find_use_path (
124
+ let type_path = current_module? . find_use_path (
170
125
ctx. sema . db ,
171
126
item_for_path_search ( ctx. sema . db , item_in_ns) ?,
172
127
) ?;
0 commit comments