aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-05-24 06:33:22 +0100
committerPaul Daniel Faria <[email protected]>2020-06-27 15:09:29 +0100
commitdaf1cac9f87023d37a4418ea24ed615c9706258b (patch)
treed03541e3288316c2570bbc86a1b2bd97eea9292c /crates/ra_hir
parent0b95bed83fc8db897f54b350168567f14527e8de (diff)
Move diagnostics back into expr, add tests for diagnostics, fix logic to account for derefs of raw ptrs
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs5
-rw-r--r--crates/ra_hir/src/diagnostics.rs50
2 files changed, 2 insertions, 53 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 131180a63..13e763e52 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -25,7 +25,7 @@ use hir_expand::{
25use hir_ty::{ 25use hir_ty::{
26 autoderef, 26 autoderef,
27 display::{HirDisplayError, HirFormatter}, 27 display::{HirDisplayError, HirFormatter},
28 expr::ExprValidator, 28 expr::{ExprValidator, UnsafeValidator},
29 method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, 29 method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs,
30 TraitEnvironment, Ty, TyDefId, TypeCtor, 30 TraitEnvironment, Ty, TyDefId, TypeCtor,
31}; 31};
@@ -36,7 +36,6 @@ use rustc_hash::FxHashSet;
36 36
37use crate::{ 37use crate::{
38 db::{DefDatabase, HirDatabase}, 38 db::{DefDatabase, HirDatabase},
39 diagnostics::UnsafeValidator,
40 has_source::HasSource, 39 has_source::HasSource,
41 CallableDef, HirDisplay, InFile, Name, 40 CallableDef, HirDisplay, InFile, Name,
42}; 41};
@@ -680,7 +679,7 @@ impl Function {
680 infer.add_diagnostics(db, self.id, sink); 679 infer.add_diagnostics(db, self.id, sink);
681 let mut validator = ExprValidator::new(self.id, infer.clone(), sink); 680 let mut validator = ExprValidator::new(self.id, infer.clone(), sink);
682 validator.validate_body(db); 681 validator.validate_body(db);
683 let mut validator = UnsafeValidator::new(&self, infer, sink); 682 let mut validator = UnsafeValidator::new(self.id, infer, sink);
684 validator.validate_body(db); 683 validator.validate_body(db);
685 } 684 }
686} 685}
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs
index 562f3fe5c..c82883d0c 100644
--- a/crates/ra_hir/src/diagnostics.rs
+++ b/crates/ra_hir/src/diagnostics.rs
@@ -2,53 +2,3 @@
2pub use hir_def::diagnostics::UnresolvedModule; 2pub use hir_def::diagnostics::UnresolvedModule;
3pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; 3pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
4pub use hir_ty::diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr, NoSuchField}; 4pub use hir_ty::diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr, NoSuchField};
5
6use std::sync::Arc;
7
8use crate::code_model::Function;
9use crate::db::HirDatabase;
10use crate::has_source::HasSource;
11use hir_ty::{
12 diagnostics::{MissingUnsafe, UnnecessaryUnsafe},
13 expr::unsafe_expressions,
14 InferenceResult,
15};
16use ra_syntax::AstPtr;
17
18pub struct UnsafeValidator<'a, 'b: 'a> {
19 func: &'a Function,
20 infer: Arc<InferenceResult>,
21 sink: &'a mut DiagnosticSink<'b>,
22}
23
24impl<'a, 'b> UnsafeValidator<'a, 'b> {
25 pub fn new(
26 func: &'a Function,
27 infer: Arc<InferenceResult>,
28 sink: &'a mut DiagnosticSink<'b>,
29 ) -> UnsafeValidator<'a, 'b> {
30 UnsafeValidator { func, infer, sink }
31 }
32
33 pub fn validate_body(&mut self, db: &dyn HirDatabase) {
34 let def = self.func.id.into();
35 let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def);
36 let func_data = db.function_data(self.func.id);
37 let unnecessary = func_data.is_unsafe && unsafe_expressions.len() == 0;
38 let missing = !func_data.is_unsafe && unsafe_expressions.len() > 0;
39 if !(unnecessary || missing) {
40 return;
41 }
42
43 let in_file = self.func.source(db);
44 let file = in_file.file_id;
45 let fn_def = AstPtr::new(&in_file.value);
46 let fn_name = func_data.name.clone().into();
47
48 if unnecessary {
49 self.sink.push(UnnecessaryUnsafe { file, fn_def, fn_name })
50 } else {
51 self.sink.push(MissingUnsafe { file, fn_def, fn_name })
52 }
53 }
54}