Skip to content

fn_to_numeric_cast incorrect suggestion #3896

Closed
@phansch

Description

@phansch

Given the following code:

#![warn(clippy::fn_to_numeric_cast)]

fn fn_with_fn_args(f: fn(i32) -> i32) -> i32 {
    f as i32
}

Clippy gives the following suggestion:

error: casting function pointer `f` to `i32`, which truncates the value
  --> $DIR/fn_to_numeric_cast.rs:53:5
   |
LL |     f as i32
   |     ^^^^^^^^ help: try: `f as usize`

Applying this, results in a compilation error:

error[E0308]: mismatched types
  --> tests/ui/fn_to_numeric_cast.fixed:53:5
   |
52 | fn fn_with_fn_args(f: fn(i32) -> i32) -> i32 {
   |                                          --- expected `i32` because of return type
53 |     f as usize
   |     ^^^^^^^^^^ expected i32, found usize

I'm not really sure if Clippy can suggest to change the return type of the function as well. Or maybe Clippy should mark this suggestion as MaybeIncorrect if the cast is the return value of the function?

(Split off from #3630)

edit: Also make sure this is fixed for fn_to_numeric_cast_with_truncation

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions