Detect usage of (x + 1).ln() and suggest x.ln_1p() instead

This commit is contained in:
Krishna Veera Reddy 2019-12-20 20:07:03 -08:00 committed by Krishna Sai Veera Reddy
parent a60ae5d31c
commit de07c84903
3 changed files with 84 additions and 24 deletions

View file

@ -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()` // TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
// where y > 1 and suggest usage of `(x + (y - 1)).ln_1p()` instead // suggest usage of `(x + (y - 1)).ln_1p()` instead
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) { fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
if_chain! { if_chain! {
if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind; if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind;
if op.node == BinOpKind::Add; if op.node == BinOpKind::Add;
if let Some((value, _)) = constant(cx, cx.tables, lhs);
if F32(1.0) == value || F64(1.0) == value;
then { 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( span_lint_and_sugg(
cx, cx,
@ -111,7 +113,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
expr.span, expr.span,
"ln(1 + x) can be computed more accurately", "ln(1 + x) can be computed more accurately",
"consider using", "consider using",
format!("{}.ln_1p()", arg), format!("{}.ln_1p()", sugg::Sugg::hir(cx, recv, "..").maybe_par()),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }

View file

@ -25,18 +25,28 @@ fn check_ln1p() {
let _ = (1.0 + x.powi(2)).ln(); let _ = (1.0 + x.powi(2)).ln();
let _ = (1.0 + x.powi(2) * 2.0).ln(); let _ = (1.0 + x.powi(2) * 2.0).ln();
let _ = (1.0 + (std::f32::consts::E - 1.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 + 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 _ = (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 _ = (1.0 + x - 2.0).ln();
let x = 1f64; let x = 1f64;
let _ = (1.0 + x).ln(); let _ = (1.0 + x).ln();
let _ = (1.0 + x * 2.0).ln(); let _ = (1.0 + x * 2.0).ln();
let _ = (1.0 + x.powi(2)).ln(); let _ = (1.0 + x.powi(2)).ln();
// Cases where the lint shouldn't be applied
let _ = (x + 1.0).ln(); 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 _ = (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 _ = (1.0 + x - 2.0).ln();
} }

View file

@ -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()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `((std::f32::consts::E - 1.0)).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()` | ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x * 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x.powi(2)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^ 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) 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(); LL | let _ = x.log2() / y.log2();
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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(); LL | let _ = x.log10() / y.log10();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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(); LL | let _ = x.ln() / y.ln();
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(4.0) / y.log(4.0);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(b) / y.log(b);
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(b) / 2f32.log(b);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
error: x.log(b) / y.log(b) can be reduced to x.log(y) 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(); LL | let _ = x.log2() / y.log2();
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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(); LL | let _ = x.log10() / y.log10();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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(); LL | let _ = x.ln() / y.ln();
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(4.0) / y.log(4.0);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(b) / y.log(b);
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: x.log(b) / y.log(b) can be reduced to 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); LL | let _ = x.log(b) / 2f64.log(b);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
error: aborting due to 28 previous errors error: aborting due to 36 previous errors