Skip to content
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

Trying to return c_void should suggest to return () instead #100972

Open
CAD97 opened this issue Aug 24, 2022 · 2 comments
Open

Trying to return c_void should suggest to return () instead #100972

CAD97 opened this issue Aug 24, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Aug 24, 2022

e.g. this irlo thread was likely the result of someone translating a void-returning C function to a c_void-returning Rust function.

Given the following:

pub fn example() -> core::ffi::c_void {
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:1:21
  |
1 | pub fn example() -> core::ffi::c_void {
  |        -------      ^^^^^^^^^^^^^^^^^ expected enum `c_void`, found `()`
  |        |
  |        implicitly returns `()` as its body has no tail or `return` expression

Ideally, this should also contain a help message along the lines of

help = returning void in C is the same as returning () in Rust

and a suggestion to remove the -> c_void.


It might be reasonable to have this as a dedicated warning lint for -> c_void either instead of or in addition to help on the type mismatch error, as writing bindings to extern "C" { fn foo() -> c_void; } is wrong but won't get a type mismatch error. This probably should go straight to a rustc lint rather than in clippy due to the practically-100% applicability.


It might also be beneficial to do more thorough linting of misuse of c_void (basically any use that isn't *mut c_void or *const c_void (or maybe ptr::NonNull<c_void>), but that is a separate issue from teaching in this error.

@CAD97 CAD97 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 24, 2022
@czzrr
Copy link
Contributor

czzrr commented Aug 31, 2022

@rustbot claim

@coreh
Copy link

coreh commented Oct 19, 2022

I think ideally a return type of void (that doesn't exist in the current scope) should also trigger this and offer an autofix... Often when I'm tired after a long day of work and I switch languages to Rust from C++ or TypeScript I find myself mixing up the languages 😅

Screenshot 2022-10-13 at 23 59 33

@czzrr czzrr removed their assignment Nov 27, 2022
@fmease fmease added the D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. label Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants