Skip to content

Commit 032af4b

Browse files
committed
don't trip the unsafe_code lint
1 parent d3940ec commit 032af4b

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/macros.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ macro_rules! iprintln {
4343
/// let y = alias();
4444
/// // BAD this second call to `alias` will definitively `panic!`
4545
/// let y_alias = alias();
46-
///
47-
/// # // check that the call to `uninitialized` requires unsafe
48-
/// # singleton!(: u8 = unsafe { std::mem::uninitialized() });
4946
/// }
5047
///
5148
/// fn alias() -> &'static mut bool {
@@ -59,15 +56,55 @@ macro_rules! singleton {
5956
static mut USED: bool = false;
6057
static mut VAR: $crate::UntaggedOption<$ty> = $crate::UntaggedOption { none: () };
6158

62-
if unsafe { USED } {
59+
60+
#[allow(unsafe_code)]
61+
let used = unsafe { USED };
62+
if used {
6363
None
6464
} else {
65+
#[allow(unsafe_code)]
6566
unsafe { USED = true }
67+
6668
let expr = $expr;
69+
70+
#[allow(unsafe_code)]
6771
unsafe { VAR.some = expr }
72+
73+
#[allow(unsafe_code)]
6874
let var: &'static mut _ = unsafe { &mut VAR.some };
75+
6976
Some(var)
7077
}
7178
})
7279
}
7380
}
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: () = ();

0 commit comments

Comments
 (0)