aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs8
-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
-rw-r--r--crates/ra_ide/src/snapshots/highlight_injection.html2
-rw-r--r--crates/ra_ide/src/snapshots/highlight_strings.html2
-rw-r--r--crates/ra_ide/src/snapshots/highlight_unsafe.html4
-rw-r--r--crates/ra_ide/src/snapshots/highlighting.html2
-rw-r--r--crates/ra_ide/src/snapshots/rainbow_highlighting.html2
-rw-r--r--crates/ra_ide/src/syntax_highlighting/html.rs2
11 files changed, 81 insertions, 58 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 13e763e52..9e3b4a2d8 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -25,9 +25,11 @@ use hir_expand::{
25use hir_ty::{ 25use hir_ty::{
26 autoderef, 26 autoderef,
27 display::{HirDisplayError, HirFormatter}, 27 display::{HirDisplayError, HirFormatter},
28 expr::{ExprValidator, UnsafeValidator}, 28 expr::ExprValidator,
29 method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, 29 method_resolution,
30 TraitEnvironment, Ty, TyDefId, TypeCtor, 30 method_resolution, ApplicationTy, Canonical, InEnvironment, Substs, TraitEnvironment, Ty,
31 TyDefId, TypeCtor,
32 unsafe_validation::UnsafeValidator,
31}; 33};
32use ra_db::{CrateId, CrateName, Edition, FileId}; 34use ra_db::{CrateId, CrateName, Edition, FileId};
33use ra_prof::profile; 35use ra_prof::profile;
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}
diff --git a/crates/ra_ide/src/snapshots/highlight_injection.html b/crates/ra_ide/src/snapshots/highlight_injection.html
index 1b0349bae..3ac6c4ae5 100644
--- a/crates/ra_ide/src/snapshots/highlight_injection.html
+++ b/crates/ra_ide/src/snapshots/highlight_injection.html
@@ -12,7 +12,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
12.string_literal { color: #CC9393; } 12.string_literal { color: #CC9393; }
13.field { color: #94BFF3; } 13.field { color: #94BFF3; }
14.function { color: #93E0E3; } 14.function { color: #93E0E3; }
15.function.unsafe { color: #BC8383; } 15.function.unsafe { color: #E28C14; }
16.operator.unsafe { color: #BC8383; } 16.operator.unsafe { color: #BC8383; }
17.parameter { color: #94BFF3; } 17.parameter { color: #94BFF3; }
18.text { color: #DCDCCC; } 18.text { color: #DCDCCC; }
diff --git a/crates/ra_ide/src/snapshots/highlight_strings.html b/crates/ra_ide/src/snapshots/highlight_strings.html
index d184b5691..8556eb850 100644
--- a/crates/ra_ide/src/snapshots/highlight_strings.html
+++ b/crates/ra_ide/src/snapshots/highlight_strings.html
@@ -12,7 +12,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
12.string_literal { color: #CC9393; } 12.string_literal { color: #CC9393; }
13.field { color: #94BFF3; } 13.field { color: #94BFF3; }
14.function { color: #93E0E3; } 14.function { color: #93E0E3; }
15.function.unsafe { color: #BC8383; } 15.function.unsafe { color: #E28C14; }
16.operator.unsafe { color: #BC8383; } 16.operator.unsafe { color: #BC8383; }
17.parameter { color: #94BFF3; } 17.parameter { color: #94BFF3; }
18.text { color: #DCDCCC; } 18.text { color: #DCDCCC; }
diff --git a/crates/ra_ide/src/snapshots/highlight_unsafe.html b/crates/ra_ide/src/snapshots/highlight_unsafe.html
index 6936e949f..692307280 100644
--- a/crates/ra_ide/src/snapshots/highlight_unsafe.html
+++ b/crates/ra_ide/src/snapshots/highlight_unsafe.html
@@ -12,7 +12,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
12.string_literal { color: #CC9393; } 12.string_literal { color: #CC9393; }
13.field { color: #94BFF3; } 13.field { color: #94BFF3; }
14.function { color: #93E0E3; } 14.function { color: #93E0E3; }
15.function.unsafe { color: #BC8383; } 15.function.unsafe { color: #E28C14; }
16.operator.unsafe { color: #BC8383; } 16.operator.unsafe { color: #BC8383; }
17.parameter { color: #94BFF3; } 17.parameter { color: #94BFF3; }
18.text { color: #DCDCCC; } 18.text { color: #DCDCCC; }
@@ -47,7 +47,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
47 <span class="keyword unsafe">unsafe</span> { 47 <span class="keyword unsafe">unsafe</span> {
48 <span class="function unsafe">unsafe_fn</span>(); 48 <span class="function unsafe">unsafe_fn</span>();
49 <span class="struct">HasUnsafeFn</span>.<span class="function unsafe">unsafe_method</span>(); 49 <span class="struct">HasUnsafeFn</span>.<span class="function unsafe">unsafe_method</span>();
50 <span class="keyword">let</span> <span class="variable declaration">y</span> = <span class="operator unsafe">*</span>(<span class="variable">x</span>); 50 <span class="keyword">let</span> <span class="variable declaration">y</span> = <span class="function unsafe">*</span><span class="variable">x</span>;
51 <span class="keyword">let</span> <span class="variable declaration">z</span> = -<span class="variable">x</span>; 51 <span class="keyword">let</span> <span class="variable declaration">z</span> = -<span class="variable">x</span>;
52 } 52 }
53}</code></pre> \ No newline at end of file 53}</code></pre> \ No newline at end of file
diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html
index 8d0b38f95..47403b367 100644
--- a/crates/ra_ide/src/snapshots/highlighting.html
+++ b/crates/ra_ide/src/snapshots/highlighting.html
@@ -12,7 +12,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
12.string_literal { color: #CC9393; } 12.string_literal { color: #CC9393; }
13.field { color: #94BFF3; } 13.field { color: #94BFF3; }
14.function { color: #93E0E3; } 14.function { color: #93E0E3; }
15.function.unsafe { color: #BC8383; } 15.function.unsafe { color: #E28C14; }
16.operator.unsafe { color: #BC8383; } 16.operator.unsafe { color: #BC8383; }
17.parameter { color: #94BFF3; } 17.parameter { color: #94BFF3; }
18.text { color: #DCDCCC; } 18.text { color: #DCDCCC; }
diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html
index 9516c7441..f5fb96f55 100644
--- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html
+++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html
@@ -12,7 +12,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
12.string_literal { color: #CC9393; } 12.string_literal { color: #CC9393; }
13.field { color: #94BFF3; } 13.field { color: #94BFF3; }
14.function { color: #93E0E3; } 14.function { color: #93E0E3; }
15.function.unsafe { color: #BC8383; } 15.function.unsafe { color: #E28C14; }
16.operator.unsafe { color: #BC8383; } 16.operator.unsafe { color: #BC8383; }
17.parameter { color: #94BFF3; } 17.parameter { color: #94BFF3; }
18.text { color: #DCDCCC; } 18.text { color: #DCDCCC; }
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs
index 0c74f7370..ed007c382 100644
--- a/crates/ra_ide/src/syntax_highlighting/html.rs
+++ b/crates/ra_ide/src/syntax_highlighting/html.rs
@@ -71,7 +71,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
71.string_literal { color: #CC9393; } 71.string_literal { color: #CC9393; }
72.field { color: #94BFF3; } 72.field { color: #94BFF3; }
73.function { color: #93E0E3; } 73.function { color: #93E0E3; }
74.function.unsafe { color: #BC8383; } 74.function.unsafe { color: #E28C14; }
75.operator.unsafe { color: #BC8383; } 75.operator.unsafe { color: #BC8383; }
76.parameter { color: #94BFF3; } 76.parameter { color: #94BFF3; }
77.text { color: #DCDCCC; } 77.text { color: #DCDCCC; }