• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use clippy_utils::consts::{
2     constant, constant_simple, Constant,
3     Constant::{Int, F32, F64},
4 };
5 use clippy_utils::{
6     diagnostics::span_lint_and_sugg, eq_expr_value, get_parent_expr, higher, in_constant, is_no_std_crate,
7     numeric_literal, peel_blocks, sugg,
8 };
9 use if_chain::if_chain;
10 use rustc_errors::Applicability;
11 use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_middle::ty;
14 use rustc_session::{declare_lint_pass, declare_tool_lint};
15 use rustc_span::source_map::Spanned;
16 
17 use rustc_ast::ast;
18 use std::f32::consts as f32_consts;
19 use std::f64::consts as f64_consts;
20 use sugg::Sugg;
21 
22 declare_clippy_lint! {
23     /// ### What it does
24     /// Looks for floating-point expressions that
25     /// can be expressed using built-in methods to improve accuracy
26     /// at the cost of performance.
27     ///
28     /// ### Why is this bad?
29     /// Negatively impacts accuracy.
30     ///
31     /// ### Example
32     /// ```rust
33     /// let a = 3f32;
34     /// let _ = a.powf(1.0 / 3.0);
35     /// let _ = (1.0 + a).ln();
36     /// let _ = a.exp() - 1.0;
37     /// ```
38     ///
39     /// Use instead:
40     /// ```rust
41     /// let a = 3f32;
42     /// let _ = a.cbrt();
43     /// let _ = a.ln_1p();
44     /// let _ = a.exp_m1();
45     /// ```
46     #[clippy::version = "1.43.0"]
47     pub IMPRECISE_FLOPS,
48     nursery,
49     "usage of imprecise floating point operations"
50 }
51 
52 declare_clippy_lint! {
53     /// ### What it does
54     /// Looks for floating-point expressions that
55     /// can be expressed using built-in methods to improve both
56     /// accuracy and performance.
57     ///
58     /// ### Why is this bad?
59     /// Negatively impacts accuracy and performance.
60     ///
61     /// ### Example
62     /// ```rust
63     /// use std::f32::consts::E;
64     ///
65     /// let a = 3f32;
66     /// let _ = (2f32).powf(a);
67     /// let _ = E.powf(a);
68     /// let _ = a.powf(1.0 / 2.0);
69     /// let _ = a.log(2.0);
70     /// let _ = a.log(10.0);
71     /// let _ = a.log(E);
72     /// let _ = a.powf(2.0);
73     /// let _ = a * 2.0 + 4.0;
74     /// let _ = if a < 0.0 {
75     ///     -a
76     /// } else {
77     ///     a
78     /// };
79     /// let _ = if a < 0.0 {
80     ///     a
81     /// } else {
82     ///     -a
83     /// };
84     /// ```
85     ///
86     /// is better expressed as
87     ///
88     /// ```rust
89     /// use std::f32::consts::E;
90     ///
91     /// let a = 3f32;
92     /// let _ = a.exp2();
93     /// let _ = a.exp();
94     /// let _ = a.sqrt();
95     /// let _ = a.log2();
96     /// let _ = a.log10();
97     /// let _ = a.ln();
98     /// let _ = a.powi(2);
99     /// let _ = a.mul_add(2.0, 4.0);
100     /// let _ = a.abs();
101     /// let _ = -a.abs();
102     /// ```
103     #[clippy::version = "1.43.0"]
104     pub SUBOPTIMAL_FLOPS,
105     nursery,
106     "usage of sub-optimal floating point operations"
107 }
108 
109 declare_lint_pass!(FloatingPointArithmetic => [
110     IMPRECISE_FLOPS,
111     SUBOPTIMAL_FLOPS
112 ]);
113 
114 // Returns the specialized log method for a given base if base is constant
115 // and is one of 2, 10 and e
get_specialized_log_method(cx: &LateContext<'_>, base: &Expr<'_>) -> Option<&'static str>116 fn get_specialized_log_method(cx: &LateContext<'_>, base: &Expr<'_>) -> Option<&'static str> {
117     if let Some(value) = constant(cx, cx.typeck_results(), base) {
118         if F32(2.0) == value || F64(2.0) == value {
119             return Some("log2");
120         } else if F32(10.0) == value || F64(10.0) == value {
121             return Some("log10");
122         } else if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
123             return Some("ln");
124         }
125     }
126 
127     None
128 }
129 
130 // Adds type suffixes and parenthesis to method receivers if necessary
prepare_receiver_sugg<'a>(cx: &LateContext<'_>, mut expr: &'a Expr<'a>) -> Sugg<'a>131 fn prepare_receiver_sugg<'a>(cx: &LateContext<'_>, mut expr: &'a Expr<'a>) -> Sugg<'a> {
132     let mut suggestion = Sugg::hir(cx, expr, "..");
133 
134     if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {
135         expr = inner_expr;
136     }
137 
138     if_chain! {
139         // if the expression is a float literal and it is unsuffixed then
140         // add a suffix so the suggestion is valid and unambiguous
141         if let ty::Float(float_ty) = cx.typeck_results().expr_ty(expr).kind();
142         if let ExprKind::Lit(lit) = &expr.kind;
143         if let ast::LitKind::Float(sym, ast::LitFloatType::Unsuffixed) = lit.node;
144         then {
145             let op = format!(
146                 "{suggestion}{}{}",
147                 // Check for float literals without numbers following the decimal
148                 // separator such as `2.` and adds a trailing zero
149                 if sym.as_str().ends_with('.') {
150                     "0"
151                 } else {
152                     ""
153                 },
154                 float_ty.name_str()
155             ).into();
156 
157             suggestion = match suggestion {
158                 Sugg::MaybeParen(_) => Sugg::MaybeParen(op),
159                 _ => Sugg::NonParen(op)
160             };
161         }
162     }
163 
164     suggestion.maybe_par()
165 }
166 
check_log_base(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>])167 fn check_log_base(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
168     if let Some(method) = get_specialized_log_method(cx, &args[0]) {
169         span_lint_and_sugg(
170             cx,
171             SUBOPTIMAL_FLOPS,
172             expr.span,
173             "logarithm for bases 2, 10 and e can be computed more accurately",
174             "consider using",
175             format!("{}.{method}()", Sugg::hir(cx, receiver, "..").maybe_par()),
176             Applicability::MachineApplicable,
177         );
178     }
179 }
180 
181 // TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
182 // suggest usage of `(x + (y - 1)).ln_1p()` instead
check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>)183 fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
184     if let ExprKind::Binary(
185         Spanned {
186             node: BinOpKind::Add, ..
187         },
188         lhs,
189         rhs,
190     ) = receiver.kind
191     {
192         let recv = match (
193             constant(cx, cx.typeck_results(), lhs),
194             constant(cx, cx.typeck_results(), rhs),
195         ) {
196             (Some(value), _) if F32(1.0) == value || F64(1.0) == value => rhs,
197             (_, Some(value)) if F32(1.0) == value || F64(1.0) == value => lhs,
198             _ => return,
199         };
200 
201         span_lint_and_sugg(
202             cx,
203             IMPRECISE_FLOPS,
204             expr.span,
205             "ln(1 + x) can be computed more accurately",
206             "consider using",
207             format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
208             Applicability::MachineApplicable,
209         );
210     }
211 }
212 
213 // Returns an integer if the float constant is a whole number and it can be
214 // converted to an integer without loss of precision. For now we only check
215 // ranges [-16777215, 16777216) for type f32 as whole number floats outside
216 // this range are lossy and ambiguous.
217 #[expect(clippy::cast_possible_truncation)]
get_integer_from_float_constant(value: &Constant<'_>) -> Option<i32>218 fn get_integer_from_float_constant(value: &Constant<'_>) -> Option<i32> {
219     match value {
220         F32(num) if num.fract() == 0.0 => {
221             if (-16_777_215.0..16_777_216.0).contains(num) {
222                 Some(num.round() as i32)
223             } else {
224                 None
225             }
226         },
227         F64(num) if num.fract() == 0.0 => {
228             if (-2_147_483_648.0..2_147_483_648.0).contains(num) {
229                 Some(num.round() as i32)
230             } else {
231                 None
232             }
233         },
234         _ => None,
235     }
236 }
237 
check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>])238 fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
239     // Check receiver
240     if let Some(value) = constant(cx, cx.typeck_results(), receiver) {
241         if let Some(method) = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
242             Some("exp")
243         } else if F32(2.0) == value || F64(2.0) == value {
244             Some("exp2")
245         } else {
246             None
247         } {
248             span_lint_and_sugg(
249                 cx,
250                 SUBOPTIMAL_FLOPS,
251                 expr.span,
252                 "exponent for bases 2 and e can be computed more accurately",
253                 "consider using",
254                 format!("{}.{method}()", prepare_receiver_sugg(cx, &args[0])),
255                 Applicability::MachineApplicable,
256             );
257         }
258     }
259 
260     // Check argument
261     if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
262         let (lint, help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
263             (
264                 SUBOPTIMAL_FLOPS,
265                 "square-root of a number can be computed more efficiently and accurately",
266                 format!("{}.sqrt()", Sugg::hir(cx, receiver, "..").maybe_par()),
267             )
268         } else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
269             (
270                 IMPRECISE_FLOPS,
271                 "cube-root of a number can be computed more accurately",
272                 format!("{}.cbrt()", Sugg::hir(cx, receiver, "..").maybe_par()),
273             )
274         } else if let Some(exponent) = get_integer_from_float_constant(&value) {
275             (
276                 SUBOPTIMAL_FLOPS,
277                 "exponentiation with integer powers can be computed more efficiently",
278                 format!(
279                     "{}.powi({})",
280                     Sugg::hir(cx, receiver, "..").maybe_par(),
281                     numeric_literal::format(&exponent.to_string(), None, false)
282                 ),
283             )
284         } else {
285             return;
286         };
287 
288         span_lint_and_sugg(
289             cx,
290             lint,
291             expr.span,
292             help,
293             "consider using",
294             suggestion,
295             Applicability::MachineApplicable,
296         );
297     }
298 }
299 
check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>])300 fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
301     if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
302         if value == Int(2) {
303             if let Some(parent) = get_parent_expr(cx, expr) {
304                 if let Some(grandparent) = get_parent_expr(cx, parent) {
305                     if let ExprKind::MethodCall(PathSegment { ident: method_name, .. }, receiver, ..) = grandparent.kind
306                     {
307                         if method_name.as_str() == "sqrt" && detect_hypot(cx, receiver).is_some() {
308                             return;
309                         }
310                     }
311                 }
312 
313                 if let ExprKind::Binary(
314                     Spanned {
315                         node: op @ (BinOpKind::Add | BinOpKind::Sub),
316                         ..
317                     },
318                     lhs,
319                     rhs,
320                 ) = parent.kind
321                 {
322                     let other_addend = if lhs.hir_id == expr.hir_id { rhs } else { lhs };
323 
324                     // Negate expr if original code has subtraction and expr is on the right side
325                     let maybe_neg_sugg = |expr, hir_id| {
326                         let sugg = Sugg::hir(cx, expr, "..");
327                         if matches!(op, BinOpKind::Sub) && hir_id == rhs.hir_id {
328                             format!("-{}", sugg.maybe_par())
329                         } else {
330                             sugg.to_string()
331                         }
332                     };
333 
334                     span_lint_and_sugg(
335                         cx,
336                         SUBOPTIMAL_FLOPS,
337                         parent.span,
338                         "multiply and add expressions can be calculated more efficiently and accurately",
339                         "consider using",
340                         format!(
341                             "{}.mul_add({}, {})",
342                             Sugg::hir(cx, receiver, "..").maybe_par(),
343                             maybe_neg_sugg(receiver, expr.hir_id),
344                             maybe_neg_sugg(other_addend, other_addend.hir_id),
345                         ),
346                         Applicability::MachineApplicable,
347                     );
348                 }
349             }
350         }
351     }
352 }
353 
detect_hypot(cx: &LateContext<'_>, receiver: &Expr<'_>) -> Option<String>354 fn detect_hypot(cx: &LateContext<'_>, receiver: &Expr<'_>) -> Option<String> {
355     if let ExprKind::Binary(
356         Spanned {
357             node: BinOpKind::Add, ..
358         },
359         add_lhs,
360         add_rhs,
361     ) = receiver.kind
362     {
363         // check if expression of the form x * x + y * y
364         if_chain! {
365             if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, lmul_lhs, lmul_rhs) = add_lhs.kind;
366             if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, rmul_lhs, rmul_rhs) = add_rhs.kind;
367             if eq_expr_value(cx, lmul_lhs, lmul_rhs);
368             if eq_expr_value(cx, rmul_lhs, rmul_rhs);
369             then {
370                 return Some(format!("{}.hypot({})", Sugg::hir(cx, lmul_lhs, "..").maybe_par(), Sugg::hir(cx, rmul_lhs, "..")));
371             }
372         }
373 
374         // check if expression of the form x.powi(2) + y.powi(2)
375         if_chain! {
376             if let ExprKind::MethodCall(
377                 PathSegment { ident: lmethod_name, .. },
378                 largs_0, [largs_1, ..],
379                 _
380             ) = &add_lhs.kind;
381             if let ExprKind::MethodCall(
382                 PathSegment { ident: rmethod_name, .. },
383                 rargs_0, [rargs_1, ..],
384                 _
385             ) = &add_rhs.kind;
386             if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
387             if let Some(lvalue) = constant(cx, cx.typeck_results(), largs_1);
388             if let Some(rvalue) = constant(cx, cx.typeck_results(), rargs_1);
389             if Int(2) == lvalue && Int(2) == rvalue;
390             then {
391                 return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, "..").maybe_par(), Sugg::hir(cx, rargs_0, "..")));
392             }
393         }
394     }
395 
396     None
397 }
398 
check_hypot(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>)399 fn check_hypot(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
400     if let Some(message) = detect_hypot(cx, receiver) {
401         span_lint_and_sugg(
402             cx,
403             IMPRECISE_FLOPS,
404             expr.span,
405             "hypotenuse can be computed more accurately",
406             "consider using",
407             message,
408             Applicability::MachineApplicable,
409         );
410     }
411 }
412 
413 // TODO: Lint expressions of the form `x.exp() - y` where y > 1
414 // and suggest usage of `x.exp_m1() - (y - 1)` instead
check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>)415 fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
416     if_chain! {
417         if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, lhs, rhs) = expr.kind;
418         if cx.typeck_results().expr_ty(lhs).is_floating_point();
419         if let Some(value) = constant(cx, cx.typeck_results(), rhs);
420         if F32(1.0) == value || F64(1.0) == value;
421         if let ExprKind::MethodCall(path, self_arg, ..) = &lhs.kind;
422         if cx.typeck_results().expr_ty(self_arg).is_floating_point();
423         if path.ident.name.as_str() == "exp";
424         then {
425             span_lint_and_sugg(
426                 cx,
427                 IMPRECISE_FLOPS,
428                 expr.span,
429                 "(e.pow(x) - 1) can be computed more accurately",
430                 "consider using",
431                 format!(
432                     "{}.exp_m1()",
433                     Sugg::hir(cx, self_arg, "..").maybe_par()
434                 ),
435                 Applicability::MachineApplicable,
436             );
437         }
438     }
439 }
440 
is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)>441 fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
442     if_chain! {
443         if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, lhs, rhs) = &expr.kind;
444         if cx.typeck_results().expr_ty(lhs).is_floating_point();
445         if cx.typeck_results().expr_ty(rhs).is_floating_point();
446         then {
447             return Some((lhs, rhs));
448         }
449     }
450 
451     None
452 }
453 
454 // TODO: Fix rust-lang/rust-clippy#4735
check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>)455 fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
456     if let ExprKind::Binary(
457         Spanned {
458             node: op @ (BinOpKind::Add | BinOpKind::Sub),
459             ..
460         },
461         lhs,
462         rhs,
463     ) = &expr.kind
464     {
465         if let Some(parent) = get_parent_expr(cx, expr) {
466             if let ExprKind::MethodCall(PathSegment { ident: method_name, .. }, receiver, ..) = parent.kind {
467                 if method_name.as_str() == "sqrt" && detect_hypot(cx, receiver).is_some() {
468                     return;
469                 }
470             }
471         }
472 
473         let maybe_neg_sugg = |expr| {
474             let sugg = Sugg::hir(cx, expr, "..");
475             if let BinOpKind::Sub = op {
476                 format!("-{sugg}")
477             } else {
478                 sugg.to_string()
479             }
480         };
481 
482         let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
483             (
484                 inner_lhs,
485                 Sugg::hir(cx, inner_rhs, "..").to_string(),
486                 maybe_neg_sugg(rhs),
487             )
488         } else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
489             (
490                 inner_lhs,
491                 maybe_neg_sugg(inner_rhs),
492                 Sugg::hir(cx, lhs, "..").to_string(),
493             )
494         } else {
495             return;
496         };
497 
498         span_lint_and_sugg(
499             cx,
500             SUBOPTIMAL_FLOPS,
501             expr.span,
502             "multiply and add expressions can be calculated more efficiently and accurately",
503             "consider using",
504             format!("{}.mul_add({arg1}, {arg2})", prepare_receiver_sugg(cx, recv)),
505             Applicability::MachineApplicable,
506         );
507     }
508 }
509 
510 /// Returns true iff expr is an expression which tests whether or not
511 /// test is positive or an expression which tests whether or not test
512 /// is nonnegative.
513 /// Used for check-custom-abs function below
is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool514 fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
515     if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
516         match op {
517             BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && eq_expr_value(cx, left, test),
518             BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && eq_expr_value(cx, right, test),
519             _ => false,
520         }
521     } else {
522         false
523     }
524 }
525 
526 /// See [`is_testing_positive`]
is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool527 fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
528     if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
529         match op {
530             BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && eq_expr_value(cx, right, test),
531             BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && eq_expr_value(cx, left, test),
532             _ => false,
533         }
534     } else {
535         false
536     }
537 }
538 
539 /// Returns true iff expr is some zero literal
is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool540 fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
541     match constant_simple(cx, cx.typeck_results(), expr) {
542         Some(Constant::Int(i)) => i == 0,
543         Some(Constant::F32(f)) => f == 0.0,
544         Some(Constant::F64(f)) => f == 0.0,
545         _ => false,
546     }
547 }
548 
549 /// If the two expressions are negations of each other, then it returns
550 /// a tuple, in which the first element is true iff expr1 is the
551 /// positive expressions, and the second element is the positive
552 /// one of the two expressions
553 /// If the two expressions are not negations of each other, then it
554 /// returns None.
are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)>555 fn are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)> {
556     if let ExprKind::Unary(UnOp::Neg, expr1_negated) = &expr1.kind {
557         if eq_expr_value(cx, expr1_negated, expr2) {
558             return Some((false, expr2));
559         }
560     }
561     if let ExprKind::Unary(UnOp::Neg, expr2_negated) = &expr2.kind {
562         if eq_expr_value(cx, expr1, expr2_negated) {
563             return Some((true, expr1));
564         }
565     }
566     None
567 }
568 
check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>)569 fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
570     if_chain! {
571         if let Some(higher::If { cond, then, r#else: Some(r#else) }) = higher::If::hir(expr);
572         let if_body_expr = peel_blocks(then);
573         let else_body_expr = peel_blocks(r#else);
574         if let Some((if_expr_positive, body)) = are_negated(cx, if_body_expr, else_body_expr);
575         then {
576             let positive_abs_sugg = (
577                 "manual implementation of `abs` method",
578                 format!("{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
579             );
580             let negative_abs_sugg = (
581                 "manual implementation of negation of `abs` method",
582                 format!("-{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
583             );
584             let sugg = if is_testing_positive(cx, cond, body) {
585                 if if_expr_positive {
586                     positive_abs_sugg
587                 } else {
588                     negative_abs_sugg
589                 }
590             } else if is_testing_negative(cx, cond, body) {
591                 if if_expr_positive {
592                     negative_abs_sugg
593                 } else {
594                     positive_abs_sugg
595                 }
596             } else {
597                 return;
598             };
599             span_lint_and_sugg(
600                 cx,
601                 SUBOPTIMAL_FLOPS,
602                 expr.span,
603                 sugg.0,
604                 "try",
605                 sugg.1,
606                 Applicability::MachineApplicable,
607             );
608         }
609     }
610 }
611 
are_same_base_logs(cx: &LateContext<'_>, expr_a: &Expr<'_>, expr_b: &Expr<'_>) -> bool612 fn are_same_base_logs(cx: &LateContext<'_>, expr_a: &Expr<'_>, expr_b: &Expr<'_>) -> bool {
613     if_chain! {
614         if let ExprKind::MethodCall(PathSegment { ident: method_name_a, .. }, _, args_a, _) = expr_a.kind;
615         if let ExprKind::MethodCall(PathSegment { ident: method_name_b, .. }, _, args_b, _) = expr_b.kind;
616         then {
617             return method_name_a.as_str() == method_name_b.as_str() &&
618                 args_a.len() == args_b.len() &&
619                 (
620                     ["ln", "log2", "log10"].contains(&method_name_a.as_str()) ||
621                     method_name_a.as_str() == "log" && args_a.len() == 1 && eq_expr_value(cx, &args_a[0], &args_b[0])
622                 );
623         }
624     }
625 
626     false
627 }
628 
check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>)629 fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
630     // check if expression of the form x.logN() / y.logN()
631     if_chain! {
632         if let ExprKind::Binary(
633             Spanned {
634                 node: BinOpKind::Div, ..
635             },
636             lhs,
637             rhs,
638         ) = &expr.kind;
639         if are_same_base_logs(cx, lhs, rhs);
640         if let ExprKind::MethodCall(_, largs_self, ..) = &lhs.kind;
641         if let ExprKind::MethodCall(_, rargs_self, ..) = &rhs.kind;
642         then {
643             span_lint_and_sugg(
644                 cx,
645                 SUBOPTIMAL_FLOPS,
646                 expr.span,
647                 "log base can be expressed more clearly",
648                 "consider using",
649                 format!("{}.log({})", Sugg::hir(cx, largs_self, "..").maybe_par(), Sugg::hir(cx, rargs_self, ".."),),
650                 Applicability::MachineApplicable,
651             );
652         }
653     }
654 }
655 
check_radians(cx: &LateContext<'_>, expr: &Expr<'_>)656 fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
657     if_chain! {
658         if let ExprKind::Binary(
659             Spanned {
660                 node: BinOpKind::Div, ..
661             },
662             div_lhs,
663             div_rhs,
664         ) = &expr.kind;
665         if let ExprKind::Binary(
666             Spanned {
667                 node: BinOpKind::Mul, ..
668             },
669             mul_lhs,
670             mul_rhs,
671         ) = &div_lhs.kind;
672         if let Some(rvalue) = constant(cx, cx.typeck_results(), div_rhs);
673         if let Some(lvalue) = constant(cx, cx.typeck_results(), mul_rhs);
674         then {
675             // TODO: also check for constant values near PI/180 or 180/PI
676             if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
677                (F32(180_f32) == lvalue || F64(180_f64) == lvalue)
678             {
679                 let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
680                 if_chain! {
681                     if let ExprKind::Lit(literal) = mul_lhs.kind;
682                     if let ast::LitKind::Float(ref value, float_type) = literal.node;
683                     if float_type == ast::LitFloatType::Unsuffixed;
684                     then {
685                         if value.as_str().ends_with('.') {
686                             proposal = format!("{}0_f64.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
687                         } else {
688                             proposal = format!("{}_f64.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
689                         }
690                     }
691                 }
692                 span_lint_and_sugg(
693                     cx,
694                     SUBOPTIMAL_FLOPS,
695                     expr.span,
696                     "conversion to degrees can be done more accurately",
697                     "consider using",
698                     proposal,
699                     Applicability::MachineApplicable,
700                 );
701             } else if
702                 (F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
703                 (F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
704             {
705                 let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
706                 if_chain! {
707                     if let ExprKind::Lit(literal) = mul_lhs.kind;
708                     if let ast::LitKind::Float(ref value, float_type) = literal.node;
709                     if float_type == ast::LitFloatType::Unsuffixed;
710                     then {
711                         if value.as_str().ends_with('.') {
712                             proposal = format!("{}0_f64.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
713                         } else {
714                             proposal = format!("{}_f64.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
715                         }
716                     }
717                 }
718                 span_lint_and_sugg(
719                     cx,
720                     SUBOPTIMAL_FLOPS,
721                     expr.span,
722                     "conversion to radians can be done more accurately",
723                     "consider using",
724                     proposal,
725                     Applicability::MachineApplicable,
726                 );
727             }
728         }
729     }
730 }
731 
732 impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>)733     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
734         // All of these operations are currently not const and are in std.
735         if in_constant(cx, expr.hir_id) {
736             return;
737         }
738 
739         if let ExprKind::MethodCall(path, receiver, args, _) = &expr.kind {
740             let recv_ty = cx.typeck_results().expr_ty(receiver);
741 
742             if recv_ty.is_floating_point() && !is_no_std_crate(cx) {
743                 match path.ident.name.as_str() {
744                     "ln" => check_ln1p(cx, expr, receiver),
745                     "log" => check_log_base(cx, expr, receiver, args),
746                     "powf" => check_powf(cx, expr, receiver, args),
747                     "powi" => check_powi(cx, expr, receiver, args),
748                     "sqrt" => check_hypot(cx, expr, receiver),
749                     _ => {},
750                 }
751             }
752         } else {
753             if !is_no_std_crate(cx) {
754                 check_expm1(cx, expr);
755                 check_mul_add(cx, expr);
756                 check_custom_abs(cx, expr);
757                 check_log_division(cx, expr);
758             }
759             check_radians(cx, expr);
760         }
761     }
762 }
763