aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir/src/diagnostics.rs43
-rw-r--r--crates/hir/src/lib.rs33
-rw-r--r--crates/ide/src/diagnostics.rs7
-rw-r--r--crates/ide/src/diagnostics/fixes.rs1
-rw-r--r--crates/ide/src/diagnostics/missing_fields.rs (renamed from crates/ide/src/diagnostics/fixes/fill_missing_fields.rs)118
-rw-r--r--crates/ide/src/diagnostics/unresolved_module.rs5
6 files changed, 98 insertions, 109 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 5e2f94698..a5e982b75 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -16,7 +16,7 @@ pub use crate::diagnostics_sink::{
16}; 16};
17 17
18macro_rules! diagnostics { 18macro_rules! diagnostics {
19 ($($diag:ident)*) => { 19 ($($diag:ident),*) => {
20 pub enum AnyDiagnostic {$( 20 pub enum AnyDiagnostic {$(
21 $diag(Box<$diag>), 21 $diag(Box<$diag>),
22 )*} 22 )*}
@@ -31,7 +31,7 @@ macro_rules! diagnostics {
31 }; 31 };
32} 32}
33 33
34diagnostics![UnresolvedModule]; 34diagnostics![UnresolvedModule, MissingFields];
35 35
36#[derive(Debug)] 36#[derive(Debug)]
37pub struct UnresolvedModule { 37pub struct UnresolvedModule {
@@ -321,17 +321,6 @@ impl Diagnostic for MissingUnsafe {
321 } 321 }
322} 322}
323 323
324// Diagnostic: missing-structure-fields
325//
326// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
327//
328// Example:
329//
330// ```rust
331// struct A { a: u8, b: u8 }
332//
333// let a = A { a: 10 };
334// ```
335#[derive(Debug)] 324#[derive(Debug)]
336pub struct MissingFields { 325pub struct MissingFields {
337 pub file: HirFileId, 326 pub file: HirFileId,
@@ -340,34 +329,6 @@ pub struct MissingFields {
340 pub missed_fields: Vec<Name>, 329 pub missed_fields: Vec<Name>,
341} 330}
342 331
343impl Diagnostic for MissingFields {
344 fn code(&self) -> DiagnosticCode {
345 DiagnosticCode("missing-structure-fields")
346 }
347 fn message(&self) -> String {
348 let mut buf = String::from("Missing structure fields:\n");
349 for field in &self.missed_fields {
350 format_to!(buf, "- {}\n", field);
351 }
352 buf
353 }
354
355 fn display_source(&self) -> InFile<SyntaxNodePtr> {
356 InFile {
357 file_id: self.file,
358 value: self
359 .field_list_parent_path
360 .clone()
361 .map(SyntaxNodePtr::from)
362 .unwrap_or_else(|| self.field_list_parent.clone().into()),
363 }
364 }
365
366 fn as_any(&self) -> &(dyn Any + Send + 'static) {
367 self
368 }
369}
370
371// Diagnostic: missing-pat-fields 332// Diagnostic: missing-pat-fields
372// 333//
373// This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. 334// This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure.
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index ff6c68dbc..583d92f20 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -609,23 +609,21 @@ impl Module {
609 } 609 }
610 for decl in self.declarations(db) { 610 for decl in self.declarations(db) {
611 match decl { 611 match decl {
612 crate::ModuleDef::Function(f) => f.diagnostics(db, sink, internal_diagnostics), 612 ModuleDef::Function(f) => acc.extend(f.diagnostics(db, sink, internal_diagnostics)),
613 crate::ModuleDef::Module(m) => { 613 ModuleDef::Module(m) => {
614 // Only add diagnostics from inline modules 614 // Only add diagnostics from inline modules
615 if def_map[m.id.local_id].origin.is_inline() { 615 if def_map[m.id.local_id].origin.is_inline() {
616 acc.extend(m.diagnostics(db, sink, internal_diagnostics)) 616 acc.extend(m.diagnostics(db, sink, internal_diagnostics))
617 } 617 }
618 } 618 }
619 _ => { 619 _ => decl.diagnostics(db, sink),
620 decl.diagnostics(db, sink);
621 }
622 } 620 }
623 } 621 }
624 622
625 for impl_def in self.impl_defs(db) { 623 for impl_def in self.impl_defs(db) {
626 for item in impl_def.items(db) { 624 for item in impl_def.items(db) {
627 if let AssocItem::Function(f) = item { 625 if let AssocItem::Function(f) = item {
628 f.diagnostics(db, sink, internal_diagnostics); 626 acc.extend(f.diagnostics(db, sink, internal_diagnostics));
629 } 627 }
630 } 628 }
631 } 629 }
@@ -1033,7 +1031,8 @@ impl Function {
1033 db: &dyn HirDatabase, 1031 db: &dyn HirDatabase,
1034 sink: &mut DiagnosticSink, 1032 sink: &mut DiagnosticSink,
1035 internal_diagnostics: bool, 1033 internal_diagnostics: bool,
1036 ) { 1034 ) -> Vec<AnyDiagnostic> {
1035 let mut acc: Vec<AnyDiagnostic> = Vec::new();
1037 let krate = self.module(db).id.krate(); 1036 let krate = self.module(db).id.krate();
1038 1037
1039 let source_map = db.body_with_source_map(self.id.into()).1; 1038 let source_map = db.body_with_source_map(self.id.into()).1;
@@ -1114,14 +1113,17 @@ impl Function {
1114 .into_iter() 1113 .into_iter()
1115 .map(|idx| variant_data.fields()[idx].name.clone()) 1114 .map(|idx| variant_data.fields()[idx].name.clone())
1116 .collect(); 1115 .collect();
1117 sink.push(MissingFields { 1116 acc.push(
1118 file: source_ptr.file_id, 1117 MissingFields {
1119 field_list_parent: AstPtr::new(record_expr), 1118 file: source_ptr.file_id,
1120 field_list_parent_path: record_expr 1119 field_list_parent: AstPtr::new(record_expr),
1121 .path() 1120 field_list_parent_path: record_expr
1122 .map(|path| AstPtr::new(&path)), 1121 .path()
1123 missed_fields, 1122 .map(|path| AstPtr::new(&path)),
1124 }) 1123 missed_fields,
1124 }
1125 .into(),
1126 )
1125 } 1127 }
1126 } 1128 }
1127 } 1129 }
@@ -1234,6 +1236,7 @@ impl Function {
1234 for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) { 1236 for diag in hir_ty::diagnostics::validate_module_item(db, krate, self.id.into()) {
1235 sink.push(diag) 1237 sink.push(diag)
1236 } 1238 }
1239 acc
1237 } 1240 }
1238 1241
1239 /// Whether this function declaration has a definition. 1242 /// Whether this function declaration has a definition.
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 075aae8d5..ef8c8044c 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -5,6 +5,7 @@
5//! original files. So we need to map the ranges. 5//! original files. So we need to map the ranges.
6 6
7mod unresolved_module; 7mod unresolved_module;
8mod missing_fields;
8 9
9mod fixes; 10mod fixes;
10mod field_shorthand; 11mod field_shorthand;
@@ -123,9 +124,6 @@ pub(crate) fn diagnostics(
123 } 124 }
124 let res = RefCell::new(res); 125 let res = RefCell::new(res);
125 let sink_builder = DiagnosticSinkBuilder::new() 126 let sink_builder = DiagnosticSinkBuilder::new()
126 .on::<hir::diagnostics::MissingFields, _>(|d| {
127 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
128 })
129 .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| { 127 .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| {
130 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve)); 128 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
131 }) 129 })
@@ -232,7 +230,8 @@ pub(crate) fn diagnostics(
232 let ctx = DiagnosticsContext { config, sema, resolve }; 230 let ctx = DiagnosticsContext { config, sema, resolve };
233 for diag in diags { 231 for diag in diags {
234 let d = match diag { 232 let d = match diag {
235 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::render(&ctx, &d), 233 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
234 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
236 }; 235 };
237 if let Some(code) = d.code { 236 if let Some(code) = d.code {
238 if ctx.config.disabled.contains(code.as_str()) { 237 if ctx.config.disabled.contains(code.as_str()) {
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs
index 8640d7231..a2e792b3b 100644
--- a/crates/ide/src/diagnostics/fixes.rs
+++ b/crates/ide/src/diagnostics/fixes.rs
@@ -2,7 +2,6 @@
2//! The same module also has all curret custom fixes for the diagnostics implemented. 2//! The same module also has all curret custom fixes for the diagnostics implemented.
3mod change_case; 3mod change_case;
4mod create_field; 4mod create_field;
5mod fill_missing_fields;
6mod remove_semicolon; 5mod remove_semicolon;
7mod replace_with_find_map; 6mod replace_with_find_map;
8mod wrap_tail_expr; 7mod wrap_tail_expr;
diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs
index c76f6008a..aee780972 100644
--- a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
+++ b/crates/ide/src/diagnostics/missing_fields.rs
@@ -1,53 +1,77 @@
1use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics}; 1use hir::{db::AstDatabase, InFile};
2use ide_assists::AssistResolveStrategy; 2use ide_assists::Assist;
3use ide_db::{source_change::SourceChange, RootDatabase}; 3use ide_db::source_change::SourceChange;
4use syntax::{algo, ast::make, AstNode}; 4use stdx::format_to;
5use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
5use text_edit::TextEdit; 6use text_edit::TextEdit;
6 7
7use crate::{ 8use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
8 diagnostics::{fix, fixes::DiagnosticWithFixes}, 9
9 Assist, 10// Diagnostic: missing-structure-fields
10}; 11//
11 12// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
12impl DiagnosticWithFixes for MissingFields { 13//
13 fn fixes( 14// Example:
14 &self, 15//
15 sema: &Semantics<RootDatabase>, 16// ```rust
16 _resolve: &AssistResolveStrategy, 17// struct A { a: u8, b: u8 }
17 ) -> Option<Vec<Assist>> { 18//
18 // Note that although we could add a diagnostics to 19// let a = A { a: 10 };
19 // fill the missing tuple field, e.g : 20// ```
20 // `struct A(usize);` 21pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
21 // `let a = A { 0: () }` 22 let mut message = String::from("Missing structure fields:\n");
22 // but it is uncommon usage and it should not be encouraged. 23 for field in &d.missed_fields {
23 if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { 24 format_to!(message, "- {}\n", field);
24 return None;
25 }
26
27 let root = sema.db.parse_or_expand(self.file)?;
28 let field_list_parent = self.field_list_parent.to_node(&root);
29 let old_field_list = field_list_parent.record_expr_field_list()?;
30 let new_field_list = old_field_list.clone_for_update();
31 for f in self.missed_fields.iter() {
32 let field =
33 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
34 .clone_for_update();
35 new_field_list.add_field(field);
36 }
37
38 let edit = {
39 let mut builder = TextEdit::builder();
40 algo::diff(old_field_list.syntax(), new_field_list.syntax())
41 .into_text_edit(&mut builder);
42 builder.finish()
43 };
44 Some(vec![fix(
45 "fill_missing_fields",
46 "Fill struct fields",
47 SourceChange::from_text_edit(self.file.original_file(sema.db), edit),
48 sema.original_range(field_list_parent.syntax()).range,
49 )])
50 } 25 }
26
27 let ptr = InFile::new(
28 d.file,
29 d.field_list_parent_path
30 .clone()
31 .map(SyntaxNodePtr::from)
32 .unwrap_or_else(|| d.field_list_parent.clone().into()),
33 );
34
35 Diagnostic::new(
36 "missing-structure-fields",
37 message,
38 ctx.sema.diagnostics_display_range(ptr).range,
39 )
40 .with_fixes(fixes(ctx, d))
41}
42
43fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> {
44 // Note that although we could add a diagnostics to
45 // fill the missing tuple field, e.g :
46 // `struct A(usize);`
47 // `let a = A { 0: () }`
48 // but it is uncommon usage and it should not be encouraged.
49 if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
50 return None;
51 }
52
53 let root = ctx.sema.db.parse_or_expand(d.file)?;
54 let field_list_parent = d.field_list_parent.to_node(&root);
55 let old_field_list = field_list_parent.record_expr_field_list()?;
56 let new_field_list = old_field_list.clone_for_update();
57 for f in d.missed_fields.iter() {
58 let field =
59 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
60 .clone_for_update();
61 new_field_list.add_field(field);
62 }
63
64 let edit = {
65 let mut builder = TextEdit::builder();
66 algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
67 builder.finish()
68 };
69 Some(vec![fix(
70 "fill_missing_fields",
71 "Fill struct fields",
72 SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
73 ctx.sema.original_range(field_list_parent.syntax()).range,
74 )])
51} 75}
52 76
53#[cfg(test)] 77#[cfg(test)]
diff --git a/crates/ide/src/diagnostics/unresolved_module.rs b/crates/ide/src/diagnostics/unresolved_module.rs
index abf53a57c..4c8c74ff7 100644
--- a/crates/ide/src/diagnostics/unresolved_module.rs
+++ b/crates/ide/src/diagnostics/unresolved_module.rs
@@ -8,7 +8,10 @@ use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
8// Diagnostic: unresolved-module 8// Diagnostic: unresolved-module
9// 9//
10// This diagnostic is triggered if rust-analyzer is unable to discover referred module. 10// This diagnostic is triggered if rust-analyzer is unable to discover referred module.
11pub(super) fn render(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Diagnostic { 11pub(super) fn unresolved_module(
12 ctx: &DiagnosticsContext<'_>,
13 d: &hir::UnresolvedModule,
14) -> Diagnostic {
12 Diagnostic::new( 15 Diagnostic::new(
13 "unresolved-module", 16 "unresolved-module",
14 "unresolved module", 17 "unresolved module",