aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/diagnostics.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 7febd6eaf..b791747c2 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -858,4 +858,88 @@ impl Foo {
858"#, 858"#,
859 ); 859 );
860 } 860 }
861
862 #[test]
863 fn missing_unsafe_diagnostic_with_raw_ptr() {
864 check_diagnostics(
865 r#"
866fn main() {
867 let x = &5 as *const usize;
868 unsafe { let y = *x; }
869 let z = *x;
870} //^^ This operation is unsafe and requires an unsafe function or block
871"#,
872 )
873 }
874
875 #[test]
876 fn missing_unsafe_diagnostic_with_unsafe_call() {
877 check_diagnostics(
878 r#"
879struct HasUnsafe;
880
881impl HasUnsafe {
882 unsafe fn unsafe_fn(&self) {
883 let x = &5 as *const usize;
884 let y = *x;
885 }
886}
887
888unsafe fn unsafe_fn() {
889 let x = &5 as *const usize;
890 let y = *x;
891}
892
893fn main() {
894 unsafe_fn();
895 //^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
896 HasUnsafe.unsafe_fn();
897 //^^^^^^^^^^^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
898 unsafe {
899 unsafe_fn();
900 HasUnsafe.unsafe_fn();
901 }
902}
903"#,
904 );
905 }
906
907 #[test]
908 fn missing_unsafe_diagnostic_with_static_mut() {
909 check_diagnostics(
910 r#"
911struct Ty {
912 a: u8,
913}
914
915static mut STATIC_MUT: Ty = Ty { a: 0 };
916
917fn main() {
918 let x = STATIC_MUT.a;
919 //^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
920 unsafe {
921 let x = STATIC_MUT.a;
922 }
923}
924"#,
925 );
926 }
927
928 #[test]
929 fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
930 check_diagnostics(
931 r#"
932extern "rust-intrinsic" {
933 pub fn bitreverse(x: u32) -> u32; // Safe intrinsic
934 pub fn floorf32(x: f32) -> f32; // Unsafe intrinsic
935}
936
937fn main() {
938 let _ = bitreverse(12);
939 let _ = floorf32(12.0);
940 //^^^^^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
941}
942"#,
943 );
944 }
861} 945}