Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1f4f357

Browse files
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.
1 parent c636c6a commit 1f4f357

File tree

6 files changed

+76
-105
lines changed

6 files changed

+76
-105
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,6 @@ Released 2018-09-13
11691169
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
11701170
[`implicit_hasher`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher
11711171
[`implicit_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
1172-
[`inaccurate_floating_point_computation`]: https://rust-lang.github.io/rust-clippy/master/index.html#inaccurate_floating_point_computation
11731172
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
11741173
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
11751174
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use std::f32::consts as f32_consts;
1313
use std::f64::consts as f64_consts;
1414

1515
declare_clippy_lint! {
16-
/// **What it does:** Looks for numerically unstable floating point
17-
/// computations and suggests better alternatives.
16+
/// **What it does:** Looks for floating-point expressions that
17+
/// can be expressed using built-in methods to improve accuracy,
18+
/// performance and/or succinctness.
1819
///
19-
/// **Why is this bad?** Numerically unstable floating point computations
20-
/// cause rounding errors to magnify and distorts the results strongly.
20+
/// **Why is this bad?** Negatively affects accuracy, performance
21+
/// and/or readability.
2122
///
2223
/// **Known problems:** None
2324
///
@@ -26,59 +27,43 @@ declare_clippy_lint! {
2627
/// ```rust
2728
/// use std::f32::consts::E;
2829
///
29-
/// let a = 1f32.log(2.0);
30-
/// let b = 1f32.log(10.0);
31-
/// let c = 1f32.log(E);
30+
/// let a = 3f32;
31+
/// let _ = (2f32).powf(a);
32+
/// let _ = E.powf(a);
33+
/// let _ = a.powf(1.0 / 2.0);
34+
/// let _ = a.powf(1.0 / 3.0);
35+
/// let _ = a.log(2.0);
36+
/// let _ = a.log(10.0);
37+
/// let _ = a.log(E);
38+
/// let _ = (1.0 + a).ln();
39+
/// let _ = a.exp() - 1.0;
3240
/// ```
3341
///
3442
/// is better expressed as
3543
///
3644
/// ```rust
37-
/// let a = 1f32.log2();
38-
/// let b = 1f32.log10();
39-
/// let c = 1f32.ln();
40-
/// ```
41-
pub INACCURATE_FLOATING_POINT_COMPUTATION,
42-
nursery,
43-
"checks for numerically unstable floating point computations"
44-
}
45-
46-
declare_clippy_lint! {
47-
/// **What it does:** Looks for inefficient floating point computations
48-
/// and suggests faster alternatives.
49-
///
50-
/// **Why is this bad?** Lower performance.
51-
///
52-
/// **Known problems:** None
53-
///
54-
/// **Example:**
55-
///
56-
/// ```rust
5745
/// use std::f32::consts::E;
5846
///
59-
/// let a = (2f32).powf(3.0);
60-
/// let c = E.powf(3.0);
61-
/// ```
62-
///
63-
/// is better expressed as
64-
///
65-
/// ```rust
66-
/// let a = (3f32).exp2();
67-
/// let b = (3f32).exp();
47+
/// let a = 3f32;
48+
/// let _ = a.exp2();
49+
/// let _ = a.exp();
50+
/// let _ = a.sqrt();
51+
/// let _ = a.cbrt();
52+
/// let _ = a.log2();
53+
/// let _ = a.log10();
54+
/// let _ = a.ln();
55+
/// let _ = a.ln_1p();
56+
/// let _ = a.exp_m1();
6857
/// ```
69-
pub SLOW_FLOATING_POINT_COMPUTATION,
58+
pub FLOATING_POINT_IMPROVEMENTS,
7059
nursery,
71-
"checks for inefficient floating point computations"
60+
"looks for improvements to floating-point expressions"
7261
}
7362

74-
declare_lint_pass!(FloatingPointArithmetic => [
75-
INACCURATE_FLOATING_POINT_COMPUTATION,
76-
SLOW_FLOATING_POINT_COMPUTATION
77-
]);
63+
declare_lint_pass!(FloatingPointArithmetic => [FLOATING_POINT_IMPROVEMENTS]);
7864

7965
fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
80-
let recv = &args[0];
81-
let arg = sugg::Sugg::hir(cx, recv, "..").maybe_par();
66+
let arg = sugg::Sugg::hir(cx, &args[0], "..").maybe_par();
8267

8368
if let Some((value, _)) = constant(cx, cx.tables, &args[1]) {
8469
let method;
@@ -95,7 +80,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
9580

9681
span_lint_and_sugg(
9782
cx,
98-
INACCURATE_FLOATING_POINT_COMPUTATION,
83+
FLOATING_POINT_IMPROVEMENTS,
9984
expr.span,
10085
"logarithm for bases 2, 10 and e can be computed more accurately",
10186
"consider using",
@@ -118,7 +103,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
118103

119104
span_lint_and_sugg(
120105
cx,
121-
INACCURATE_FLOATING_POINT_COMPUTATION,
106+
FLOATING_POINT_IMPROVEMENTS,
122107
expr.span,
123108
"ln(1 + x) can be computed more accurately",
124109
"consider using",
@@ -144,9 +129,9 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
144129

145130
span_lint_and_sugg(
146131
cx,
147-
SLOW_FLOATING_POINT_COMPUTATION,
132+
FLOATING_POINT_IMPROVEMENTS,
148133
expr.span,
149-
"exponent for bases 2 and e can be computed more efficiently",
134+
"exponent for bases 2 and e can be computed more accurately",
150135
"consider using",
151136
format!("{}.{}()", sugg::Sugg::hir(cx, &args[1], "..").maybe_par(), method),
152137
Applicability::MachineApplicable,
@@ -159,18 +144,18 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
159144
let method;
160145

161146
if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
162-
help = "square-root of a number can be computer more efficiently";
147+
help = "square-root of a number can be computed more efficiently and accurately";
163148
method = "sqrt";
164149
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
165-
help = "cube-root of a number can be computer more efficiently";
150+
help = "cube-root of a number can be computed more accurately";
166151
method = "cbrt";
167152
} else {
168153
return;
169154
}
170155

171156
span_lint_and_sugg(
172157
cx,
173-
SLOW_FLOATING_POINT_COMPUTATION,
158+
FLOATING_POINT_IMPROVEMENTS,
174159
expr.span,
175160
help,
176161
"consider using",
@@ -194,7 +179,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
194179
then {
195180
span_lint_and_sugg(
196181
cx,
197-
INACCURATE_FLOATING_POINT_COMPUTATION,
182+
FLOATING_POINT_IMPROVEMENTS,
198183
expr.span,
199184
"(e.pow(x) - 1) can be computed more accurately",
200185
"consider using",

clippy_lints/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,8 +1649,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16491649
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
16501650
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
16511651
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
1652-
LintId::of(&floating_point_arithmetic::INACCURATE_FLOATING_POINT_COMPUTATION),
1653-
LintId::of(&floating_point_arithmetic::SLOW_FLOATING_POINT_COMPUTATION),
1652+
LintId::of(&floating_point_arithmetic::FLOATING_POINT_IMPROVEMENTS),
16541653
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
16551654
LintId::of(&mul_add::MANUAL_MUL_ADD),
16561655
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),

src/lintlist/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -749,13 +749,6 @@ pub const ALL_LINTS: [Lint; 357] = [
749749
deprecation: None,
750750
module: "implicit_return",
751751
},
752-
Lint {
753-
name: "inaccurate_floating_point_computation",
754-
group: "nursery",
755-
desc: "checks for numerically unstable floating point computations",
756-
deprecation: None,
757-
module: "floating_point_arithmetic",
758-
},
759752
Lint {
760753
name: "inconsistent_digit_grouping",
761754
group: "style",

tests/ui/floating_point_arithmetic.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#![allow(dead_code)]
2-
#![warn(
3-
clippy::inaccurate_floating_point_computation,
4-
clippy::slow_floating_point_computation
5-
)]
2+
#![warn(clippy::floating_point_improvements)]
63

74
const TWO: f32 = 2.0;
85
const E: f32 = std::f32::consts::E;

0 commit comments

Comments
 (0)