Skip to content

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

Description

@CAD97

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions