File tree 1 file changed +41
-4
lines changed
1 file changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -43,9 +43,6 @@ macro_rules! iprintln {
43
43
/// let y = alias();
44
44
/// // BAD this second call to `alias` will definitively `panic!`
45
45
/// let y_alias = alias();
46
- ///
47
- /// # // check that the call to `uninitialized` requires unsafe
48
- /// # singleton!(: u8 = unsafe { std::mem::uninitialized() });
49
46
/// }
50
47
///
51
48
/// fn alias() -> &'static mut bool {
@@ -59,15 +56,55 @@ macro_rules! singleton {
59
56
static mut USED : bool = false ;
60
57
static mut VAR : $crate:: UntaggedOption <$ty> = $crate:: UntaggedOption { none: ( ) } ;
61
58
62
- if unsafe { USED } {
59
+
60
+ #[ allow( unsafe_code) ]
61
+ let used = unsafe { USED } ;
62
+ if used {
63
63
None
64
64
} else {
65
+ #[ allow( unsafe_code) ]
65
66
unsafe { USED = true }
67
+
66
68
let expr = $expr;
69
+
70
+ #[ allow( unsafe_code) ]
67
71
unsafe { VAR . some = expr }
72
+
73
+ #[ allow( unsafe_code) ]
68
74
let var: & ' static mut _ = unsafe { & mut VAR . some } ;
75
+
69
76
Some ( var)
70
77
}
71
78
} )
72
79
}
73
80
}
81
+
82
+
83
+ /// ``` compile_fail
84
+ /// #[macro_use(singleton)]
85
+ /// extern crate cortex_m;
86
+ ///
87
+ /// fn main() {}
88
+ ///
89
+ /// fn foo() {
90
+ /// // check that the call to `uninitialized` requires unsafe
91
+ /// singleton!(: u8 = std::mem::uninitialized());
92
+ /// }
93
+ /// ```
94
+ #[ allow( dead_code) ]
95
+ const CFAIL : ( ) = ( ) ;
96
+
97
+ /// ```
98
+ /// #![deny(unsafe_code)]
99
+ /// #[macro_use(singleton)]
100
+ /// extern crate cortex_m;
101
+ ///
102
+ /// fn main() {}
103
+ ///
104
+ /// fn foo() {
105
+ /// // check that calls to `singleton!` don't trip the `unsafe_code` lint
106
+ /// singleton!(: u8 = 0);
107
+ /// }
108
+ /// ```
109
+ #[ allow( dead_code) ]
110
+ const CPASS : ( ) = ( ) ;
You can’t perform that action at this time.
0 commit comments