Open
Conversation
nbdd0121
reviewed
Mar 31, 2026
nbdd0121
requested changes
Mar 31, 2026
d15550a to
d4d61d9
Compare
Recognize `{static,const,build}_assert!` as special items, preferring
explicit `#[klint::diagnostic_item]` annotations and keeping a
conservative kernel-specific fallback for older trees.
The fallback resolves known macro paths structurally, extending the
existing diagnostic-item discovery pattern so later lints can identify
assert macros semantically.
Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
Detect build-time assertions that can use a stronger form and suggest the strongest applicable one. The new lint classifies assertion conditions as `build_assert!`, `const_assert!`, or `static_assert!` depending on whether they depend on runtime values, generic compile-time context, or only closed compile-time context. Using that classification, it warns for: - `build_assert!` -> `const_assert!` - `build_assert!` -> `static_assert!` - `const_assert!` -> `static_assert!` This keeps diagnostics aligned with the documented assertion hierarchy by preferring the earliest and most robust assertion form available. Add `#![allow(klint::assert_hierarchy)]` in `tests/ui/build_error.rs` and re-bless the test so that test remains focused. Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
UI test coverage includes: - literals - `const` generics - wrapper macros - local `const`-only helpers - non-`const` helpers - `match` expressions - comments around macro commas - outer local bindings - local bindings inside the asserted condition - `const_assert!` sites that can be changed to `static_assert!` Signed-off-by: Mohamad Alsadhan <mo@sdhn.cc>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was split out from #13
Add a new
assert_hierarchylint that suggests to use stronger build-time assertion forms when possible.The lint implements the assertion ordering:
static_assert!>const_assert!>build_assert!and warns when a weaker assertion can be replaced with a stronger one.
e.g. cases where
build_assert!can be replaced withstatic_assert!build_assert!can be replaced withconst_assert!const_assert!can be replaced withstatic_assert!Implementation Notes
This change adds a dedicated late HIR lint in
src/assert_hierarchy.rs.The lint:
build_assert!andconst_assert!via diagnostic-item support and macro backtracesBuild,ConstorStaticThe condition classifier handles:
constitems, assocconsts, andconstparamsconst fnandconst-usable method callstypecksubstitutionsThis lint does not use any of the mono-graph, interprocedural summary and propagation machinery from the
build_assert_not_inlinedwork in #13. It is only local and source-oriented; it inspects the asserted condition itself, along with local expression structure, resolution,typeckinformation,const-function legality, and generic dependence.Other Changes/Refactors
src/diagnostic_items/out_of_band.rs.Extract out(reverted in favour of using a derive diagnostic).ClosureDiagfrominfalliable_allocation.rsto be reused for this lintklint:assert_hierarchyinbuild_error.rs, so the output isn't polluted by this lint.Tests
tests/ui/assert_hierarchy.rsUI test coverage includes:
constgenericsconst fnhelpersconst_assert!->static_assert!Docs
Add documentation in
doc/assert_hierarchy.mdand the corresponding entry in README "Implemented Lints" section.Lint output on Linux kernel tree (Rust-for-Linux/linux@3a2486c)
(same cases as #13 btw)