rust-lang / rust-lang/rust-clippy
Lint for `floating64 as f32` losing the sign of NAN
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Check for any as cast between floats that are not followed by copysign
Advantage
Help libraries that are trying to preserve NAN signage.
From toml-rs/toml#637
I learned recently in rust-lang/miri#3139 that
asproduces NaN with a nondeterministic sign, and separately that the sign of f64::NAN is not specified (may be a negative NaN). As of rust-lang/rust#116551 Miri has begun intentionally exercising these cases.This PR fixes places where
-nanwould incorrectly be deserialized as a positive NaN,nanor+nanwould be deserialized as a negative NaN, negative NaN would be serialized asnan, or positive NaN would be serialized as-nan. It adds tests to confirm the expected sign of NaN values, and improves existing tests related to NaN.
Drawbacks
This would be noisy and not apply to most people as they won't care what the sign of NAN is. I'd recommend this be a pedantic lint.
Example
let foo = f32::NAN.copysign(1); // this is just to avoid the lint for #11717
let foo = foo as f64;
Could be written as:
let foo = f32::NAN.copysign(1); // this is just to avoid the lint for #11717
let foo = (foo as f64).copysign(if foo.is_sign_positive() { 1.0 } else { -1.0 }]);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The payload names no files or tests; start by tracing Clippy's existing float-cast lint entry points and test patterns. Compare the as cast and copysign examples, and consider the requested pedantic behavior. Done means the lint covers the described NaN-sign cases while allowing the shown sign-preserving form.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100