From b9070cc64e0767d2a8bde5084a61f46e2e804f5b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jul 2020 16:43:39 +0200 Subject: Refactor the test of diagnostic tests --- crates/ra_hir_ty/src/diagnostics/expr.rs | 2 +- crates/ra_hir_ty/src/diagnostics/match_check.rs | 2 +- crates/ra_hir_ty/src/diagnostics/unsafe_check.rs | 50 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_ty/src/diagnostics') diff --git a/crates/ra_hir_ty/src/diagnostics/expr.rs b/crates/ra_hir_ty/src/diagnostics/expr.rs index 277ace180..91caedc3d 100644 --- a/crates/ra_hir_ty/src/diagnostics/expr.rs +++ b/crates/ra_hir_ty/src/diagnostics/expr.rs @@ -376,7 +376,7 @@ pub fn record_pattern_missing_fields( #[cfg(test)] mod tests { - use crate::diagnostics::check_diagnostics; + use crate::diagnostics::tests::check_diagnostics; #[test] fn simple_free_fn_zero() { diff --git a/crates/ra_hir_ty/src/diagnostics/match_check.rs b/crates/ra_hir_ty/src/diagnostics/match_check.rs index 95f272811..507edcb7d 100644 --- a/crates/ra_hir_ty/src/diagnostics/match_check.rs +++ b/crates/ra_hir_ty/src/diagnostics/match_check.rs @@ -837,7 +837,7 @@ fn enum_variant_matches(cx: &MatchCheckCtx, pat_id: PatId, enum_variant_id: Enum #[cfg(test)] mod tests { - use crate::diagnostics::check_diagnostics; + use crate::diagnostics::tests::check_diagnostics; #[test] fn empty_tuple() { diff --git a/crates/ra_hir_ty/src/diagnostics/unsafe_check.rs b/crates/ra_hir_ty/src/diagnostics/unsafe_check.rs index b8ff95ee1..9e4ed9a8b 100644 --- a/crates/ra_hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/ra_hir_ty/src/diagnostics/unsafe_check.rs @@ -121,3 +121,53 @@ fn walk_unsafe( walk_unsafe(unsafe_exprs, db, infer, body, child, inside_unsafe_block); }); } + +#[cfg(test)] +mod tests { + use crate::diagnostics::tests::check_diagnostics; + + #[test] + fn missing_unsafe_diagnostic_with_raw_ptr() { + check_diagnostics( + r#" +fn main() { + let x = &5 as *const usize; + unsafe { let y = *x; } + let z = *x; +} //^^ This operation is unsafe and requires an unsafe function or block +"#, + ) + } + + #[test] + fn missing_unsafe_diagnostic_with_unsafe_call() { + check_diagnostics( + r#" +struct HasUnsafe; + +impl HasUnsafe { + unsafe fn unsafe_fn(&self) { + let x = &5 as *const usize; + let y = *x; + } +} + +unsafe fn unsafe_fn() { + let x = &5 as *const usize; + let y = *x; +} + +fn main() { + unsafe_fn(); + //^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block + HasUnsafe.unsafe_fn(); + //^^^^^^^^^^^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block + unsafe { + unsafe_fn(); + HasUnsafe.unsafe_fn(); + } +} +"#, + ); + } +} -- cgit v1.2.3