From b358fbfdf82409700a8a328794429ec790306fc2 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 24 May 2020 02:10:34 -0400 Subject: Add tests covering unsafe blocks, more attempts to get call expr tests passing --- crates/ra_hir_ty/src/expr.rs | 17 ++++++++++- crates/ra_hir_ty/src/tests.rs | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_ty') 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( match expr { Expr::Call { callee, .. } => { if infer - .method_resolution(*callee) + .method_resolution(/* id */ *callee) .map(|func| db.function_data(func).is_unsafe) .unwrap_or(false) { unsafe_expr_ids.push(id); } } + Expr::MethodCall {/*_receiver, method_name,*/ .. } => { + // let receiver_ty = &infer.type_of_expr[*receiver]; + // receiver_ty + if infer + .method_resolution(id) + .map(|func| { + db.function_data(func).is_unsafe + }) + .unwrap_or_else(|| { + false + }) + { + unsafe_expr_ids.push(id); + } + } Expr::UnaryOp { expr, op: UnaryOp::Deref } => { if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { 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 @@ -602,6 +602,76 @@ fn missing_unsafe() { assert_snapshot!(diagnostics, @r#""fn missing_unsafe() {\n HasUnsafe.unsafe_fn();\n}": Missing unsafe keyword on fn"#); } +#[test] +fn no_missing_unsafe_diagnostic_with_raw_ptr_in_unsafe_block() { + let diagnostics = TestDB::with_files( + r" +//- /lib.rs +fn nothing_to_see_move_along() { + unsafe { + let x = &5 as *usize; + let y = *x; + } +} +", + ) + .diagnostics() + .0; + + assert_snapshot!(diagnostics, @""); +} + +#[test] +fn no_missing_unsafe_diagnostic_with_unsafe_call_in_unsafe_block() { + let diagnostics = TestDB::with_files( + r" +//- /lib.rs +unsafe fn unsafe_fn() { + let x = &5 as *usize; + let y = *x; +} + +fn nothing_to_see_move_along() { + unsafe { + unsafe_fn(); + } +} +", + ) + .diagnostics() + .0; + + assert_snapshot!(diagnostics, @""); +} + +#[test] +fn no_missing_unsafe_diagnostic_with_unsafe_method_call_in_unsafe_block() { + let diagnostics = TestDB::with_files( + r" +//- /lib.rs +struct HasUnsafe; + +impl HasUnsafe { + unsafe fn unsafe_fn() { + let x = &5 as *usize; + let y = *x; + } +} + +fn nothing_to_see_move_along() { + unsafe { + HasUnsafe.unsafe_fn(); + } +} + +", + ) + .diagnostics() + .0; + + assert_snapshot!(diagnostics, @""); +} + #[test] fn unnecessary_unsafe_diagnostic() { let diagnostics = TestDB::with_files( -- cgit v1.2.3