rust-lang / rust-lang/rust-clippy
Generic framework for detecting mis-use of C-APIs with `init` functions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
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).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the prototype in clippy_lints/src/init_before_foo.rs and review the four example tests in tests/ui/init_before_foo.rs through init_before_foo4.rs. Examine how the current annotations and MIR analysis work, then determine the scope for arbitrary API annotations. Done means the analysis is generalized and its reported false positives are addressed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100