From 0b95bed83fc8db897f54b350168567f14527e8de Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 23 May 2020 17:49:53 -0400 Subject: Add unsafe diagnostics and unsafe highlighting --- crates/ra_hir_ty/src/diagnostics.rs | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'crates/ra_hir_ty/src/diagnostics.rs') diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index ebd9cb08f..3469cc680 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -169,3 +169,61 @@ impl AstDiagnostic for BreakOutsideOfLoop { ast::Expr::cast(node).unwrap() } } + +#[derive(Debug)] +pub struct MissingUnsafe { + pub file: HirFileId, + pub fn_def: AstPtr, + pub fn_name: Name, +} + +impl Diagnostic for MissingUnsafe { + fn message(&self) -> String { + format!("Missing unsafe marker on fn `{}`", self.fn_name) + } + fn source(&self) -> InFile { + InFile { file_id: self.file, value: self.fn_def.clone().into() } + } + fn as_any(&self) -> &(dyn Any + Send + 'static) { + self + } +} + +impl AstDiagnostic for MissingUnsafe { + type AST = ast::FnDef; + + fn ast(&self, db: &impl AstDatabase) -> Self::AST { + let root = db.parse_or_expand(self.source().file_id).unwrap(); + let node = self.source().value.to_node(&root); + ast::FnDef::cast(node).unwrap() + } +} + +#[derive(Debug)] +pub struct UnnecessaryUnsafe { + pub file: HirFileId, + pub fn_def: AstPtr, + pub fn_name: Name, +} + +impl Diagnostic for UnnecessaryUnsafe { + fn message(&self) -> String { + format!("Unnecessary unsafe marker on fn `{}`", self.fn_name) + } + fn source(&self) -> InFile { + InFile { file_id: self.file, value: self.fn_def.clone().into() } + } + fn as_any(&self) -> &(dyn Any + Send + 'static) { + self + } +} + +impl AstDiagnostic for UnnecessaryUnsafe { + type AST = ast::FnDef; + + fn ast(&self, db: &impl AstDatabase) -> Self::AST { + let root = db.parse_or_expand(self.source().file_id).unwrap(); + let node = self.source().value.to_node(&root); + ast::FnDef::cast(node).unwrap() + } +} -- cgit v1.2.3