Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::suboptimal_flops: exponent for bases 2 and e can be computed more accurately
What it meansLooks for floating-point expressions that can be expressed using built-in methods to improve both accuracy and performance. It is in the nursery group and off by default.
What you see
warning: exponent for bases 2 and e can be computed more accurately --> main.rs:6:9 | 6 | let _ = (2f32).powf(a); | ^^^^^^^^^^^^^^ help: consider using: `a.exp2()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#suboptimal_flops = note: `-W clippy::suboptimal-flops` implied by `-W clippy::nursery`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Negatively impacts accuracy and performance.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
use std::f32::consts::E;
let a = 3f32;
let _ = (2f32).powf(a);
let _ = E.powf(a);
let _ = a.powf(1.0 / 2.0);
let _ = a.log(2.0);
let _ = a.log(10.0);
let _ = a.log(E);
let _ = a.powf(2.0);
let _ = a * 2.0 + 4.0;
let _ = if a < 0.0 {
-a
} else {
a
};
let _ = if a < 0.0 {
a
} else {
-a
};
Write this instead
use std::f32::consts::E;
let a = 3f32;
let _ = a.exp2();
let _ = a.exp();
let _ = a.sqrt();
let _ = a.log2();
let _ = a.log10();
let _ = a.ln();
let _ = a.powi(2);
let _ = a.mul_add(2.0, 4.0);
let _ = a.abs();
let _ = -a.abs();
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
suboptimal_flops = "warn"In code
#![warn(clippy::suboptimal_flops)]One run
cargo clippy -- -W clippy::suboptimal-flopsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Looks for floating-point expressions that can be expressed using built-in methods to improve both accuracy and performance.
Why is this bad?
Negatively impacts accuracy and performance.
Example
use std::f32::consts::E;
let a = 3f32;
let _ = (2f32).powf(a);
let _ = E.powf(a);
let _ = a.powf(1.0 / 2.0);
let _ = a.log(2.0);
let _ = a.log(10.0);
let _ = a.log(E);
let _ = a.powf(2.0);
let _ = a * 2.0 + 4.0;
let _ = if a < 0.0 {
-a
} else {
a
};
let _ = if a < 0.0 {
a
} else {
-a
};
is better expressed as
use std::f32::consts::E;
let a = 3f32;
let _ = a.exp2();
let _ = a.exp();
let _ = a.sqrt();
let _ = a.log2();
let _ = a.log10();
let _ = a.ln();
let _ = a.powi(2);
let _ = a.mul_add(2.0, 4.0);
let _ = a.abs();
let _ = -a.abs();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- significant_drop_tighteningSearches for elements marked with #[clippy::has_significant_drop] that could be early dropped but are in fact dropped at the end of their scopes
- single_option_map`fn` that only maps over argument
- string_lit_as_bytesChecks for the as_bytes method called on string literals that contain only ASCII characters
- suspicious_operation_groupingsthis sequence of operators looks suspiciously like a bug
- too_long_first_doc_paragraphChecks if the first paragraph in the documentation of items listed in the module page is too long
- trailing_empty_arraytrailing zero-sized array in a struct which is not marked with a `repr` attribute
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?