aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-05-28 14:30:19 +0100
committerPaul Daniel Faria <[email protected]>2020-06-27 15:13:14 +0100
commitf678e0d837e472dc2f1421f89f794d33f3ade55c (patch)
tree0b8501bce63bc5b340f67fe770dbbcb41ecee2e4 /crates/ra_hir_ty
parent6c1682396c9894da07af74b43c2443e9bde89be4 (diff)
Add HighlightTag::Operator, use it for unsafe deref. Move unsafe validation to its own file
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/src/expr.rs48
-rw-r--r--crates/ra_hir_ty/src/lib.rs1
-rw-r--r--crates/ra_hir_ty/src/test_db.rs5
-rw-r--r--crates/ra_hir_ty/src/unsafe_validation.rs63
4 files changed, 69 insertions, 48 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index 3942aada5..99eed949f 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -9,9 +9,7 @@ use rustc_hash::FxHashSet;
9 9
10use crate::{ 10use crate::{
11 db::HirDatabase, 11 db::HirDatabase,
12 diagnostics::{ 12 diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields},
13 MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields, MissingUnsafe,
14 },
15 lower::CallableDef, 13 lower::CallableDef,
16 utils::variant_data, 14 utils::variant_data,
17 ApplicationTy, InferenceResult, Ty, TypeCtor, 15 ApplicationTy, InferenceResult, Ty, TypeCtor,
@@ -317,8 +315,8 @@ pub fn record_pattern_missing_fields(
317} 315}
318 316
319pub struct UnsafeExpr { 317pub struct UnsafeExpr {
320 expr: ExprId, 318 pub expr: ExprId,
321 inside_unsafe_block: bool, 319 pub inside_unsafe_block: bool,
322} 320}
323 321
324impl UnsafeExpr { 322impl UnsafeExpr {
@@ -383,43 +381,3 @@ pub fn unsafe_expressions(
383 381
384 unsafe_exprs 382 unsafe_exprs
385} 383}
386
387pub struct UnsafeValidator<'a, 'b: 'a> {
388 func: FunctionId,
389 infer: Arc<InferenceResult>,
390 sink: &'a mut DiagnosticSink<'b>,
391}
392
393impl<'a, 'b> UnsafeValidator<'a, 'b> {
394 pub fn new(
395 func: FunctionId,
396 infer: Arc<InferenceResult>,
397 sink: &'a mut DiagnosticSink<'b>,
398 ) -> UnsafeValidator<'a, 'b> {
399 UnsafeValidator { func, infer, sink }
400 }
401
402 pub fn validate_body(&mut self, db: &dyn HirDatabase) {
403 let def = self.func.into();
404 let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def);
405 let func_data = db.function_data(self.func);
406 if func_data.is_unsafe
407 || unsafe_expressions
408 .iter()
409 .filter(|unsafe_expr| !unsafe_expr.inside_unsafe_block)
410 .count()
411 == 0
412 {
413 return;
414 }
415
416 let (_, body_source) = db.body_with_source_map(def);
417 for unsafe_expr in unsafe_expressions {
418 if !unsafe_expr.inside_unsafe_block {
419 if let Ok(in_file) = body_source.as_ref().expr_syntax(unsafe_expr.expr) {
420 self.sink.push(MissingUnsafe { file: in_file.file_id, expr: in_file.value })
421 }
422 }
423 }
424 }
425}
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index f22232324..414158139 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -37,6 +37,7 @@ pub(crate) mod utils;
37pub mod db; 37pub mod db;
38pub mod diagnostics; 38pub mod diagnostics;
39pub mod expr; 39pub mod expr;
40pub mod unsafe_validation;
40 41
41#[cfg(test)] 42#[cfg(test)]
42mod tests; 43mod tests;
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs
index 9ccf2aa37..9c2c6959d 100644
--- a/crates/ra_hir_ty/src/test_db.rs
+++ b/crates/ra_hir_ty/src/test_db.rs
@@ -12,9 +12,8 @@ use rustc_hash::FxHashSet;
12use stdx::format_to; 12use stdx::format_to;
13 13
14use crate::{ 14use crate::{
15 db::HirDatabase, 15 db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator,
16 diagnostics::Diagnostic, 16 unsafe_validation::UnsafeValidator,
17 expr::{ExprValidator, UnsafeValidator},
18}; 17};
19 18
20#[salsa::database( 19#[salsa::database(
diff --git a/crates/ra_hir_ty/src/unsafe_validation.rs b/crates/ra_hir_ty/src/unsafe_validation.rs
new file mode 100644
index 000000000..55dbe23fa
--- /dev/null
+++ b/crates/ra_hir_ty/src/unsafe_validation.rs
@@ -0,0 +1,63 @@
1//! Provides validations for unsafe code. Currently checks if unsafe functions are missing
2//! unsafe blocks.
3
4use std::sync::Arc;
5
6use hir_def::FunctionId;
7use hir_expand::diagnostics::DiagnosticSink;
8
9use crate::{
10 db::HirDatabase, diagnostics::MissingUnsafe, expr::unsafe_expressions, InferenceResult,
11};
12
13pub use hir_def::{
14 body::{
15 scope::{ExprScopes, ScopeEntry, ScopeId},
16 Body, BodySourceMap, ExprPtr, ExprSource, PatPtr, PatSource,
17 },
18 expr::{
19 ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
20 MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
21 },
22 LocalFieldId, VariantId,
23};
24
25pub struct UnsafeValidator<'a, 'b: 'a> {
26 func: FunctionId,
27 infer: Arc<InferenceResult>,
28 sink: &'a mut DiagnosticSink<'b>,
29}
30
31impl<'a, 'b> UnsafeValidator<'a, 'b> {
32 pub fn new(
33 func: FunctionId,
34 infer: Arc<InferenceResult>,
35 sink: &'a mut DiagnosticSink<'b>,
36 ) -> UnsafeValidator<'a, 'b> {
37 UnsafeValidator { func, infer, sink }
38 }
39
40 pub fn validate_body(&mut self, db: &dyn HirDatabase) {
41 let def = self.func.into();
42 let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def);
43 let func_data = db.function_data(self.func);
44 if func_data.is_unsafe
45 || unsafe_expressions
46 .iter()
47 .filter(|unsafe_expr| !unsafe_expr.inside_unsafe_block)
48 .count()
49 == 0
50 {
51 return;
52 }
53
54 let (_, body_source) = db.body_with_source_map(def);
55 for unsafe_expr in unsafe_expressions {
56 if !unsafe_expr.inside_unsafe_block {
57 if let Ok(in_file) = body_source.as_ref().expr_syntax(unsafe_expr.expr) {
58 self.sink.push(MissingUnsafe { file: in_file.file_id, expr: in_file.value })
59 }
60 }
61 }
62 }
63}