rust-lang / rust-lang/rust-clippy

Lint for `floating64 as f32` losing the sign of NAN

Open
#11,718 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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 as produces 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 -nan would incorrectly be deserialized as a positive NaN, nan or +nan would be deserialized as a negative NaN, negative NaN would be serialized as nan, 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.