rust-lang / rust-lang/rust-clippy

Generic framework for detecting mis-use of C-APIs with `init` functions

Open
#4,904 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint C-question L-correctness
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:

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.