Skip to content

Commit a9dcd2c

Browse files
bors[bot]matklad
andcommitted
Merge #1495
1495: use correct file for diagnostics r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 3775e5e + 0b5e399 commit a9dcd2c

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

crates/ra_hir/src/diagnostics.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{any::Any, fmt};
22

3-
use ra_syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc};
3+
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc};
44
use relative_path::RelativePathBuf;
55

66
use crate::{HirDatabase, HirFileId, Name};
@@ -27,11 +27,17 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
2727
fn as_any(&self) -> &(dyn Any + Send + 'static);
2828
}
2929

30+
pub trait AstDiagnostic {
31+
type AST;
32+
fn ast(&self, db: &impl HirDatabase) -> Self::AST;
33+
}
34+
3035
impl dyn Diagnostic {
3136
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
3237
let node = db.parse_or_expand(self.file()).unwrap();
3338
self.syntax_node_ptr().to_node(&*node).to_owned()
3439
}
40+
3541
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
3642
self.as_any().downcast_ref()
3743
}
@@ -135,3 +141,13 @@ impl Diagnostic for MissingFields {
135141
self
136142
}
137143
}
144+
145+
impl AstDiagnostic for MissingFields {
146+
type AST = TreeArc<ast::NamedFieldList>;
147+
148+
fn ast(&self, db: &impl HirDatabase) -> Self::AST {
149+
let root = db.parse_or_expand(self.file()).unwrap();
150+
let node = self.syntax_node_ptr().to_node(&*root);
151+
ast::NamedFieldList::cast(&node).unwrap().to_owned()
152+
}
153+
}

crates/ra_ide_api/src/diagnostics.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::cell::RefCell;
22

33
use hir::{
4-
diagnostics::{Diagnostic as _, DiagnosticSink},
4+
diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink},
55
source_binder,
66
};
77
use itertools::Itertools;
88
use ra_assists::ast_editor::{AstBuilder, AstEditor};
99
use ra_db::SourceDatabase;
1010
use ra_prof::profile;
1111
use ra_syntax::{
12-
ast::{self, AstNode, NamedField, NamedFieldList},
12+
ast::{self, AstNode, NamedField},
1313
Location, SyntaxNode, TextRange, T,
1414
};
1515
use ra_text_edit::{TextEdit, TextEditBuilder};
@@ -34,9 +34,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
3434
fix: None,
3535
}));
3636

37-
let source_file = parse.tree;
38-
39-
for node in source_file.syntax().descendants() {
37+
for node in parse.tree.syntax().descendants() {
4038
check_unnecessary_braces_in_use_statement(&mut res, file_id, node);
4139
check_struct_shorthand_initialization(&mut res, file_id, node);
4240
}
@@ -61,9 +59,8 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
6159
})
6260
})
6361
.on::<hir::diagnostics::MissingFields, _>(|d| {
64-
let syntax_node = d.syntax_node_ptr();
65-
let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap();
66-
let mut ast_editor = AstEditor::new(node);
62+
let node = d.ast(db);
63+
let mut ast_editor = AstEditor::new(&*node);
6764
for f in d.missed_fields.iter() {
6865
ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f));
6966
}

0 commit comments

Comments
 (0)