Consolidate the accuracy and efficiency lints
Merge the accuracy and efficiency lints into a single lint that checks for improvements to accuracy, efficiency and readability of floating-point expressions.
This commit is contained in:
parent
c636c6a55b
commit
1f4f357bf5
6 changed files with 76 additions and 105 deletions
|
@ -1169,7 +1169,6 @@ Released 2018-09-13
|
|||
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
|
||||
[`implicit_hasher`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher
|
||||
[`implicit_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
|
||||
[`inaccurate_floating_point_computation`]: https://rust-lang.github.io/rust-clippy/master/index.html#inaccurate_floating_point_computation
|
||||
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
|
||||
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
|
||||
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask
|
||||
|
|
|
@ -13,11 +13,12 @@ use std::f32::consts as f32_consts;
|
|||
use std::f64::consts as f64_consts;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Looks for numerically unstable floating point
|
||||
/// computations and suggests better alternatives.
|
||||
/// **What it does:** Looks for floating-point expressions that
|
||||
/// can be expressed using built-in methods to improve accuracy,
|
||||
/// performance and/or succinctness.
|
||||
///
|
||||
/// **Why is this bad?** Numerically unstable floating point computations
|
||||
/// cause rounding errors to magnify and distorts the results strongly.
|
||||
/// **Why is this bad?** Negatively affects accuracy, performance
|
||||
/// and/or readability.
|
||||
///
|
||||
/// **Known problems:** None
|
||||
///
|
||||
|
@ -26,59 +27,43 @@ declare_clippy_lint! {
|
|||
/// ```rust
|
||||
/// use std::f32::consts::E;
|
||||
///
|
||||
/// let a = 1f32.log(2.0);
|
||||
/// let b = 1f32.log(10.0);
|
||||
/// let c = 1f32.log(E);
|
||||
/// let a = 3f32;
|
||||
/// let _ = (2f32).powf(a);
|
||||
/// let _ = E.powf(a);
|
||||
/// let _ = a.powf(1.0 / 2.0);
|
||||
/// let _ = a.powf(1.0 / 3.0);
|
||||
/// let _ = a.log(2.0);
|
||||
/// let _ = a.log(10.0);
|
||||
/// let _ = a.log(E);
|
||||
/// let _ = (1.0 + a).ln();
|
||||
/// let _ = a.exp() - 1.0;
|
||||
/// ```
|
||||
///
|
||||
/// is better expressed as
|
||||
///
|
||||
/// ```rust
|
||||
/// let a = 1f32.log2();
|
||||
/// let b = 1f32.log10();
|
||||
/// let c = 1f32.ln();
|
||||
/// ```
|
||||
pub INACCURATE_FLOATING_POINT_COMPUTATION,
|
||||
nursery,
|
||||
"checks for numerically unstable floating point computations"
|
||||
}
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Looks for inefficient floating point computations
|
||||
/// and suggests faster alternatives.
|
||||
///
|
||||
/// **Why is this bad?** Lower performance.
|
||||
///
|
||||
/// **Known problems:** None
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::f32::consts::E;
|
||||
///
|
||||
/// let a = (2f32).powf(3.0);
|
||||
/// let c = E.powf(3.0);
|
||||
/// let a = 3f32;
|
||||
/// let _ = a.exp2();
|
||||
/// let _ = a.exp();
|
||||
/// let _ = a.sqrt();
|
||||
/// let _ = a.cbrt();
|
||||
/// let _ = a.log2();
|
||||
/// let _ = a.log10();
|
||||
/// let _ = a.ln();
|
||||
/// let _ = a.ln_1p();
|
||||
/// let _ = a.exp_m1();
|
||||
/// ```
|
||||
///
|
||||
/// is better expressed as
|
||||
///
|
||||
/// ```rust
|
||||
/// let a = (3f32).exp2();
|
||||
/// let b = (3f32).exp();
|
||||
/// ```
|
||||
pub SLOW_FLOATING_POINT_COMPUTATION,
|
||||
pub FLOATING_POINT_IMPROVEMENTS,
|
||||
nursery,
|
||||
"checks for inefficient floating point computations"
|
||||
"looks for improvements to floating-point expressions"
|
||||
}
|
||||
|
||||
declare_lint_pass!(FloatingPointArithmetic => [
|
||||
INACCURATE_FLOATING_POINT_COMPUTATION,
|
||||
SLOW_FLOATING_POINT_COMPUTATION
|
||||
]);
|
||||
declare_lint_pass!(FloatingPointArithmetic => [FLOATING_POINT_IMPROVEMENTS]);
|
||||
|
||||
fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
||||
let recv = &args[0];
|
||||
let arg = sugg::Sugg::hir(cx, recv, "..").maybe_par();
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..").maybe_par();
|
||||
|
||||
if let Some((value, _)) = constant(cx, cx.tables, &args[1]) {
|
||||
let method;
|
||||
|
@ -95,7 +80,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
|||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
INACCURATE_FLOATING_POINT_COMPUTATION,
|
||||
FLOATING_POINT_IMPROVEMENTS,
|
||||
expr.span,
|
||||
"logarithm for bases 2, 10 and e can be computed more accurately",
|
||||
"consider using",
|
||||
|
@ -118,7 +103,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
|||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
INACCURATE_FLOATING_POINT_COMPUTATION,
|
||||
FLOATING_POINT_IMPROVEMENTS,
|
||||
expr.span,
|
||||
"ln(1 + x) can be computed more accurately",
|
||||
"consider using",
|
||||
|
@ -144,9 +129,9 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
|||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
SLOW_FLOATING_POINT_COMPUTATION,
|
||||
FLOATING_POINT_IMPROVEMENTS,
|
||||
expr.span,
|
||||
"exponent for bases 2 and e can be computed more efficiently",
|
||||
"exponent for bases 2 and e can be computed more accurately",
|
||||
"consider using",
|
||||
format!("{}.{}()", sugg::Sugg::hir(cx, &args[1], "..").maybe_par(), method),
|
||||
Applicability::MachineApplicable,
|
||||
|
@ -159,10 +144,10 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
|||
let method;
|
||||
|
||||
if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
|
||||
help = "square-root of a number can be computer more efficiently";
|
||||
help = "square-root of a number can be computed more efficiently and accurately";
|
||||
method = "sqrt";
|
||||
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
|
||||
help = "cube-root of a number can be computer more efficiently";
|
||||
help = "cube-root of a number can be computed more accurately";
|
||||
method = "cbrt";
|
||||
} else {
|
||||
return;
|
||||
|
@ -170,7 +155,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
|
|||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
SLOW_FLOATING_POINT_COMPUTATION,
|
||||
FLOATING_POINT_IMPROVEMENTS,
|
||||
expr.span,
|
||||
help,
|
||||
"consider using",
|
||||
|
@ -194,7 +179,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
|
|||
then {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
INACCURATE_FLOATING_POINT_COMPUTATION,
|
||||
FLOATING_POINT_IMPROVEMENTS,
|
||||
expr.span,
|
||||
"(e.pow(x) - 1) can be computed more accurately",
|
||||
"consider using",
|
||||
|
|
|
@ -1649,8 +1649,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
|
||||
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
|
||||
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
|
||||
LintId::of(&floating_point_arithmetic::INACCURATE_FLOATING_POINT_COMPUTATION),
|
||||
LintId::of(&floating_point_arithmetic::SLOW_FLOATING_POINT_COMPUTATION),
|
||||
LintId::of(&floating_point_arithmetic::FLOATING_POINT_IMPROVEMENTS),
|
||||
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
|
||||
LintId::of(&mul_add::MANUAL_MUL_ADD),
|
||||
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
|
||||
|
|
|
@ -749,13 +749,6 @@ pub const ALL_LINTS: [Lint; 357] = [
|
|||
deprecation: None,
|
||||
module: "implicit_return",
|
||||
},
|
||||
Lint {
|
||||
name: "inaccurate_floating_point_computation",
|
||||
group: "nursery",
|
||||
desc: "checks for numerically unstable floating point computations",
|
||||
deprecation: None,
|
||||
module: "floating_point_arithmetic",
|
||||
},
|
||||
Lint {
|
||||
name: "inconsistent_digit_grouping",
|
||||
group: "style",
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
#![allow(dead_code)]
|
||||
#![warn(
|
||||
clippy::inaccurate_floating_point_computation,
|
||||
clippy::slow_floating_point_computation
|
||||
)]
|
||||
#![warn(clippy::floating_point_improvements)]
|
||||
|
||||
const TWO: f32 = 2.0;
|
||||
const E: f32 = std::f32::consts::E;
|
||||
|
|
|
@ -1,171 +1,169 @@
|
|||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:12:13
|
||||
--> $DIR/floating_point_arithmetic.rs:9:13
|
||||
|
|
||||
LL | let _ = x.log(2f32);
|
||||
| ^^^^^^^^^^^ help: consider using: `x.log2()`
|
||||
|
|
||||
= note: `-D clippy::inaccurate-floating-point-computation` implied by `-D warnings`
|
||||
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:13:13
|
||||
--> $DIR/floating_point_arithmetic.rs:10:13
|
||||
|
|
||||
LL | let _ = x.log(10f32);
|
||||
| ^^^^^^^^^^^^ help: consider using: `x.log10()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:14:13
|
||||
--> $DIR/floating_point_arithmetic.rs:11:13
|
||||
|
|
||||
LL | let _ = x.log(std::f32::consts::E);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:15:13
|
||||
--> $DIR/floating_point_arithmetic.rs:12:13
|
||||
|
|
||||
LL | let _ = x.log(TWO);
|
||||
| ^^^^^^^^^^ help: consider using: `x.log2()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:16:13
|
||||
--> $DIR/floating_point_arithmetic.rs:13:13
|
||||
|
|
||||
LL | let _ = x.log(E);
|
||||
| ^^^^^^^^ help: consider using: `x.ln()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:19:13
|
||||
--> $DIR/floating_point_arithmetic.rs:16:13
|
||||
|
|
||||
LL | let _ = x.log(2f64);
|
||||
| ^^^^^^^^^^^ help: consider using: `x.log2()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:20:13
|
||||
--> $DIR/floating_point_arithmetic.rs:17:13
|
||||
|
|
||||
LL | let _ = x.log(10f64);
|
||||
| ^^^^^^^^^^^^ help: consider using: `x.log10()`
|
||||
|
||||
error: logarithm for bases 2, 10 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:21:13
|
||||
--> $DIR/floating_point_arithmetic.rs:18:13
|
||||
|
|
||||
LL | let _ = x.log(std::f64::consts::E);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:26:13
|
||||
--> $DIR/floating_point_arithmetic.rs:23:13
|
||||
|
|
||||
LL | let _ = (1.0 + x).ln();
|
||||
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:27:13
|
||||
--> $DIR/floating_point_arithmetic.rs:24:13
|
||||
|
|
||||
LL | let _ = (1.0 + x * 2.0).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:28:13
|
||||
--> $DIR/floating_point_arithmetic.rs:25:13
|
||||
|
|
||||
LL | let _ = (1.0 + x.powi(2)).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2).ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:29:13
|
||||
--> $DIR/floating_point_arithmetic.rs:26:13
|
||||
|
|
||||
LL | let _ = (1.0 + x.powi(2) * 2.0).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(2) * 2.0).ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:30:13
|
||||
--> $DIR/floating_point_arithmetic.rs:27:13
|
||||
|
|
||||
LL | let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `((std::f32::consts::E - 1.0)).ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:37:13
|
||||
--> $DIR/floating_point_arithmetic.rs:34:13
|
||||
|
|
||||
LL | let _ = (1.0 + x).ln();
|
||||
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:38:13
|
||||
--> $DIR/floating_point_arithmetic.rs:35:13
|
||||
|
|
||||
LL | let _ = (1.0 + x * 2.0).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
|
||||
|
||||
error: ln(1 + x) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:39:13
|
||||
--> $DIR/floating_point_arithmetic.rs:36:13
|
||||
|
|
||||
LL | let _ = (1.0 + x.powi(2)).ln();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2).ln_1p()`
|
||||
|
||||
error: exponent for bases 2 and e can be computed more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:48:13
|
||||
error: exponent for bases 2 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:45:13
|
||||
|
|
||||
LL | let _ = 2f32.powf(x);
|
||||
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
|
||||
|
|
||||
= note: `-D clippy::slow-floating-point-computation` implied by `-D warnings`
|
||||
|
||||
error: exponent for bases 2 and e can be computed more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:49:13
|
||||
error: exponent for bases 2 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:46:13
|
||||
|
|
||||
LL | let _ = std::f32::consts::E.powf(x);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
|
||||
|
||||
error: square-root of a number can be computer more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:50:13
|
||||
error: square-root of a number can be computed more efficiently and accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:47:13
|
||||
|
|
||||
LL | let _ = x.powf(1.0 / 2.0);
|
||||
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
|
||||
|
||||
error: cube-root of a number can be computer more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:51:13
|
||||
error: cube-root of a number can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:48:13
|
||||
|
|
||||
LL | let _ = x.powf(1.0 / 3.0);
|
||||
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
|
||||
|
||||
error: exponent for bases 2 and e can be computed more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:54:13
|
||||
error: exponent for bases 2 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:51:13
|
||||
|
|
||||
LL | let _ = 2f64.powf(x);
|
||||
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
|
||||
|
||||
error: exponent for bases 2 and e can be computed more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:55:13
|
||||
error: exponent for bases 2 and e can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:52:13
|
||||
|
|
||||
LL | let _ = std::f64::consts::E.powf(x);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
|
||||
|
||||
error: square-root of a number can be computer more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:56:13
|
||||
error: square-root of a number can be computed more efficiently and accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:53:13
|
||||
|
|
||||
LL | let _ = x.powf(1.0 / 2.0);
|
||||
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
|
||||
|
||||
error: cube-root of a number can be computer more efficiently
|
||||
--> $DIR/floating_point_arithmetic.rs:57:13
|
||||
error: cube-root of a number can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:54:13
|
||||
|
|
||||
LL | let _ = x.powf(1.0 / 3.0);
|
||||
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
|
||||
|
||||
error: (e.pow(x) - 1) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:62:13
|
||||
--> $DIR/floating_point_arithmetic.rs:59:13
|
||||
|
|
||||
LL | let _ = x.exp() - 1.0;
|
||||
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
||||
|
||||
error: (e.pow(x) - 1) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:63:13
|
||||
--> $DIR/floating_point_arithmetic.rs:60:13
|
||||
|
|
||||
LL | let _ = x.exp() - 1.0 + 2.0;
|
||||
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
||||
|
||||
error: (e.pow(x) - 1) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:69:13
|
||||
--> $DIR/floating_point_arithmetic.rs:66:13
|
||||
|
|
||||
LL | let _ = x.exp() - 1.0;
|
||||
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
||||
|
||||
error: (e.pow(x) - 1) can be computed more accurately
|
||||
--> $DIR/floating_point_arithmetic.rs:70:13
|
||||
--> $DIR/floating_point_arithmetic.rs:67:13
|
||||
|
|
||||
LL | let _ = x.exp() - 1.0 + 2.0;
|
||||
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
||||
|
|
Loading…
Add table
Reference in a new issue