Skip to content

Commit 81a0bdc

Browse files
Cleanup
1 parent c5735da commit 81a0bdc

File tree

8 files changed

+66
-143
lines changed

8 files changed

+66
-143
lines changed

README.md

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,38 @@
22
results](https://github.com/peter-lyons-kehl/prudent/actions/workflows/main.yml/badge.svg)](https://github.com/peter-lyons-kehl/prudent/actions)
33

44
# Summary
5-
`prudent` helps you minimize the amount of Rust code that is marked as `unsafe`.
5+
`prudent` helps you minimize/isolate parts of Rust `unsafe` expressions/statements.
66

77
- ergonomic (as much as possible)
8-
- obvious
9-
- lightweight (no dependencies, no procedural macros - fast build)
8+
- lightweight (no dependencies, no procedural macros - fast build, `rust-analyzer`-friendly)
109
- zero-cost (for binary size, speed and memory), verified in compile time
1110

12-
# const-friendly
13-
Results of `prudent`'s macro invocations are `const` (if the original invocation/expression would
14-
also be `const`).
15-
16-
# Lint-like check for unsafe_method
17-
18-
# Quality assurance
11+
# API and examples
12+
Following are all the positive examples. They are also run by the above [GitHub Actions] as
13+
[doctests](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html).
1914

20-
Checks and tests are run by [GitHub Actions]. See
21-
[results](https://github.com/peter-lyons-kehl/prudent/actions). All tests <!--scripts--> run on
22-
Alpine Linux (without `libc`, in a `rust:1.87-alpine` container)<!-- and are POSIX-compliant-->:
15+
For negative examples, see documentation of each `prudent` macro.
2316

24-
- `rustup component add clippy rustfmt`
25-
- `cargo clippy`
26-
- `cargo fmt --check`
27-
- `cargo doc --no-deps --quiet`
28-
- `cargo test`
29-
- `cargo test --release`
30-
- with [`MIRI`]
31-
- `rustup install nightly --profile minimal`
32-
- `rustup +nightly component add miri`
33-
- `cargo +nightly miri test`
17+
## unsafe_fn
18+
```rust
19+
use prudent::unsafe_fn;
3420

35-
## Verification of expected errors
36-
- Error code validation: Where possible, expected error numbers are validated with `cargo +nightly
37-
test`, ([The Rustdoc book > Unstable features > Error numbers for compile-fail
38-
doctests](https://doc.rust-lang.org/rustdoc/unstable-features.html#error-numbers-for-compile-fail-doctests)).
39-
The error codes are validated by [GitHub Actions](.github/workflows/main.yml), see
40-
[results](https://github.com/prudent-rs/prudent/actions). Error code validation requires `nightly`
41-
Rust toolchain. See also [`src/linted_with_tests.rs`](src/linted_with_tests.rs) for expected
42-
compilation error codes.
43-
- Error output validation: Some lint violations don't have a special error code. So we validate the
44-
error message in
45-
[violations_coverage/verify_error_messages/](violations_coverage/verify_error_messages/) with
46-
[dtolnay/trybuild](https://github.com/dtolnay/trybuild/)
47-
[crates.io/crates/trybuild](https://crates.io/crates/trybuild).
21+
const unsafe fn unsafe_fn_no_args() {}
22+
const unsafe fn unsafe_fn_one_arg(b: bool) -> bool { b }
23+
const unsafe fn unsafe_fn_two_args(_: bool, u: u8) -> u8 { u }
4824

49-
# API and examples
50-
Following are all the positive examples. They are also run by the above [GitHub Actions] as
51-
[doctests](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html).
25+
const _: () = unsafe_fn!(unsafe_fn_no_args);
26+
const _: bool = unsafe_fn!(unsafe_fn_one_arg=> true);
27+
const _: u8 = unsafe_fn!(unsafe_fn_two_args=> true, 0);
28+
fn main() {}
29+
```
5230

53-
For negative examples, which catch unintended `unsafe` functions/expressions/access, see
54-
documentation of each `prudent` macro.
31+
## unsafe_method
5532

33+
### self by value
5634
```rust
57-
let _todo = ();
58-
//# use prudent::unsafe_method;
59-
//const _: u8 = unsafe_method!(~expect_unsafe ~allow_unsafe 1u8, unchecked_add, 0);
35+
use prudent::unsafe_method;
36+
const _: u8 = unsafe_method!( 1u8 =>@ unchecked_add => 0 );
6037
```
6138

6239
```rust
@@ -425,6 +402,36 @@ fn main() {
425402
}
426403
```
427404

405+
# const-friendly
406+
Results of `prudent`'s macro invocations are `const` (if the original invocation/expression would
407+
also be `const`).
408+
409+
# Quality assurance
410+
## Continuous integration
411+
[GitHub Actions] runs on Alpine Linux (without `libc`). See [the
412+
reports](https://github.com/peter-lyons-kehl/prudent/actions).
413+
414+
- `cargo test`
415+
- `cargo clippy` for linting
416+
- [`MIRI`]
417+
418+
## Verification of expected errors
419+
- Error code validation: Where possible, expected error numbers are validated with `cargo +nightly
420+
test`, ([The Rustdoc book > Unstable features > Error numbers for compile-fail
421+
doctests](https://doc.rust-lang.org/rustdoc/unstable-features.html#error-numbers-for-compile-fail-doctests)).
422+
The error codes are validated by [GitHub Actions](.github/workflows/main.yml), see
423+
[results](https://github.com/prudent-rs/prudent/actions). Error code validation requires `nightly`
424+
Rust toolchain. See also [`src/linted_with_tests.rs`](src/linted_with_tests.rs) for expected
425+
compilation error codes.
426+
- Error output validation: Some lint violations don't have a special error code. So we validate the
427+
error message in
428+
[violations_coverage/verify_error_messages/](violations_coverage/verify_error_messages/) with
429+
[dtolnay/trybuild](https://github.com/dtolnay/trybuild/)
430+
[crates.io/crates/trybuild](https://crates.io/crates/trybuild).
431+
432+
## unsafe_method
433+
TODO
434+
428435
# Details
429436

430437
`prudent` helps both authors, reviewers and all of us:
@@ -467,16 +474,21 @@ However,
467474

468475
`prudent` is `no-std`-compatible. It doesn't need allocation either.
469476

477+
All of `prudent`'s "positive" functionality works on `stable` Rust (minimum version 1.39). However,
478+
if you use `assert_unsafe_methods` feature to verify that `unsafe_method` is applied only to methods
479+
that are indeed `unsafe`, that requires
480+
- `nightly`, and
481+
- access to set `RUSTFLAGS="-Znext-solver=globally"`. That is unfortunately not possible for
482+
doctests. See, and give thumbs up to, [Related issues](#Related-issues).
483+
470484
## Always forward compatible
471485

472-
`prudent` is planned to be always below version `1.0`. So <!--stable (**even**-numbered) versions-->
473-
it will be forward compatible. (If a need ever arises for big incompatibility, that can go to a new
474-
crate.)
486+
`prudent` is planned to be forward compatible. (If a need ever arises for big incompatibility, that
487+
can go to a new crate.)
475488

476-
That allows you to specify `prudent` as a dependency with version `0.*`, which will match ANY
477-
**major** versions (below `1.0`, of course). That will match the newest <!-- (**even**-numbered
478-
major)-->
479-
<!-- stable--> version (available for your Rust) automatically.
489+
That allows `prudent` to be always below version `1.0`. Then you can specify `prudent` as a
490+
dependency with version `0.*`, which will match ANY **major** versions (below `1.0`, of course).
491+
That will match the newest version (available for your Rust) automatically.
480492

481493
This is special only to `0.*` - it is **not** possible to have a wildcard matching various **major**
482494
versions `1.0` or higher.

coverage_positive/fn.rs

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

coverage_positive/md-mut_ref.rs

Whitespace-only changes.

coverage_positive/md-shared_ref.rs

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

coverage_positive/md-value.rs

Whitespace-only changes.

src/backend.rs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
11
//! "backend" functionality (anything else than linted macros)
2-
//! - macros that don't need `warn/deny/forbid/allow/expect` lint rules
3-
//! - non-macro functionality.
2+
//! - a few macros macros; and
3+
//! - any non-macro functionality.
44
5-
/// Implementation may change to support a range of versions etc.
6-
///
7-
/// NOT a part of public API - internal.
8-
#[doc(hidden)]
9-
pub const fn verify_linted_version(linted_version: &'static str) {
10-
// https://github.com/rust-lang/rust/issues/143874 we can't (yet) use == on &str constants, not
11-
// even matches! macro.
12-
//
13-
//assert!( linted_version==env!("CARGO_PKG_VERSION") );
14-
//
15-
//assert!( matches!(linted_version, env!("CARGO_PKG_VERSION")) );
16-
let linted_version = linted_version.as_bytes();
17-
//
18-
//assert!( matches!(linted_version, env!("CARGO_PKG_VERSION").as_bytes()) );
19-
//
20-
//Can't yet:
21-
//
22-
//assert!( linted_version == env!("CARGO_PKG_VERSION").as_bytes() );
23-
//
24-
// Can't yet:
25-
//
26-
// assert!( matches!(&linted_version[0..5], b"0.0.3") );
27-
//
28-
// Can now:
29-
//
30-
//assert!( matches!(linted_version, b"0.0.3-beta") );
31-
assert!(matches!(linted_version, [b'0', b'.', b'0', b'.', b'3', ..]));
32-
}
33-
34-
/// For casting/ensuring that a user-provided function is unsafe. Used by `unsafe_fn`.
5+
/// For casting/ensuring that a user-provided function is unsafe. Used by [crate::unsafe_fn].
356
///
367
/// Internal - NOT a part of public API.
378
#[doc(hidden)]

src/lib.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! # Examples
2-
#![doc = internal_coverage_positive!() ]
31
#![doc = include_str!("../README.md")]
42
//!
53
//! Implementation notes of macros ARE a part of the documentation. Why?
@@ -59,47 +57,6 @@
5957
#[cfg(doc)]
6058
extern crate alloc;
6159

62-
#[cfg(feature = "assert_unsafe_methods")]
63-
/// Enable a necessary nightly feature IF prudent is configured to use it.
64-
#[macro_export]
65-
macro_rules! top_header_assert_unsafe_methods {
66-
() => {
67-
"#![feature(type_alias_impl_trait)]"
68-
};
69-
}
70-
#[cfg(not(feature = "assert_unsafe_methods"))]
71-
/// Enable a necessary nightly feature IF prudent is configured to use it.
72-
#[macro_export]
73-
macro_rules! top_header_assert_unsafe_methods {
74-
() => {};
75-
}
76-
77-
#[doc(hidden)]
78-
#[macro_export]
79-
macro_rules! internal_coverage_positive {
80-
(
81-
) => {
82-
$crate::internal_coverage_positive!(
83-
"# unsafe_fn" -> "../coverage_positive/fn.rs",
84-
"# unsafe_method\n## unsafe_method > self: shared reference" -> "../coverage_positive/md-shared_ref.rs"
85-
)
86-
};
87-
(
88-
$( $description:literal -> $file:literal ),*
89-
) => {
90-
::core::concat!(
91-
$(
92-
$description,
93-
"\n```\n",
94-
::core::include_str!($file),
95-
// just in case the file doesn't end with a new line, inject it anyway:
96-
"\n```\n",
97-
)*
98-
"\n"
99-
)
100-
};
101-
}
102-
10360
pub mod backend;
10461

10562
/// "Frontend" macros.

violations_coverage/in_crate/src/bin/frontend_linted.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)