Add type suffixes to unsuffixed method receiver suggestions

This commit is contained in:
Krishna Veera Reddy 2020-01-03 22:19:13 -08:00 committed by Krishna Sai Veera Reddy
parent de07c84903
commit fd2506bcbf
5 changed files with 186 additions and 58 deletions

View File

@ -7,10 +7,13 @@ use if_chain::if_chain;
use rustc::declare_lint_pass;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::ty;
use rustc_errors::Applicability;
use rustc_session::declare_tool_lint;
use std::f32::consts as f32_consts;
use std::f64::consts as f64_consts;
use sugg::Sugg;
use syntax::ast;
declare_clippy_lint! {
/// **What it does:** Looks for floating-point expressions that
@ -80,7 +83,45 @@ fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr) -> Option<&
None
}
fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
// Adds type suffixes and parenthesis to method receivers if necessary
fn prepare_receiver_sugg<'a>(cx: &LateContext<'_, '_>, mut expr: &'a Expr<'a>) -> Sugg<'a> {
let mut suggestion = Sugg::hir(cx, expr, "..");
if let ExprKind::Unary(UnOp::UnNeg, inner_expr) = &expr.kind {
expr = &inner_expr;
}
if_chain! {
// if the expression is a float literal and it is unsuffixed then
// add a suffix so the suggestion is valid and unambiguous
if let ty::Float(float_ty) = cx.tables.expr_ty(expr).kind;
if let ExprKind::Lit(lit) = &expr.kind;
if let ast::LitKind::Float(sym, ast::LitFloatType::Unsuffixed) = lit.node;
then {
let op = format!(
"{}{}{}",
suggestion,
// Check for float literals without numbers following the decimal
// separator such as `2.` and adds a trailing zero
if sym.as_str().ends_with('.') {
"0"
} else {
""
},
float_ty.name_str()
).into();
suggestion = match suggestion {
Sugg::MaybeParen(_) => Sugg::MaybeParen(op),
_ => Sugg::NonParen(op)
};
}
}
suggestion.maybe_par()
}
fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
if let Some(method) = get_specialized_log_method(cx, &args[1]) {
span_lint_and_sugg(
cx,
@ -88,7 +129,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
expr.span,
"logarithm for bases 2, 10 and e can be computed more accurately",
"consider using",
format!("{}.{}()", sugg::Sugg::hir(cx, &args[0], ".."), method),
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
Applicability::MachineApplicable,
);
}
@ -113,7 +154,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()", sugg::Sugg::hir(cx, recv, "..").maybe_par()),
format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
Applicability::MachineApplicable,
);
}
@ -164,7 +205,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
expr.span,
"exponent for bases 2 and e can be computed more accurately",
"consider using",
format!("{}.{}()", sugg::Sugg::hir(cx, &args[1], "..").maybe_par(), method),
format!("{}.{}()", prepare_receiver_sugg(cx, &args[1]), method),
Applicability::MachineApplicable,
);
}
@ -187,7 +228,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
expr.span,
"exponentiation with integer powers can be computed more efficiently",
"consider using",
format!("{}.powi({})", sugg::Sugg::hir(cx, &args[0], ".."), exponent),
format!("{}.powi({})", Sugg::hir(cx, &args[0], ".."), exponent),
Applicability::MachineApplicable,
);
@ -202,7 +243,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
expr.span,
help,
"consider using",
format!("{}.{}()", sugg::Sugg::hir(cx, &args[0], ".."), method),
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
Applicability::MachineApplicable,
);
}
@ -218,6 +259,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
if let Some((value, _)) = constant(cx, cx.tables, rhs);
if F32(1.0) == value || F64(1.0) == value;
if let ExprKind::MethodCall(ref path, _, ref method_args) = lhs.kind;
if cx.tables.expr_ty(&method_args[0]).is_floating_point();
if path.ident.name.as_str() == "exp";
then {
span_lint_and_sugg(
@ -228,7 +270,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
"consider using",
format!(
"{}.exp_m1()",
sugg::Sugg::hir(cx, &method_args[0], "..")
Sugg::hir(cx, &method_args[0], "..")
),
Applicability::MachineApplicable,
);
@ -275,7 +317,9 @@ fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
if op.node == BinOpKind::Div;
if cx.tables.expr_ty(lhs).is_floating_point();
if let ExprKind::MethodCall(left_path, _, left_args) = &lhs.kind;
if cx.tables.expr_ty(&left_args[0]).is_floating_point();
if let ExprKind::MethodCall(right_path, _, right_args) = &rhs.kind;
if cx.tables.expr_ty(&right_args[0]).is_floating_point();
let left_method = left_path.ident.name.as_str();
if left_method == right_path.ident.name.as_str();
if log_methods.iter().any(|&method| left_method == method);
@ -290,12 +334,12 @@ fn check_log_division(cx: &LateContext<'_, '_>, expr: &Expr) {
// Reduce the expression further for bases 2, 10 and e
let suggestion = if let Some(method) = get_specialized_log_method(cx, right_recv) {
format!("{}.{}()", sugg::Sugg::hir(cx, left_recv, ".."), method)
format!("{}.{}()", Sugg::hir(cx, left_recv, ".."), method)
} else {
format!(
"{}.log({})",
sugg::Sugg::hir(cx, left_recv, ".."),
sugg::Sugg::hir(cx, right_recv, "..")
Sugg::hir(cx, left_recv, ".."),
Sugg::hir(cx, right_recv, "..")
)
};

View File

@ -20,6 +20,8 @@ fn check_log_base() {
fn check_ln1p() {
let x = 1f32;
let _ = (1f32 + 2.).ln();
let _ = (1f32 + 2.0).ln();
let _ = (1.0 + x).ln();
let _ = (1.0 + x * 2.0).ln();
let _ = (1.0 + x.powi(2)).ln();
@ -36,6 +38,8 @@ fn check_ln1p() {
let _ = (1.0 + x - 2.0).ln();
let x = 1f64;
let _ = (1f64 + 2.).ln();
let _ = (1f64 + 2.0).ln();
let _ = (1.0 + x).ln();
let _ = (1.0 + x * 2.0).ln();
let _ = (1.0 + x.powi(2)).ln();

View File

@ -51,170 +51,194 @@ LL | let _ = x.log(std::f64::consts::E);
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:23:13
|
LL | let _ = (1f32 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:24:13
|
LL | let _ = (1f32 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:25: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:24:13
--> $DIR/floating_point_log.rs:26: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:25:13
--> $DIR/floating_point_log.rs:27: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:26:13
--> $DIR/floating_point_log.rs:28:13
|
LL | let _ = (1.0 + x.powi(2) * 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(2) * 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:27:13
--> $DIR/floating_point_log.rs:29:13
|
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:28:13
--> $DIR/floating_point_log.rs:30: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
--> $DIR/floating_point_log.rs:31: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
--> $DIR/floating_point_log.rs:32: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
--> $DIR/floating_point_log.rs:33: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: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:41:13
|
LL | let _ = (1f64 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:42:13
|
LL | let _ = (1f64 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:43: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:44: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:45: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
--> $DIR/floating_point_log.rs:46: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
--> $DIR/floating_point_log.rs:47: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
--> $DIR/floating_point_log.rs:48: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
--> $DIR/floating_point_log.rs:49: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:58:13
--> $DIR/floating_point_log.rs:62: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:59:13
--> $DIR/floating_point_log.rs:63: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:60:13
--> $DIR/floating_point_log.rs:64: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:61:13
--> $DIR/floating_point_log.rs:65: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:62:13
--> $DIR/floating_point_log.rs:66: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:64:13
--> $DIR/floating_point_log.rs:68: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:70:13
--> $DIR/floating_point_log.rs:74: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:71:13
--> $DIR/floating_point_log.rs:75: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:72:13
--> $DIR/floating_point_log.rs:76: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:73:13
--> $DIR/floating_point_log.rs:77: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:74:13
--> $DIR/floating_point_log.rs:78: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:76:13
--> $DIR/floating_point_log.rs:80:13
|
LL | let _ = x.log(b) / 2f64.log(b);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log2()`
error: aborting due to 36 previous errors
error: aborting due to 40 previous errors

View File

@ -3,7 +3,11 @@
fn main() {
let x = 3f32;
let _ = 2f32.powf(x);
let _ = 2f32.powf(3.1);
let _ = 2f32.powf(-3.1);
let _ = std::f32::consts::E.powf(x);
let _ = std::f32::consts::E.powf(3.1);
let _ = std::f32::consts::E.powf(-3.1);
let _ = x.powf(1.0 / 2.0);
let _ = x.powf(1.0 / 3.0);
let _ = x.powf(2.0);
@ -15,7 +19,11 @@ fn main() {
let x = 3f64;
let _ = 2f64.powf(x);
let _ = 2f64.powf(3.1);
let _ = 2f64.powf(-3.1);
let _ = std::f64::consts::E.powf(x);
let _ = std::f64::consts::E.powf(3.1);
let _ = std::f64::consts::E.powf(-3.1);
let _ = x.powf(1.0 / 2.0);
let _ = x.powf(1.0 / 3.0);
let _ = x.powf(2.0);

View File

@ -9,68 +9,116 @@ LL | let _ = 2f32.powf(x);
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:6:13
|
LL | let _ = 2f32.powf(3.1);
| ^^^^^^^^^^^^^^ help: consider using: `3.1f32.exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:7:13
|
LL | let _ = 2f32.powf(-3.1);
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f32).exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:8:13
|
LL | let _ = std::f32::consts::E.powf(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:9:13
|
LL | let _ = std::f32::consts::E.powf(3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f32.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:10:13
|
LL | let _ = std::f32::consts::E.powf(-3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f32).exp()`
error: square-root of a number can be computed more efficiently and accurately
--> $DIR/floating_point_powf.rs:7:13
--> $DIR/floating_point_powf.rs:11:13
|
LL | let _ = x.powf(1.0 / 2.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
error: cube-root of a number can be computed more accurately
--> $DIR/floating_point_powf.rs:8:13
--> $DIR/floating_point_powf.rs:12:13
|
LL | let _ = x.powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:9:13
--> $DIR/floating_point_powf.rs:13:13
|
LL | let _ = x.powf(2.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(2)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:10:13
--> $DIR/floating_point_powf.rs:14:13
|
LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:17:13
--> $DIR/floating_point_powf.rs:21:13
|
LL | let _ = 2f64.powf(x);
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:18:13
--> $DIR/floating_point_powf.rs:22:13
|
LL | let _ = 2f64.powf(3.1);
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:23:13
|
LL | let _ = 2f64.powf(-3.1);
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:24:13
|
LL | let _ = std::f64::consts::E.powf(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:25:13
|
LL | let _ = std::f64::consts::E.powf(3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:26:13
|
LL | let _ = std::f64::consts::E.powf(-3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
error: square-root of a number can be computed more efficiently and accurately
--> $DIR/floating_point_powf.rs:19:13
--> $DIR/floating_point_powf.rs:27:13
|
LL | let _ = x.powf(1.0 / 2.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
error: cube-root of a number can be computed more accurately
--> $DIR/floating_point_powf.rs:20:13
--> $DIR/floating_point_powf.rs:28:13
|
LL | let _ = x.powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:21:13
--> $DIR/floating_point_powf.rs:29:13
|
LL | let _ = x.powf(2.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(2)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:22:13
--> $DIR/floating_point_powf.rs:30:13
|
LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: aborting due to 12 previous errors
error: aborting due to 20 previous errors