Skip to content

Warn on pointer cast with type inference #7028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tiago-lqt opened this issue Apr 4, 2021 · 2 comments
Open

Warn on pointer cast with type inference #7028

tiago-lqt opened this issue Apr 4, 2021 · 2 comments
Labels
A-lint Area: New lints

Comments

@tiago-lqt
Copy link

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.

@tiago-lqt tiago-lqt added the A-lint Area: New lints label Apr 4, 2021
@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label May 3, 2021
@panosfol
Copy link
Contributor

panosfol commented Jul 3, 2023

Any news on this? Im interested in working on this.

@Centri3
Copy link
Member

Centri3 commented Jul 3, 2023

There is already #10567 which implements this, but only the *mut _ and *const _ cases. Maybe a lint to detect any such cases could be nice.

@Alexendoo Alexendoo removed the good-first-issue These issues are a good way to get started with Clippy label Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

5 participants