aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-05-24 07:10:34 +0100
committerPaul Daniel Faria <[email protected]>2020-06-27 15:09:29 +0100
commitb358fbfdf82409700a8a328794429ec790306fc2 (patch)
treec21ff40842d2141f2b3043d3ccef9e9222157ed8 /crates/ra_hir_ty
parentdaf1cac9f87023d37a4418ea24ed615c9706258b (diff)
Add tests covering unsafe blocks, more attempts to get call expr tests passing
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/src/expr.rs17
-rw-r--r--crates/ra_hir_ty/src/tests.rs70
2 files changed, 86 insertions, 1 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs
index 7532e2dc7..04668f486 100644
--- a/crates/ra_hir_ty/src/expr.rs
+++ b/crates/ra_hir_ty/src/expr.rs
@@ -329,13 +329,28 @@ pub fn unsafe_expressions(
329 match expr { 329 match expr {
330 Expr::Call { callee, .. } => { 330 Expr::Call { callee, .. } => {
331 if infer 331 if infer
332 .method_resolution(*callee) 332 .method_resolution(/* id */ *callee)
333 .map(|func| db.function_data(func).is_unsafe) 333 .map(|func| db.function_data(func).is_unsafe)
334 .unwrap_or(false) 334 .unwrap_or(false)
335 { 335 {
336 unsafe_expr_ids.push(id); 336 unsafe_expr_ids.push(id);
337 } 337 }
338 } 338 }
339 Expr::MethodCall {/*_receiver, method_name,*/ .. } => {
340 // let receiver_ty = &infer.type_of_expr[*receiver];
341 // receiver_ty
342 if infer
343 .method_resolution(id)
344 .map(|func| {
345 db.function_data(func).is_unsafe
346 })
347 .unwrap_or_else(|| {
348 false
349 })
350 {
351 unsafe_expr_ids.push(id);
352 }
353 }
339 Expr::UnaryOp { expr, op: UnaryOp::Deref } => { 354 Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
340 if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { 355 if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
341 unsafe_expr_ids.push(id); 356 unsafe_expr_ids.push(id);
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index 4ff2b2d4a..c1f6fbab8 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -603,6 +603,76 @@ fn missing_unsafe() {
603} 603}
604 604
605#[test] 605#[test]
606fn no_missing_unsafe_diagnostic_with_raw_ptr_in_unsafe_block() {
607 let diagnostics = TestDB::with_files(
608 r"
609//- /lib.rs
610fn nothing_to_see_move_along() {
611 unsafe {
612 let x = &5 as *usize;
613 let y = *x;
614 }
615}
616",
617 )
618 .diagnostics()
619 .0;
620
621 assert_snapshot!(diagnostics, @"");
622}
623
624#[test]
625fn no_missing_unsafe_diagnostic_with_unsafe_call_in_unsafe_block() {
626 let diagnostics = TestDB::with_files(
627 r"
628//- /lib.rs
629unsafe fn unsafe_fn() {
630 let x = &5 as *usize;
631 let y = *x;
632}
633
634fn nothing_to_see_move_along() {
635 unsafe {
636 unsafe_fn();
637 }
638}
639",
640 )
641 .diagnostics()
642 .0;
643
644 assert_snapshot!(diagnostics, @"");
645}
646
647#[test]
648fn no_missing_unsafe_diagnostic_with_unsafe_method_call_in_unsafe_block() {
649 let diagnostics = TestDB::with_files(
650 r"
651//- /lib.rs
652struct HasUnsafe;
653
654impl HasUnsafe {
655 unsafe fn unsafe_fn() {
656 let x = &5 as *usize;
657 let y = *x;
658 }
659}
660
661fn nothing_to_see_move_along() {
662 unsafe {
663 HasUnsafe.unsafe_fn();
664 }
665}
666
667",
668 )
669 .diagnostics()
670 .0;
671
672 assert_snapshot!(diagnostics, @"");
673}
674
675#[test]
606fn unnecessary_unsafe_diagnostic() { 676fn unnecessary_unsafe_diagnostic() {
607 let diagnostics = TestDB::with_files( 677 let diagnostics = TestDB::with_files(
608 r" 678 r"