aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-06-13 18:00:27 +0100
committerAleksey Kladov <[email protected]>2021-06-13 18:01:01 +0100
commitbccf77f26cd504de14f7d7d03f9f2a85d0fabb3d (patch)
tree2d38ea6fb04b05a44467bd9614253b8621848f20 /crates/ide/src/diagnostics.rs
parent886b66cd03cbe7cb13e248d7c7bbdeba66c7796a (diff)
internal: refactor missing unsafe diagnostic
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r--crates/ide/src/diagnostics.rs86
1 files changed, 2 insertions, 84 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index e8f22c889..67390345f 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -8,6 +8,7 @@ mod break_outside_of_loop;
8mod inactive_code; 8mod inactive_code;
9mod macro_error; 9mod macro_error;
10mod missing_fields; 10mod missing_fields;
11mod missing_unsafe;
11mod no_such_field; 12mod no_such_field;
12mod unimplemented_builtin_macro; 13mod unimplemented_builtin_macro;
13mod unresolved_extern_crate; 14mod unresolved_extern_crate;
@@ -222,6 +223,7 @@ pub(crate) fn diagnostics(
222 AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d), 223 AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d),
223 AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d), 224 AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
224 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), 225 AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
226 AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
225 AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d), 227 AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
226 AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d), 228 AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
227 AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d), 229 AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
@@ -713,90 +715,6 @@ mod foo;
713 ); 715 );
714 } 716 }
715 717
716 #[test]
717 fn missing_unsafe_diagnostic_with_raw_ptr() {
718 check_diagnostics(
719 r#"
720fn main() {
721 let x = &5 as *const usize;
722 unsafe { let y = *x; }
723 let z = *x;
724} //^^ This operation is unsafe and requires an unsafe function or block
725"#,
726 )
727 }
728
729 #[test]
730 fn missing_unsafe_diagnostic_with_unsafe_call() {
731 check_diagnostics(
732 r#"
733struct HasUnsafe;
734
735impl HasUnsafe {
736 unsafe fn unsafe_fn(&self) {
737 let x = &5 as *const usize;
738 let y = *x;
739 }
740}
741
742unsafe fn unsafe_fn() {
743 let x = &5 as *const usize;
744 let y = *x;
745}
746
747fn main() {
748 unsafe_fn();
749 //^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
750 HasUnsafe.unsafe_fn();
751 //^^^^^^^^^^^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
752 unsafe {
753 unsafe_fn();
754 HasUnsafe.unsafe_fn();
755 }
756}
757"#,
758 );
759 }
760
761 #[test]
762 fn missing_unsafe_diagnostic_with_static_mut() {
763 check_diagnostics(
764 r#"
765struct Ty {
766 a: u8,
767}
768
769static mut STATIC_MUT: Ty = Ty { a: 0 };
770
771fn main() {
772 let x = STATIC_MUT.a;
773 //^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
774 unsafe {
775 let x = STATIC_MUT.a;
776 }
777}
778"#,
779 );
780 }
781
782 #[test]
783 fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
784 check_diagnostics(
785 r#"
786extern "rust-intrinsic" {
787 pub fn bitreverse(x: u32) -> u32; // Safe intrinsic
788 pub fn floorf32(x: f32) -> f32; // Unsafe intrinsic
789}
790
791fn main() {
792 let _ = bitreverse(12);
793 let _ = floorf32(12.0);
794 //^^^^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
795}
796"#,
797 );
798 }
799
800 // Register the required standard library types to make the tests work 718 // Register the required standard library types to make the tests work
801 fn add_filter_map_with_find_next_boilerplate(body: &str) -> String { 719 fn add_filter_map_with_find_next_boilerplate(body: &str) -> String {
802 let prefix = r#" 720 let prefix = r#"