Skip to content

Commit 3662946

Browse files
Tests and docs
1 parent d295c79 commit 3662946

File tree

9 files changed

+39
-60
lines changed

9 files changed

+39
-60
lines changed

src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
// code generated by this crate's macros. (https://github.com/rust-lang/nomicon/issues/506)
55
#![cfg_attr(not(any(doc, test)), forbid(unsafe_code))]
66
#![forbid(unknown_lints)]
7-
#![forbid(unused, dead_code)]
7+
// We can't `#![forbid(dead_code)]`, because we use `#[allow(unused_unsafe)]`. Without that
8+
// unsafe_method! existed only as multiple specialized macros: unsafe_method_ref!,
9+
// unsafe_method_mut!... And there were problems with unintended duplicates of Copy `self` when
10+
// invoking methods with the receiver being &self, that is, a shared reference.
11+
#![deny(unused)]
12+
#![forbid(dead_code)]
813
// docs
914
#![forbid(missing_docs)]
1015
// rustdoc lints: https://doc.rust-lang.org/rustdoc/lints.html
@@ -73,13 +78,22 @@ extern crate alloc;
7378
#[doc = include_str!("../violations_coverage/unsafe_fn/some_args/arg.rs")]
7479
/// ```
7580
/// ```
81+
/// # // @TODO
7682
/// # use prudent::unsafe_fn;
7783
/// unsafe fn return_array() -> [bool; 1] {
7884
/// [true]
7985
/// }
8086
///
8187
/// let _b = unsafe_fn!( return_array)[0];
8288
/// ```
89+
/// ```no_run
90+
/// # use prudent::unsafe_fn;
91+
/// unsafe fn return_mut_ref_array() -> &'static mut [bool; 1] {
92+
/// unreachable!()
93+
/// }
94+
///
95+
/// unsafe_fn!( return_mut_ref_array)[0] = true;
96+
/// ```
8397
#[macro_export]
8498
macro_rules! unsafe_fn {
8599
( $fn:expr $(, $arg:expr)* ) => {
@@ -131,6 +145,9 @@ pub const _: () = {};
131145
/// - This treats `self` as if it were evaluated **outside** the `unsafe {...}` block.
132146
/// - $fn can **NOT** be an expression or a qualified path (which doesn't work in standard methods
133147
/// calls anyways), but only an identifier.
148+
/// ```compile_fail
149+
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/arg.rs")]
150+
/// ```
134151
#[macro_export]
135152
macro_rules! unsafe_method {
136153
($self:expr, $fn:ident $(, $arg:expr)* ) => {
@@ -143,11 +160,25 @@ macro_rules! unsafe_method {
143160
unreachable!()
144161
} else {
145162
#[allow(unsafe_code)]
163+
//@TODO: for unsafe_fn, too:
164+
//
165+
// If $self or any $arg include `unsafe {...}`, that would trigger "unused_unsafe".
166+
//
167+
// Unfortunately, because of this, we can't detect code where unsafe_fn! or
168+
// unsafe_method! is not needed at all. For example, if a function/method use to be
169+
// `unsafe`, and it stopped being so.
170+
#[allow(unused_unsafe)]
146171
unsafe { $self. $fn ( $( $arg ),* ) }
147172
}
148173
}
149174
};
150175
}
176+
/// ```compile_fail,E0133
177+
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/arg.rs")]
178+
/// ```
179+
#[cfg(doctest)]
180+
pub const _: () = {};
181+
151182
//-------------
152183

153184
/// Set a value of a `static mut` variable or its (sub...-)field, but isolate `unsafe {...}` only to
@@ -189,7 +220,7 @@ macro_rules! unsafe_method {
189220
/// *_mref = [false];
190221
/// _mref[ 0 ] = true;
191222
///
192-
/// // Read accesss OK:
223+
/// // Read access OK:
193224
/// let _b: bool = { unsafe {&mut *mptr} }[ 0 ];
194225
/// // Mut access - bad: The following refused:
195226
/// //

violations_coverage/fn_add_four/Cargo.lock

Lines changed: 0 additions & 14 deletions
This file was deleted.

violations_coverage/fn_add_four/Cargo.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

violations_coverage/fn_add_four/src/main.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../unsafe_method/some_args/arg.rs

violations_coverage/method_one_postfix_param/Cargo.lock

Lines changed: 0 additions & 14 deletions
This file was deleted.

violations_coverage/method_one_postfix_param/Cargo.toml

Lines changed: 0 additions & 7 deletions
This file was deleted.

violations_coverage/method_one_postfix_param/src/main.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use prudent::unsafe_method;
2+
3+
fn main() {
4+
let _ = unsafe_method!(1u8, unchecked_add, 0u8.unchecked_add(0));
5+
}

0 commit comments

Comments
 (0)