Skip to content

Commit d15550a

Browse files
committed
Add docs for assert_hierarchy lint
Add user-facing documentation for the new `assert_hierarchy` lint in `doc/assert_hierarchy.md`. Including: - `build_assert!` uses that should become `const_assert!` - `build_assert!` and `const_assert!` uses that should become `static_assert!` - the assertion ordering of `static_assert!`, `const_assert!`, and `build_assert!` - the distinction from runtime-dependent `build_assert!` uses Add a corresponding entry in the "Implemented Lints" section of README. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
1 parent 8cd7a2a commit d15550a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ If you want to check it out, you can opt into it with `-Dklint::atomic_context`.
5454
* [`build_error` checks](doc/build_error.md)
5555
* [Stack frame size check](doc/stack_size.md)
5656
* [Prelude check](doc/not_using_prelude.md)
57+
* [Assertion hierarchy](doc/assert_hierarchy.md)

doc/assert_hierarchy.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!--
2+
SPDX-License-Identifier: MIT OR Apache-2.0
3+
-->
4+
5+
# `assert_hierarchy`
6+
7+
This lint warns when an assertion can use a stronger compile-time assertion form.
8+
9+
The preference order is:
10+
11+
1. `static_assert!`
12+
2. `const_assert!`
13+
3. `build_assert!`
14+
15+
`static_assert!` is preferred when the condition is fully closed over compile-time context.
16+
`const_assert!` is preferred when the condition is valid in a generic-aware const context but still
17+
depends on generics or expression-local bindings. `build_assert!` is only needed once the condition
18+
depends on variables or other non-const context.
19+
20+
## `build_assert!` To `static_assert!`
21+
22+
These trigger the lint with a `static_assert!` suggestion:
23+
24+
```rust
25+
fn literal_const_only() {
26+
build_assert!(1 < LIMIT);
27+
}
28+
```
29+
30+
```rust
31+
fn wrapper_const_only() {
32+
forward_build_assert!(OFFSET < LIMIT);
33+
}
34+
```
35+
36+
Macro-generated assertions can also trigger:
37+
38+
```rust
39+
macro_rules! forward_const_check {
40+
($expr:expr $(,)?) => {
41+
build_assert!($expr);
42+
let _x = 0usize;
43+
};
44+
}
45+
46+
fn f<const N: usize>() {
47+
forward_const_check!(N > 0);
48+
}
49+
```
50+
51+
## `build_assert!` To `const_assert!`
52+
53+
These trigger the lint with a `const_assert!` suggestion:
54+
55+
```rust
56+
fn const_generic_only<const N: usize>() {
57+
build_assert!(OFFSET < N);
58+
}
59+
```
60+
61+
```rust
62+
const fn helper<const N: usize>() -> usize {
63+
N - 1
64+
}
65+
66+
fn const_fn_helper_only<const N: usize>() {
67+
build_assert!(helper::<N>() < N);
68+
}
69+
```
70+
71+
## `const_assert!` To `static_assert!`
72+
73+
This also applies to `const_assert!` when it does not actually need generic-aware const context:
74+
75+
```rust
76+
fn const_assert_static_only() {
77+
const_assert!(1 < LIMIT);
78+
}
79+
```
80+
81+
## Cases That Do Not Trigger
82+
83+
These do not trigger `assert_hierarchy`:
84+
85+
```rust
86+
fn const_assert_generic<const N: usize>() {
87+
const_assert!(OFFSET < N);
88+
}
89+
```
90+
91+
```rust
92+
fn runtime_direct(offset: usize, n: usize) {
93+
build_assert!(offset < n);
94+
}
95+
```
96+
97+
```rust
98+
fn non_const_fn_helper<const N: usize>() {
99+
build_assert!(helper::<N>() < N);
100+
}
101+
```

0 commit comments

Comments
 (0)