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

On 32-bit, usize as f64 recommends f64::from(usize) #3689

Closed
tspiteri opened this issue Jan 24, 2019 · 2 comments · Fixed by #8778
Closed

On 32-bit, usize as f64 recommends f64::from(usize) #3689

tspiteri opened this issue Jan 24, 2019 · 2 comments · Fixed by #8778
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy 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

@tspiteri
Copy link
Contributor

tspiteri commented Jan 24, 2019

On i686, the cast_lossless lint recommends using f64::from(len) instead of len as f64 where len is of type usize. This would change the behaviour on x86_64 fail to compile.

@flip1995 flip1995 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 Jan 24, 2019
@repi
Copy link

repi commented Apr 24, 2022

We ran into this as well when compiling to wasm32-unknown-unknown:

warning: casting `usize` to `f64` may become silently lossy if you later change the type
  --> world-benchmark/src/lib.rs:94:28
   |
94 |                         / (self.ns_history.len() as f64 * f64::from(n))
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `f64::from(self.ns_history.len())`
   |
   = note: requested on the command line with `-W clippy::cast-lossless`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless

following this suggestion leads to:

error[E0277]: the trait bound `f64: std::convert::From<usize>` is not satisfied
  --> world-benchmark/src/lib.rs:94:28
   |
94 |                         / (f64::from(self.ns_history.len()) * f64::from(n))
   |                            ^^^^^^^^^ the trait `std::convert::From<usize>` is not implemented for `f64`
   |
   = help: the following implementations were found:
             <f64 as std::convert::From<f32>>
             <f64 as std::convert::From<i16>>
             <f64 as std::convert::From<i32>>
             <f64 as std::convert::From<i8>>
           and 135 others

For more information about this error, try `rustc --explain E0277`.

would be great to fix this in the lint, we do find it useful and works well for all other code we have.

@Serial-ATA
Copy link
Contributor

@rustbot label +good-first-issue

@rustbot rustbot added the good-first-issue These issues are a good way to get started with Clippy label Apr 24, 2022
bors added a commit that referenced this issue May 6, 2022
Fix `cast_lossless` to avoid warning on `usize` to `f64` conversion.

Previously, the `cast_lossless` lint would issue a warning on code that
converted a `usize` value to `f64`, on 32-bit targets.

`usize` to `f64` is a lossless cast on 32-bit targets, however there is
no corresponding `f64::from` that takes a `usize`, so `cast_lossless`'s
suggested replacement does not compile.

This PR disables the lint in the case of casting from `usize` or `isize`.

Fixes #3689.

changelog: [`cast_lossless`] no longer gives wrong suggestion on usize,isize->f64
@bors bors closed this as completed in 6ff77b9 May 6, 2022
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 good-first-issue These issues are a good way to get started with Clippy 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

Successfully merging a pull request may close this issue.

5 participants