1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
use either::Either;
use hir::{db::AstDatabase, InFile};
use ide_assists::Assist;
use ide_db::source_change::SourceChange;
use stdx::format_to;
use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
use text_edit::TextEdit;
use crate::{fix, Diagnostic, DiagnosticsContext};
// Diagnostic: missing-fields
//
// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
//
// Example:
//
// ```rust
// struct A { a: u8, b: u8 }
//
// let a = A { a: 10 };
// ```
pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
let mut message = String::from("Missing structure fields:\n");
for field in &d.missed_fields {
format_to!(message, "- {}\n", field);
}
let ptr = InFile::new(
d.file,
d.field_list_parent_path
.clone()
.map(SyntaxNodePtr::from)
.unwrap_or_else(|| d.field_list_parent.clone().either(|it| it.into(), |it| it.into())),
);
Diagnostic::new("missing-fields", message, ctx.sema.diagnostics_display_range(ptr).range)
.with_fixes(fixes(ctx, d))
}
fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> {
// Note that although we could add a diagnostics to
// fill the missing tuple field, e.g :
// `struct A(usize);`
// `let a = A { 0: () }`
// but it is uncommon usage and it should not be encouraged.
if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
return None;
}
let root = ctx.sema.db.parse_or_expand(d.file)?;
let field_list_parent = match &d.field_list_parent {
Either::Left(record_expr) => record_expr.to_node(&root),
// FIXE: patterns should be fixable as well.
Either::Right(_) => return None,
};
let old_field_list = field_list_parent.record_expr_field_list()?;
let new_field_list = old_field_list.clone_for_update();
for f in d.missed_fields.iter() {
let field =
make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
.clone_for_update();
new_field_list.add_field(field);
}
let edit = {
let mut builder = TextEdit::builder();
algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
builder.finish()
};
Some(vec![fix(
"fill_missing_fields",
"Fill struct fields",
SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
ctx.sema.original_range(field_list_parent.syntax()).range,
)])
}
#[cfg(test)]
mod tests {
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn missing_record_pat_field_diagnostic() {
check_diagnostics(
r#"
struct S { foo: i32, bar: () }
fn baz(s: S) {
let S { foo: _ } = s;
//^ Missing structure fields:
//| - bar
}
"#,
);
}
#[test]
fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
check_diagnostics(
r"
struct S { foo: i32, bar: () }
fn baz(s: S) -> i32 {
match s {
S { foo, .. } => foo,
}
}
",
)
}
#[test]
fn missing_record_pat_field_box() {
check_diagnostics(
r"
struct S { s: Box<u32> }
fn x(a: S) {
let S { box s } = a;
}
",
)
}
#[test]
fn missing_record_pat_field_ref() {
check_diagnostics(
r"
struct S { s: u32 }
fn x(a: S) {
let S { ref s } = a;
}
",
)
}
#[test]
fn range_mapping_out_of_macros() {
// FIXME: this is very wrong, but somewhat tricky to fix.
check_fix(
r#"
fn some() {}
fn items() {}
fn here() {}
macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
fn main() {
let _x = id![Foo { a: $042 }];
}
pub struct Foo { pub a: i32, pub b: i32 }
"#,
r#"
fn some(, b: () ) {}
fn items() {}
fn here() {}
macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
fn main() {
let _x = id![Foo { a: 42 }];
}
pub struct Foo { pub a: i32, pub b: i32 }
"#,
);
}
#[test]
fn test_fill_struct_fields_empty() {
check_fix(
r#"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let s = TestStruct {$0};
}
"#,
r#"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let s = TestStruct { one: (), two: () };
}
"#,
);
}
#[test]
fn test_fill_struct_fields_self() {
check_fix(
r#"
struct TestStruct { one: i32 }
impl TestStruct {
fn test_fn() { let s = Self {$0}; }
}
"#,
r#"
struct TestStruct { one: i32 }
impl TestStruct {
fn test_fn() { let s = Self { one: () }; }
}
"#,
);
}
#[test]
fn test_fill_struct_fields_enum() {
check_fix(
r#"
enum Expr {
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
}
impl Expr {
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
Expr::Bin {$0 }
}
}
"#,
r#"
enum Expr {
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
}
impl Expr {
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
Expr::Bin { lhs: (), rhs: () }
}
}
"#,
);
}
#[test]
fn test_fill_struct_fields_partial() {
check_fix(
r#"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let s = TestStruct{ two: 2$0 };
}
"#,
r"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let s = TestStruct{ two: 2, one: () };
}
",
);
}
#[test]
fn test_fill_struct_fields_raw_ident() {
check_fix(
r#"
struct TestStruct { r#type: u8 }
fn test_fn() {
TestStruct { $0 };
}
"#,
r"
struct TestStruct { r#type: u8 }
fn test_fn() {
TestStruct { r#type: () };
}
",
);
}
#[test]
fn test_fill_struct_fields_no_diagnostic() {
check_diagnostics(
r#"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let one = 1;
let s = TestStruct{ one, two: 2 };
}
"#,
);
}
#[test]
fn test_fill_struct_fields_no_diagnostic_on_spread() {
check_diagnostics(
r#"
struct TestStruct { one: i32, two: i64 }
fn test_fn() {
let one = 1;
let s = TestStruct{ ..a };
}
"#,
);
}
#[test]
fn test_fill_struct_fields_blank_line() {
check_fix(
r#"
struct S { a: (), b: () }
fn f() {
S {
$0
};
}
"#,
r#"
struct S { a: (), b: () }
fn f() {
S {
a: (),
b: (),
};
}
"#,
);
}
}
|