Description
I developed a proof of concept MIR analysis that detects if an init
function is not called before other functions of an API. This is a common pattern in C, you need to call an init
function that sets up all kinds of global state, and only then you can call other functions of the API. While the correct solution would be to make the init
function return a handle that is then passed to all other functions, this is the status we have.
Right now the analysis is very much hardcoded to the way you can annotate your code with init
and a single other API function, but I could definitely generalize this to some arbitrary #[clippy::something]
annotations that users could mark their APIs with.
Is there interest in such a lint?
Prototype impl: https://github.com/rust-lang/rust-clippy/blob/init_before_foo/clippy_lints/src/init_before_foo.rs
Example tests:
- https://github.com/rust-lang/rust-clippy/blob/init_before_foo/tests/ui/init_before_foo.rs
- https://github.com/rust-lang/rust-clippy/blob/init_before_foo/tests/ui/init_before_foo2.rs
- https://github.com/rust-lang/rust-clippy/blob/init_before_foo/tests/ui/init_before_foo3.rs
- https://github.com/rust-lang/rust-clippy/blob/init_before_foo/tests/ui/init_before_foo4.rs
The analysis also has a bunch of false alarms (false positives) that would need to be eliminated before it becomes really useful, but I believe it detects all mis-uses (so it has no false negatives).