@@ -4,10 +4,10 @@ use crate::consts::{
4
4
} ;
5
5
use crate :: utils:: * ;
6
6
use if_chain:: if_chain;
7
- use rustc_hir:: * ;
8
- use rustc_lint:: { LateContext , LateLintPass } ;
9
7
use rustc:: ty;
10
8
use rustc_errors:: Applicability ;
9
+ use rustc_hir:: * ;
10
+ use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
12
12
use std:: f32:: consts as f32_consts;
13
13
use std:: f64:: consts as f64_consts;
@@ -16,11 +16,10 @@ use syntax::ast;
16
16
17
17
declare_clippy_lint ! {
18
18
/// **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 .
21
21
///
22
- /// **Why is this bad?** Negatively affects accuracy, performance
23
- /// and/or readability.
22
+ /// **Why is this bad?** Negatively impacts accuracy and performance.
24
23
///
25
24
/// **Known problems:** None
26
25
///
@@ -59,16 +58,16 @@ declare_clippy_lint! {
59
58
/// let _ = a.exp_m1();
60
59
/// let _ = a.powi(2);
61
60
/// ```
62
- pub FLOATING_POINT_IMPROVEMENTS ,
61
+ pub SUBOPTIMAL_FLOPS ,
63
62
nursery,
64
- "looks for improvements to floating- point expressions "
63
+ "usage of sub-optimal floating point operations "
65
64
}
66
65
67
- declare_lint_pass ! ( FloatingPointArithmetic => [ FLOATING_POINT_IMPROVEMENTS ] ) ;
66
+ declare_lint_pass ! ( FloatingPointArithmetic => [ SUBOPTIMAL_FLOPS ] ) ;
68
67
69
68
// Returns the specialized log method for a given base if base is constant
70
69
// 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 > {
72
71
if let Some ( ( value, _) ) = constant ( cx, cx. tables , base) {
73
72
if F32 ( 2.0 ) == value || F64 ( 2.0 ) == value {
74
73
return Some ( "log2" ) ;
@@ -124,7 +123,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
124
123
if let Some ( method) = get_specialized_log_method ( cx, & args[ 1 ] ) {
125
124
span_lint_and_sugg (
126
125
cx,
127
- FLOATING_POINT_IMPROVEMENTS ,
126
+ SUBOPTIMAL_FLOPS ,
128
127
expr. span ,
129
128
"logarithm for bases 2, 10 and e can be computed more accurately" ,
130
129
"consider using" ,
@@ -136,7 +135,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
136
135
137
136
// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
138
137
// 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 < ' _ > ] ) {
140
139
if_chain ! {
141
140
if let ExprKind :: Binary ( op, ref lhs, ref rhs) = & args[ 0 ] . kind;
142
141
if op. node == BinOpKind :: Add ;
@@ -149,7 +148,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
149
148
150
149
span_lint_and_sugg(
151
150
cx,
152
- FLOATING_POINT_IMPROVEMENTS ,
151
+ SUBOPTIMAL_FLOPS ,
153
152
expr. span,
154
153
"ln(1 + x) can be computed more accurately" ,
155
154
"consider using" ,
@@ -185,7 +184,7 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i64> {
185
184
}
186
185
}
187
186
188
- fn check_powf ( cx : & LateContext < ' _ , ' _ > , expr : & Expr , args : & HirVec < Expr > ) {
187
+ fn check_powf ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
189
188
// Check receiver
190
189
if let Some ( ( value, _) ) = constant ( cx, cx. tables , & args[ 0 ] ) {
191
190
let method;
@@ -200,7 +199,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
200
199
201
200
span_lint_and_sugg (
202
201
cx,
203
- FLOATING_POINT_IMPROVEMENTS ,
202
+ SUBOPTIMAL_FLOPS ,
204
203
expr. span ,
205
204
"exponent for bases 2 and e can be computed more accurately" ,
206
205
"consider using" ,
@@ -223,7 +222,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
223
222
} else if let Some ( exponent) = get_integer_from_float_constant ( & value) {
224
223
span_lint_and_sugg (
225
224
cx,
226
- FLOATING_POINT_IMPROVEMENTS ,
225
+ SUBOPTIMAL_FLOPS ,
227
226
expr. span ,
228
227
"exponentiation with integer powers can be computed more efficiently" ,
229
228
"consider using" ,
@@ -238,7 +237,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
238
237
239
238
span_lint_and_sugg (
240
239
cx,
241
- FLOATING_POINT_IMPROVEMENTS ,
240
+ SUBOPTIMAL_FLOPS ,
242
241
expr. span ,
243
242
help,
244
243
"consider using" ,
@@ -250,7 +249,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
250
249
251
250
// TODO: Lint expressions of the form `x.exp() - y` where y > 1
252
251
// 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 < ' _ > ) {
254
253
if_chain ! {
255
254
if let ExprKind :: Binary ( op, ref lhs, ref rhs) = expr. kind;
256
255
if op. node == BinOpKind :: Sub ;
@@ -263,7 +262,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
263
262
then {
264
263
span_lint_and_sugg(
265
264
cx,
266
- FLOATING_POINT_IMPROVEMENTS ,
265
+ SUBOPTIMAL_FLOPS ,
267
266
expr. span,
268
267
"(e.pow(x) - 1) can be computed more accurately" ,
269
268
"consider using" ,
@@ -278,7 +277,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
278
277
}
279
278
280
279
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 < ' _ > ) {
282
281
if let ExprKind :: MethodCall ( ref path, _, args) = & expr. kind {
283
282
let recv_ty = cx. tables . expr_ty ( & args[ 0 ] ) ;
284
283
0 commit comments