about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKrishna Veera Reddy <veerareddy@email.arizona.edu>2019-12-20 20:07:03 -0800
committerKrishna Sai Veera Reddy <veerareddy@email.arizona.edu>2020-02-23 22:20:34 -0800
commitde07c8490332c17deafa89d29724a6483a1765b3 (patch)
tree81ee03520f23af7f02a0f6dbc4275913f978edfd
parenta60ae5d31cea7dff1a5b02b958d3295b474a7c9f (diff)
downloadrust-de07c8490332c17deafa89d29724a6483a1765b3.tar.gz
rust-de07c8490332c17deafa89d29724a6483a1765b3.zip
Detect usage of `(x + 1).ln()` and suggest `x.ln_1p()` instead
-rw-r--r--clippy_lints/src/floating_point_arithmetic.rs14
-rw-r--r--tests/ui/floating_point_log.rs14
-rw-r--r--tests/ui/floating_point_log.stderr80
3 files changed, 84 insertions, 24 deletions
diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs
index 7f6dac87d04..6a135126078 100644
--- a/clippy_lints/src/floating_point_arithmetic.rs
+++ b/clippy_lints/src/floating_point_arithmetic.rs
@@ -94,16 +94,18 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
     }
 }
 
-// TODO: Lint expressions of the form `(x + 1).ln()` and `(x + y).ln()`
-// where y > 1 and suggest usage of `(x + (y - 1)).ln_1p()` instead
+// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
+// suggest usage of `(x + (y - 1)).ln_1p()` instead
 fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
     if_chain! {
         if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind;
         if op.node == BinOpKind::Add;
-        if let Some((value, _)) = constant(cx, cx.tables, lhs);
-        if F32(1.0) == value || F64(1.0) == value;
         then {
-            let arg = sugg::Sugg::hir(cx, rhs, "..").maybe_par();
+            let recv = match (constant(cx, cx.tables, lhs), constant(cx, cx.tables, rhs)) {
+                (Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
+                (_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
+                _ => return,
+            };
 
             span_lint_and_sugg(
                 cx,
@@ -111,7 +113,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
                 expr.span,
                 "ln(1 + x) can be computed more accurately",
                 "consider using",
-                format!("{}.ln_1p()", arg),
+                format!("{}.ln_1p()", sugg::Sugg::hir(cx, recv, "..").maybe_par()),
                 Applicability::MachineApplicable,
             );
         }
diff --git a/tests/ui/floating_point_log.rs b/tests/ui/floating_point_log.rs
index 4c01d5e716b..9a46d2803be 100644
--- a/tests/ui/floating_point_log.rs
+++ b/tests/ui/floating_point_log.rs
@@ -25,18 +25,28 @@ fn check_ln1p() {
     let _ = (1.0 + x.powi(2)).ln();
     let _ = (1.0 + x.powi(2) * 2.0).ln();
     let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
-    // Cases where the lint shouldn't be applied
     let _ = (x + 1.0).ln();
+    let _ = (x.powi(2) + 1.0).ln();
+    let _ = (x + 2.0 + 1.0).ln();
+    let _ = (x * 2.0 + 1.0).ln();
+    // Cases where the lint shouldn't be applied
     let _ = (1.0 + x + 2.0).ln();
+    let _ = (x + 1.0 + 2.0).ln();
+    let _ = (x + 1.0 * 2.0).ln();
     let _ = (1.0 + x - 2.0).ln();
 
     let x = 1f64;
     let _ = (1.0 + x).ln();
     let _ = (1.0 + x * 2.0).ln();
     let _ = (1.0 + x.powi(2)).ln();
-    // Cases where the lint shouldn't be applied
     let _ = (x + 1.0).ln();
+    let _ = (x.powi(2) + 1.0).ln();
+    let _ = (x + 2.0 + 1.0).ln();
+    let _ = (x * 2.0 + 1.0).ln();
+    // Cases where the lint shouldn't be applied
     let _ = (1.0 + x + 2.0).ln();
+    let _ = (x + 1.0 + 2.0).ln();
+    let _ = (x + 1.0 * 2.0).ln();
     let _ = (1.0 + x - 2.0).ln();
 }
 
diff --git a/tests/ui/floating_point_log.stderr b/tests/ui/floating_point_log.stderr
index 0a36f8fc69f..6ae9de02f2c 100644
--- a/tests/ui/floating_point_log.stderr
+++ b/tests/ui/floating_point_log.stderr
@@ -79,94 +79,142 @@ 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_log.rs:34:13
+  --> $DIR/floating_point_log.rs:28:13
+   |
+LL |     let _ = (x + 1.0).ln();
+   |             ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:29:13
+   |
+LL |     let _ = (x.powi(2) + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2).ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:30:13
+   |
+LL |     let _ = (x + 2.0 + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:31:13
+   |
+LL |     let _ = (x * 2.0 + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:39: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_log.rs:35:13
+  --> $DIR/floating_point_log.rs:40: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_log.rs:36:13
+  --> $DIR/floating_point_log.rs:41: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_log.rs:42:13
+   |
+LL |     let _ = (x + 1.0).ln();
+   |             ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:43:13
+   |
+LL |     let _ = (x.powi(2) + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2).ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:44:13
+   |
+LL |     let _ = (x + 2.0 + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
+
+error: ln(1 + x) can be computed more accurately
+  --> $DIR/floating_point_log.rs:45:13
+   |
+LL |     let _ = (x * 2.0 + 1.0).ln();
+   |             ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
+
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:48:13
+  --> $DIR/floating_point_log.rs:58:13
    |
 LL |     let _ = x.log2() / y.log2();
    |             ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:49:13
+  --> $DIR/floating_point_log.rs:59:13
    |
 LL |     let _ = x.log10() / y.log10();
    |             ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:50:13
+  --> $DIR/floating_point_log.rs:60:13
    |
 LL |     let _ = x.ln() / y.ln();
    |             ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:51:13
+  --> $DIR/floating_point_log.rs:61:13
    |
 LL |     let _ = x.log(4.0) / y.log(4.0);
    |             ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:52:13
+  --> $DIR/floating_point_log.rs:62:13
    |
 LL |     let _ = x.log(b) / y.log(b);
    |             ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:54:13
+  --> $DIR/floating_point_log.rs:64:13
    |
 LL |     let _ = x.log(b) / 2f32.log(b);
    |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:60:13
+  --> $DIR/floating_point_log.rs:70:13
    |
 LL |     let _ = x.log2() / y.log2();
    |             ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:61:13
+  --> $DIR/floating_point_log.rs:71:13
    |
 LL |     let _ = x.log10() / y.log10();
    |             ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:62:13
+  --> $DIR/floating_point_log.rs:72:13
    |
 LL |     let _ = x.ln() / y.ln();
    |             ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:63:13
+  --> $DIR/floating_point_log.rs:73:13
    |
 LL |     let _ = x.log(4.0) / y.log(4.0);
    |             ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:64:13
+  --> $DIR/floating_point_log.rs:74:13
    |
 LL |     let _ = x.log(b) / y.log(b);
    |             ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
 
 error: x.log(b) / y.log(b) can be reduced to x.log(y)
-  --> $DIR/floating_point_log.rs:66:13
+  --> $DIR/floating_point_log.rs:76:13
    |
 LL |     let _ = x.log(b) / 2f64.log(b);
    |             ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
 
-error: aborting due to 28 previous errors
+error: aborting due to 36 previous errors