Skip to content

Re-implementing float abs with no_std #10634

Closed
@Rudxain

Description

@Rudxain

Summary

A warning is triggered when trying to define an abs fn for floats, even when no_std is used. This is annoying, because f{32, 64}::abs depends on std, not core

Lint Name

suboptimal_flops

Reproducer

I tried this code (in lib.rs):

#![no_std]
#![warn(clippy::suboptimal_flops)]

pub fn abs(x: f64) -> f64 {
    if x < 0.0 {
        -x
    } else {
        x
    }
}

(BTW, I'm aware it doesn't handle -0.0)

I saw this happen:

warning: manual implementation of `abs` method
 --> src/lib.rs:5:5
  |
5 | /     if x < 0.0 {
6 | |         -x
7 | |     } else {
8 | |         x
9 | |     }
  | |_____^ help: try: `x.abs()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![warn(clippy::suboptimal_flops)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

warning: `tmp` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

I expected to see this happen:

    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

Version

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-unknown-linux-gnu
release: 1.68.2
LLVM version: 15.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions