rust-lang / rust-lang/rust-clippy

Warn on pointer cast with type inference

Open
#7,028 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Lint should warn when casts using type inference are being used on pointers types.

Recently we had a bug on a FFI boundary where a *const T was being cast with self.inner() as *mut _ when calling a C function with signature sys::foo(T**), but inner function is defined as fn inner() -> *const T.
This lead to passing a pointer as pointer to pointer, and the application crashing at runtime.

Categories (optional)
  • Kind: clippy::correctness, clippy::pedantic

Original code might be buggy. Intent is to have a mechanism that warns or errors when such a code is found on particular scenarios, specifically for our case in code involving the FFI boundary.

Drawbacks

Might get annoying for most use cases, thats why it was suggested also as pedantic linter.

Example
sys::foo(self.inner() as *mut _);

Could be written as:

sys::foo(self.inner() as *mut T);

In this case code would fail to compile, but provides us the chance to catch and fix the error at compile time.

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

The issue names no implementation file, test, or entry point. Start by locating Clippy's existing cast-related lint registration and tests, then determine how pointer casts using type inference are represented. Done means the shown pointer cast produces a warning or error with an appropriate lint category, while explicit pointer types remain usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.