Skip to content

Lint for sign-ambiguous NAN constants #11720

Open
@epage

Description

@epage

What it does

Check if a f32::NAN or f64::NAN does not have a sign explicitly set (a more general version of #11717)

Advantage

In reviewing toml-rs/toml#637, I found a ambiguously-signed NAN was still left in. This would help catch that.

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 very noisy and most people won't care. This would likely be a pedantic lint

Example

let foo = f64::NAN;

Could be written as:

let foo = f64::NAN.copysign(1.0);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions