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

clippy::transmute_float_to_int in const context should not trigger when rust-version is less than 1.83 in Cargo.toml #13383

Closed
tspiteri opened this issue Sep 11, 2024 · 6 comments · Fixed by rust-lang/rust#130305
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@tspiteri
Copy link
Contributor

Summary

When rust-version is less than 1.83, the suggested code does not compile.

Lint Name

clippy::transmute_float_to_int

Reproducer

Cargo.toml contains:

rust-version = "1.82"

I tried this code:

pub const fn f32_to_u32(f: f32) -> u32 {
    unsafe { core::mem::transmute(f) }
}

I saw this happen:

warning: transmute from a `f32` to a `u32`
 --> src/lib.rs:2:14
  |
2 |     unsafe { core::mem::transmute(f) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f.to_bits()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_float_to_int
  = note: `#[warn(clippy::transmute_float_to_int)]` on by default

I expected to see this happen:
No warning as rust 1.82.0 does not support f32::to_bits in const context.

Version

rustc 1.83.0-nightly (0ee7cb5e3 2024-09-10)
binary: rustc
commit-hash: 0ee7cb5e3633502d9a90a85c3c367eccd59a0aba
commit-date: 2024-09-10
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Additional Labels

No response

@tspiteri tspiteri added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Sep 11, 2024
@tspiteri tspiteri changed the title clippy::transmute_float_to_int in const context should not trigger when rust-version is less than 1.82 in Cargo.toml clippy::transmute_float_to_int in const context should not trigger when rust-version is less than 1.83 in Cargo.toml Sep 11, 2024
@tspiteri
Copy link
Contributor Author

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Sep 11, 2024
@y21
Copy link
Member

y21 commented Sep 11, 2024

transmute_float_to_int already checks if it's in a const context, so it shouldn't fire here regardless of the version:

(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {

The reproducer also doesn't have any warnings: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=87b21a87034fd451c9278971fe6971bb

@tspiteri
Copy link
Contributor Author

tspiteri commented Sep 11, 2024

In rust 1.83, f32::to_bits will be usable on const context.

And it seems that the playground clippy is an older version than the one I used above.

@tspiteri
Copy link
Contributor Author

It seems that in the rust-clippy repo it is conditioned on const_context as pointed out above,but in the rust repo it is not:

https://github.com/rust-lang/rust/blob/1f51450c6826cc4c3d1089cd3f57089a14f49190/src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs#L20

@y21
Copy link
Member

y21 commented Sep 12, 2024

I see. Yeah, looks like those checks were removed in rust-lang/rust#129555 which was merged after the last rust->clippy sync, so that makes sense for why the bug isn't present in the clippy repo yet

@tspiteri
Copy link
Contributor Author

Since this is in the rust repo, I created rust-lang/rust#130305 in the rust repo for it.

rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 13, 2024
Rollup merge of rust-lang#130305 - tspiteri:clippy-msrv-for-const_float_bits_conv, r=flip1995

Clippy: consider msrv for const context for const_float_bits_conv

When `const_float_bits_conv` was stabilized for 1.83.0, clippy lints started to be triggered in const context ignoring MSRV. This PR makes the lints trigger in const context only when the MSRV meets 1.83.0.

Fixes rust-lang/rust-clippy#13383.
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Sep 14, 2024
…onv, r=flip1995

Clippy: consider msrv for const context for const_float_bits_conv

When `const_float_bits_conv` was stabilized for 1.83.0, clippy lints started to be triggered in const context ignoring MSRV. This PR makes the lints trigger in const context only when the MSRV meets 1.83.0.

Fixes rust-lang/rust-clippy#13383.
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-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants