33// This only refuses unsafe code in functions/expressions in this crate, not ones generated by this
44// crate's macros.
55#![ cfg_attr( not( any( doc, test) ) , forbid( unsafe_code) ) ]
6- // #[cfg(doc)]
7- // extern crate alloc;
6+ #[ cfg( doc) ]
7+ extern crate alloc;
88
99/// Invoke am unsafe function.
1010///
@@ -40,7 +40,8 @@ macro_rules! unsafe_fn {
4040 ( ~~ $last: expr) => {
4141 ( $last, )
4242 } ;
43- // Commented out: For now, we require the (potentially unsafe) function to have at least 1 argument.
43+ // Commented out: For now, we require the (potentially unsafe) function to have at least 1
44+ // argument.
4445 //
4546 //(~~) => { () };
4647
@@ -85,7 +86,7 @@ macro_rules! unsafe_fn {
8586/// Invoke an unsafe method. Like [unsafe_fn], but
8687/// - we accept a receiver `self`
8788/// - we store `self` in a variable outside of the generated `unsafe {...}`
88- /// - we don't allow $fn to be an expression (which doesn't work in standard methods calls), but
89+ /// - we do NOT allow $fn to be an expression (which doesn't work in standard methods calls), but
8990/// only an identifier.
9091#[ macro_export]
9192macro_rules! unsafe_method {
@@ -188,25 +189,21 @@ macro_rules! unsafe_deref_set {
188189/// underlying type).
189190#[ macro_export]
190191macro_rules! unsafe_ref {
191- ( $ptr: expr) => {
192- {
193- let ptr = $ptr;
194- let _: * const _ = ptr; // Partial type check: $ptr needs to yield a const pointer
195- unsafe { & * ptr }
196- }
197- } ;
192+ ( $ptr: expr) => { {
193+ let ptr = $ptr;
194+ let _: * const _ = ptr; // Partial type check: $ptr needs to yield a const pointer
195+ unsafe { & * ptr }
196+ } } ;
198197}
199198
200199/// Deref a `mut` pointer and yield a `mut` reference (to the same underlying type).
201200#[ macro_export]
202201macro_rules! unsafe_ref_mut {
203- ( $ptr: expr) => {
204- {
205- let ptr = $ptr;
206- let _: * mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
207- unsafe { & mut * ptr }
208- }
209- } ;
202+ ( $ptr: expr) => { {
203+ let ptr = $ptr;
204+ let _: * mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
205+ unsafe { & mut * ptr }
206+ } } ;
210207}
211208
212209/// Assign the given value to the location given in the pointer.
@@ -225,7 +222,9 @@ macro_rules! unsafe_ref_set {
225222 let ptr = $ptr;
226223 let _: * mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
227224 let value = $value;
228- unsafe { * ptr = value; }
225+ unsafe {
226+ * ptr = value;
227+ }
229228 } } ;
230229}
231230
@@ -237,11 +236,10 @@ macro_rules! unsafe_ref_set {
237236macro_rules! unsafe_cast {
238237 ( $ptr: expr, $t: ty) => { {
239238 let ptr = $ptr;
240- unsafe { ptr as $ty }
239+ unsafe { & * ptr as $t }
241240 } } ;
242241}
243242
244-
245243//-------------
246244
247245#[ cfg( test) ]
@@ -289,8 +287,8 @@ mod fn_method_tests {
289287 }
290288 fn _unsafe_set_x_0_simple_2 ( value : bool ) {
291289 * unsafe {
292- // @TODO The expression (here: X) could include subexpressions (like array index) and
293- // those may include `unsafe` code, too!
290+ // @TODO The expression (here: X) could include subexpressions (like array index)
291+ // and those may include `unsafe` code, too!
294292 #[ allow( static_mut_refs) ]
295293 & mut X [ 0 ]
296294 //
@@ -303,7 +301,7 @@ mod fn_method_tests {
303301
304302 // FLEXIBLE: <<~<<~<<
305303 ( * & mut unsafe { X } ) [ 0 ] = value;
306-
304+
307305 ( * & mut unsafe { X } ) = [ true , true ] ;
308306 }
309307 fn _unsafe_set_x_0_store_ref ( value : bool ) {
@@ -349,8 +347,8 @@ mod fn_method_tests {
349347
350348 // @TODO requires a return type
351349 //
352- // @TODO doesn't work if the static variable is identified with a
353- // path like `crate::mod_x::mod_y::STATIC_X``
350+ // @TODO doesn't work if the static variable is identified with a path like
351+ // `crate::mod_x::mod_y::STATIC_X``
354352 pub fn _f ( ) -> bool {
355353 #[ allow( non_snake_case) ]
356354 let X = & unsafe { super :: X } ;
@@ -397,8 +395,7 @@ mod fn_method_tests {
397395 let pt: *mut bool = &mut a as *mut bool;
398396
399397 let a2: bool = unsafe_deref!(pt);
400- // FAILS:
401- //unsafe_deref!(pt) = false;
398+ // FAILS: unsafe_deref!(pt) = false;
402399 }
403400 #[test]
404401 fn deref_mut_field_or_method() {
@@ -416,6 +413,9 @@ mod fn_method_tests {
416413mod casting_tests {
417414 use std:: fmt:: Display ;
418415
416+ #[ test]
417+ fn todo ( ) { }
418+
419419 #[ test]
420420 fn read_only ( ) {
421421 let a: bool = true ;
@@ -434,10 +434,10 @@ mod casting_tests {
434434 // Not allowed:
435435 //
436436 // unsafe { *pt } = false;
437- unsafe { * pt = false } ;
437+ unsafe { * pt = false } ;
438438
439439 // Move to a separate test:
440- unsafe_ref_set ! ( pt, false ) ;
440+ unsafe_ref_set ! ( pt, false ) ;
441441 }
442442
443443 #[ test]
0 commit comments