Skip to content

Commit 6dacb1a

Browse files
Change lint name to suboptimal_flops
1 parent bc03f46 commit 6dacb1a

File tree

10 files changed

+45
-41
lines changed

10 files changed

+45
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,7 @@ Released 2018-09-13
13491349
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
13501350
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
13511351
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
1352+
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
13521353
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
13531354
[`suspicious_assignment_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_assignment_formatting
13541355
[`suspicious_else_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_else_formatting

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::consts::{
44
};
55
use crate::utils::*;
66
use if_chain::if_chain;
7-
use rustc_hir::*;
8-
use rustc_lint::{LateContext, LateLintPass};
97
use rustc::ty;
108
use rustc_errors::Applicability;
9+
use rustc_hir::*;
10+
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use std::f32::consts as f32_consts;
1313
use std::f64::consts as f64_consts;
@@ -16,11 +16,10 @@ use syntax::ast;
1616

1717
declare_clippy_lint! {
1818
/// **What it does:** Looks for floating-point expressions that
19-
/// can be expressed using built-in methods to improve accuracy,
20-
/// performance and/or succinctness.
19+
/// can be expressed using built-in methods to improve both
20+
/// accuracy and performance.
2121
///
22-
/// **Why is this bad?** Negatively affects accuracy, performance
23-
/// and/or readability.
22+
/// **Why is this bad?** Negatively impacts accuracy and performance.
2423
///
2524
/// **Known problems:** None
2625
///
@@ -59,16 +58,16 @@ declare_clippy_lint! {
5958
/// let _ = a.exp_m1();
6059
/// let _ = a.powi(2);
6160
/// ```
62-
pub FLOATING_POINT_IMPROVEMENTS,
61+
pub SUBOPTIMAL_FLOPS,
6362
nursery,
64-
"looks for improvements to floating-point expressions"
63+
"usage of sub-optimal floating point operations"
6564
}
6665

67-
declare_lint_pass!(FloatingPointArithmetic => [FLOATING_POINT_IMPROVEMENTS]);
66+
declare_lint_pass!(FloatingPointArithmetic => [SUBOPTIMAL_FLOPS]);
6867

6968
// Returns the specialized log method for a given base if base is constant
7069
// and is one of 2, 10 and e
71-
fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr) -> Option<&'static str> {
70+
fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr<'_>) -> Option<&'static str> {
7271
if let Some((value, _)) = constant(cx, cx.tables, base) {
7372
if F32(2.0) == value || F64(2.0) == value {
7473
return Some("log2");
@@ -124,7 +123,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
124123
if let Some(method) = get_specialized_log_method(cx, &args[1]) {
125124
span_lint_and_sugg(
126125
cx,
127-
FLOATING_POINT_IMPROVEMENTS,
126+
SUBOPTIMAL_FLOPS,
128127
expr.span,
129128
"logarithm for bases 2, 10 and e can be computed more accurately",
130129
"consider using",
@@ -136,7 +135,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
136135

137136
// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
138137
// suggest usage of `(x + (y - 1)).ln_1p()` instead
139-
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
138+
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
140139
if_chain! {
141140
if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind;
142141
if op.node == BinOpKind::Add;
@@ -149,7 +148,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
149148

150149
span_lint_and_sugg(
151150
cx,
152-
FLOATING_POINT_IMPROVEMENTS,
151+
SUBOPTIMAL_FLOPS,
153152
expr.span,
154153
"ln(1 + x) can be computed more accurately",
155154
"consider using",
@@ -185,7 +184,7 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i64> {
185184
}
186185
}
187186

188-
fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
187+
fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
189188
// Check receiver
190189
if let Some((value, _)) = constant(cx, cx.tables, &args[0]) {
191190
let method;
@@ -200,7 +199,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
200199

201200
span_lint_and_sugg(
202201
cx,
203-
FLOATING_POINT_IMPROVEMENTS,
202+
SUBOPTIMAL_FLOPS,
204203
expr.span,
205204
"exponent for bases 2 and e can be computed more accurately",
206205
"consider using",
@@ -223,7 +222,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
223222
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
224223
span_lint_and_sugg(
225224
cx,
226-
FLOATING_POINT_IMPROVEMENTS,
225+
SUBOPTIMAL_FLOPS,
227226
expr.span,
228227
"exponentiation with integer powers can be computed more efficiently",
229228
"consider using",
@@ -238,7 +237,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
238237

239238
span_lint_and_sugg(
240239
cx,
241-
FLOATING_POINT_IMPROVEMENTS,
240+
SUBOPTIMAL_FLOPS,
242241
expr.span,
243242
help,
244243
"consider using",
@@ -250,7 +249,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
250249

251250
// TODO: Lint expressions of the form `x.exp() - y` where y > 1
252251
// and suggest usage of `x.exp_m1() - (y - 1)` instead
253-
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
252+
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
254253
if_chain! {
255254
if let ExprKind::Binary(op, ref lhs, ref rhs) = expr.kind;
256255
if op.node == BinOpKind::Sub;
@@ -263,7 +262,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
263262
then {
264263
span_lint_and_sugg(
265264
cx,
266-
FLOATING_POINT_IMPROVEMENTS,
265+
SUBOPTIMAL_FLOPS,
267266
expr.span,
268267
"(e.pow(x) - 1) can be computed more accurately",
269268
"consider using",
@@ -278,7 +277,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
278277
}
279278

280279
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
281-
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
280+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
282281
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
283282
let recv_ty = cx.tables.expr_ty(&args[0]);
284283

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
538538
&fallible_impl_from::FALLIBLE_IMPL_FROM,
539539
&float_literal::EXCESSIVE_PRECISION,
540540
&float_literal::LOSSY_FLOAT_LITERAL,
541+
&floating_point_arithmetic::SUBOPTIMAL_FLOPS,
541542
&format::USELESS_FORMAT,
542543
&formatting::POSSIBLE_MISSING_COMMA,
543544
&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
@@ -1649,7 +1650,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16491650
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
16501651
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
16511652
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
1652-
LintId::of(&floating_point_arithmetic::FLOATING_POINT_IMPROVEMENTS),
1653+
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),
16531654
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
16541655
LintId::of(&mul_add::MANUAL_MUL_ADD),
16551656
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,13 @@ pub const ALL_LINTS: [Lint; 357] = [
19601960
deprecation: None,
19611961
module: "excessive_bools",
19621962
},
1963+
Lint {
1964+
name: "suboptimal_flops",
1965+
group: "nursery",
1966+
desc: "usage of sub-optimal floating point operations",
1967+
deprecation: None,
1968+
module: "floating_point_arithmetic",
1969+
},
19631970
Lint {
19641971
name: "suspicious_arithmetic_impl",
19651972
group: "correctness",

tests/ui/floating_point_exp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::floating_point_improvements)]
1+
#![warn(clippy::suboptimal_flops)]
22

33
fn main() {
44
let x = 2f32;

tests/ui/floating_point_exp.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: (e.pow(x) - 1) can be computed more accurately
44
LL | let _ = x.exp() - 1.0;
55
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
66
|
7-
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
7+
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

99
error: (e.pow(x) - 1) can be computed more accurately
1010
--> $DIR/floating_point_exp.rs:6:13

tests/ui/floating_point_log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(dead_code)]
2-
#![warn(clippy::floating_point_improvements)]
2+
#![warn(clippy::suboptimal_flops)]
33

44
const TWO: f32 = 2.0;
55
const E: f32 = std::f32::consts::E;

tests/ui/floating_point_log.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: logarithm for bases 2, 10 and e can be computed more accurately
44
LL | let _ = x.log(2f32);
55
| ^^^^^^^^^^^ help: consider using: `x.log2()`
66
|
7-
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
7+
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

99
error: logarithm for bases 2, 10 and e can be computed more accurately
1010
--> $DIR/floating_point_log.rs:10:13

tests/ui/floating_point_powf.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::floating_point_improvements)]
1+
#![warn(clippy::suboptimal_flops)]
22

33
fn main() {
44
let x = 3f32;
@@ -14,8 +14,6 @@ fn main() {
1414
let _ = x.powf(-2.0);
1515
let _ = x.powf(2.1);
1616
let _ = x.powf(-2.1);
17-
let _ = x.powf(16_777_217.0);
18-
let _ = x.powf(-16_777_217.0);
1917

2018
let x = 3f64;
2119
let _ = 2f64.powf(x);
@@ -30,6 +28,4 @@ fn main() {
3028
let _ = x.powf(-2.0);
3129
let _ = x.powf(2.1);
3230
let _ = x.powf(-2.1);
33-
let _ = x.powf(9_007_199_254_740_993.0);
34-
let _ = x.powf(-9_007_199_254_740_993.0);
3531
}

tests/ui/floating_point_powf.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: exponent for bases 2 and e can be computed more accurately
44
LL | let _ = 2f32.powf(x);
55
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
66
|
7-
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
7+
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

99
error: exponent for bases 2 and e can be computed more accurately
1010
--> $DIR/floating_point_powf.rs:6:13
@@ -61,61 +61,61 @@ LL | let _ = x.powf(-2.0);
6161
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
6262

6363
error: exponent for bases 2 and e can be computed more accurately
64-
--> $DIR/floating_point_powf.rs:21:13
64+
--> $DIR/floating_point_powf.rs:19:13
6565
|
6666
LL | let _ = 2f64.powf(x);
6767
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
6868

6969
error: exponent for bases 2 and e can be computed more accurately
70-
--> $DIR/floating_point_powf.rs:22:13
70+
--> $DIR/floating_point_powf.rs:20:13
7171
|
7272
LL | let _ = 2f64.powf(3.1);
7373
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
7474

7575
error: exponent for bases 2 and e can be computed more accurately
76-
--> $DIR/floating_point_powf.rs:23:13
76+
--> $DIR/floating_point_powf.rs:21:13
7777
|
7878
LL | let _ = 2f64.powf(-3.1);
7979
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
8080

8181
error: exponent for bases 2 and e can be computed more accurately
82-
--> $DIR/floating_point_powf.rs:24:13
82+
--> $DIR/floating_point_powf.rs:22:13
8383
|
8484
LL | let _ = std::f64::consts::E.powf(x);
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
8686

8787
error: exponent for bases 2 and e can be computed more accurately
88-
--> $DIR/floating_point_powf.rs:25:13
88+
--> $DIR/floating_point_powf.rs:23:13
8989
|
9090
LL | let _ = std::f64::consts::E.powf(3.1);
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
9292

9393
error: exponent for bases 2 and e can be computed more accurately
94-
--> $DIR/floating_point_powf.rs:26:13
94+
--> $DIR/floating_point_powf.rs:24:13
9595
|
9696
LL | let _ = std::f64::consts::E.powf(-3.1);
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
9898

9999
error: square-root of a number can be computed more efficiently and accurately
100-
--> $DIR/floating_point_powf.rs:27:13
100+
--> $DIR/floating_point_powf.rs:25:13
101101
|
102102
LL | let _ = x.powf(1.0 / 2.0);
103103
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
104104

105105
error: cube-root of a number can be computed more accurately
106-
--> $DIR/floating_point_powf.rs:28:13
106+
--> $DIR/floating_point_powf.rs:26:13
107107
|
108108
LL | let _ = x.powf(1.0 / 3.0);
109109
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
110110

111111
error: exponentiation with integer powers can be computed more efficiently
112-
--> $DIR/floating_point_powf.rs:29:13
112+
--> $DIR/floating_point_powf.rs:27:13
113113
|
114114
LL | let _ = x.powf(2.0);
115115
| ^^^^^^^^^^^ help: consider using: `x.powi(2)`
116116

117117
error: exponentiation with integer powers can be computed more efficiently
118-
--> $DIR/floating_point_powf.rs:30:13
118+
--> $DIR/floating_point_powf.rs:28:13
119119
|
120120
LL | let _ = x.powf(-2.0);
121121
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`

0 commit comments

Comments
 (0)