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

fn_to_numeric_cast incorrect suggestion #3896

Closed
phansch opened this issue Mar 19, 2019 · 1 comment
Closed

fn_to_numeric_cast incorrect suggestion #3896

phansch opened this issue Mar 19, 2019 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@phansch
Copy link
Member

phansch commented Mar 19, 2019

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

@phansch phansch added C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Mar 19, 2019
@phansch phansch self-assigned this Apr 15, 2019
@phansch phansch removed their assignment Apr 30, 2019
@ghost
Copy link

ghost commented Jul 4, 2019

I think MaybeIncorrect is the way to go. Even if you fixed the signature to return usize, you'd now have to fix all the places where the function is used which isn't possible.

phansch added a commit to phansch/rust-clippy that referenced this issue Jul 31, 2019
At least for now so that `cargo fix --clippy` is not causing problems
with this lint.

cc rust-lang#3630, rust-lang#3896
bors added a commit that referenced this issue Aug 1, 2019
…p1995

Mark `fn_to_numeric_cast` lints as MaybeIncorrect

At least for now so that `cargo fix --clippy` is not causing problems
with this lint. See #3896 for the remaining problems with the suggestions of this lint.

changelog: none

cc #3630, #3896
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

2 participants