aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-25 20:41:44 +0100
committerGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-25 20:41:44 +0100
commit032af4b2b914f42b5533358cd2f40bbc7a888256 (patch)
tree67afff58530513cbf82329a9e90c5bea8f89fa93
parentd3940ec95cca6673c919f37c319170019374da88 (diff)
downloadcortex-m-032af4b2b914f42b5533358cd2f40bbc7a888256.tar.gz
cortex-m-032af4b2b914f42b5533358cd2f40bbc7a888256.tar.zst
cortex-m-032af4b2b914f42b5533358cd2f40bbc7a888256.zip
don't trip the unsafe_code lint
-rw-r--r--src/macros.rs45
1 files changed, 41 insertions, 4 deletions
diff --git a/src/macros.rs b/src/macros.rs
index 45c511c..7d2cf6a 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -43,9 +43,6 @@ macro_rules! iprintln {
/// let y = alias();
/// // BAD this second call to `alias` will definitively `panic!`
/// let y_alias = alias();
-///
-/// # // check that the call to `uninitialized` requires unsafe
-/// # singleton!(: u8 = unsafe { std::mem::uninitialized() });
/// }
///
/// fn alias() -> &'static mut bool {
@@ -59,15 +56,55 @@ macro_rules! singleton {
static mut USED: bool = false;
static mut VAR: $crate::UntaggedOption<$ty> = $crate::UntaggedOption { none: () };
- if unsafe { USED } {
+
+ #[allow(unsafe_code)]
+ let used = unsafe { USED };
+ if used {
None
} else {
+ #[allow(unsafe_code)]
unsafe { USED = true }
+
let expr = $expr;
+
+ #[allow(unsafe_code)]
unsafe { VAR.some = expr }
+
+ #[allow(unsafe_code)]
let var: &'static mut _ = unsafe { &mut VAR.some };
+
Some(var)
}
})
}
}
+
+
+/// ``` compile_fail
+/// #[macro_use(singleton)]
+/// extern crate cortex_m;
+///
+/// fn main() {}
+///
+/// fn foo() {
+/// // check that the call to `uninitialized` requires unsafe
+/// singleton!(: u8 = std::mem::uninitialized());
+/// }
+/// ```
+#[allow(dead_code)]
+const CFAIL: () = ();
+
+/// ```
+/// #![deny(unsafe_code)]
+/// #[macro_use(singleton)]
+/// extern crate cortex_m;
+///
+/// fn main() {}
+///
+/// fn foo() {
+/// // check that calls to `singleton!` don't trip the `unsafe_code` lint
+/// singleton!(: u8 = 0);
+/// }
+/// ```
+#[allow(dead_code)]
+const CPASS: () = ();