2019-12-14 17:28:11 +00:00
|
|
|
use crate::consts::{
|
2019-12-16 03:26:44 +00:00
|
|
|
constant, Constant,
|
2019-12-14 17:28:11 +00:00
|
|
|
Constant::{F32, F64},
|
|
|
|
};
|
2020-02-28 06:31:41 +00:00
|
|
|
use crate::utils::{higher, span_lint_and_sugg, sugg, SpanlessEq};
|
2019-12-14 17:28:11 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-04 06:19:13 +00:00
|
|
|
use rustc::ty;
|
2019-12-14 17:28:11 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-02-28 20:40:13 +00:00
|
|
|
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Lit, UnOp};
|
2020-02-17 20:56:55 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-02-17 19:38:35 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-02-24 04:48:57 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
|
|
|
|
2020-03-01 21:12:56 +00:00
|
|
|
use rustc_ast::ast::{self, FloatTy, LitFloatType, LitKind};
|
2019-12-14 17:28:11 +00:00
|
|
|
use std::f32::consts as f32_consts;
|
|
|
|
use std::f64::consts as f64_consts;
|
2020-02-23 04:29:22 +00:00
|
|
|
use sugg::{format_numeric_literal, Sugg};
|
2019-12-14 17:28:11 +00:00
|
|
|
|
2020-02-24 05:06:55 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Looks for floating-point expressions that
|
|
|
|
/// can be expressed using built-in methods to improve accuracy
|
|
|
|
/// at the cost of performance.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Negatively impacts accuracy.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// let a = 3f32;
|
|
|
|
/// let _ = a.powf(1.0 / 3.0);
|
|
|
|
/// let _ = (1.0 + a).ln();
|
|
|
|
/// let _ = a.exp() - 1.0;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// is better expressed as
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
///
|
|
|
|
/// let a = 3f32;
|
|
|
|
/// let _ = a.cbrt();
|
|
|
|
/// let _ = a.ln_1p();
|
|
|
|
/// let _ = a.exp_m1();
|
|
|
|
/// ```
|
|
|
|
pub IMPRECISE_FLOPS,
|
|
|
|
nursery,
|
|
|
|
"usage of imprecise floating point operations"
|
|
|
|
}
|
|
|
|
|
2019-12-14 17:28:11 +00:00
|
|
|
declare_clippy_lint! {
|
2019-12-15 04:10:23 +00:00
|
|
|
/// **What it does:** Looks for floating-point expressions that
|
2020-02-17 20:56:55 +00:00
|
|
|
/// can be expressed using built-in methods to improve both
|
|
|
|
/// accuracy and performance.
|
2019-12-14 17:28:11 +00:00
|
|
|
///
|
2020-02-17 20:56:55 +00:00
|
|
|
/// **Why is this bad?** Negatively impacts accuracy and performance.
|
2019-12-14 17:28:11 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::f32::consts::E;
|
|
|
|
///
|
2019-12-15 04:10:23 +00:00
|
|
|
/// let a = 3f32;
|
|
|
|
/// let _ = (2f32).powf(a);
|
|
|
|
/// let _ = E.powf(a);
|
|
|
|
/// let _ = a.powf(1.0 / 2.0);
|
|
|
|
/// let _ = a.log(2.0);
|
|
|
|
/// let _ = a.log(10.0);
|
|
|
|
/// let _ = a.log(E);
|
2019-12-16 03:26:44 +00:00
|
|
|
/// let _ = a.powf(2.0);
|
2020-02-23 08:04:11 +00:00
|
|
|
/// let _ = a * 2.0 + 4.0;
|
2020-02-28 06:31:41 +00:00
|
|
|
/// let _ = if a < 0.0 {
|
|
|
|
/// -a
|
|
|
|
/// } else {
|
|
|
|
/// a
|
|
|
|
/// }
|
|
|
|
/// let _ = if a < 0.0 {
|
|
|
|
/// a
|
|
|
|
/// } else {
|
|
|
|
/// -a
|
|
|
|
/// }
|
2019-12-14 17:28:11 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// is better expressed as
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::f32::consts::E;
|
|
|
|
///
|
2019-12-15 04:10:23 +00:00
|
|
|
/// let a = 3f32;
|
|
|
|
/// let _ = a.exp2();
|
|
|
|
/// let _ = a.exp();
|
|
|
|
/// let _ = a.sqrt();
|
|
|
|
/// let _ = a.log2();
|
|
|
|
/// let _ = a.log10();
|
|
|
|
/// let _ = a.ln();
|
2019-12-16 03:26:44 +00:00
|
|
|
/// let _ = a.powi(2);
|
2020-02-23 08:04:11 +00:00
|
|
|
/// let _ = a.mul_add(2.0, 4.0);
|
2020-02-28 06:31:41 +00:00
|
|
|
/// let _ = a.abs();
|
|
|
|
/// let _ = -a.abs();
|
2019-12-14 17:28:11 +00:00
|
|
|
/// ```
|
2020-02-17 20:56:55 +00:00
|
|
|
pub SUBOPTIMAL_FLOPS,
|
2019-12-14 17:28:11 +00:00
|
|
|
nursery,
|
2020-02-17 20:56:55 +00:00
|
|
|
"usage of sub-optimal floating point operations"
|
2019-12-14 17:28:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 05:06:55 +00:00
|
|
|
declare_lint_pass!(FloatingPointArithmetic => [
|
|
|
|
IMPRECISE_FLOPS,
|
|
|
|
SUBOPTIMAL_FLOPS
|
|
|
|
]);
|
2019-12-14 17:28:11 +00:00
|
|
|
|
2019-12-17 02:03:51 +00:00
|
|
|
// Returns the specialized log method for a given base if base is constant
|
|
|
|
// and is one of 2, 10 and e
|
2020-02-17 20:56:55 +00:00
|
|
|
fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr<'_>) -> Option<&'static str> {
|
2019-12-17 02:03:51 +00:00
|
|
|
if let Some((value, _)) = constant(cx, cx.tables, base) {
|
2019-12-14 17:28:11 +00:00
|
|
|
if F32(2.0) == value || F64(2.0) == value {
|
2019-12-17 02:03:51 +00:00
|
|
|
return Some("log2");
|
2019-12-14 17:28:11 +00:00
|
|
|
} else if F32(10.0) == value || F64(10.0) == value {
|
2019-12-17 02:03:51 +00:00
|
|
|
return Some("log10");
|
2019-12-14 17:28:11 +00:00
|
|
|
} else if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
|
2019-12-17 02:03:51 +00:00
|
|
|
return Some("ln");
|
2019-12-14 17:28:11 +00:00
|
|
|
}
|
2019-12-17 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2019-12-14 17:28:11 +00:00
|
|
|
|
2020-01-04 06:19:13 +00:00
|
|
|
// 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<'_>]) {
|
2019-12-17 02:03:51 +00:00
|
|
|
if let Some(method) = get_specialized_log_method(cx, &args[1]) {
|
2019-12-14 17:28:11 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-17 20:56:55 +00:00
|
|
|
SUBOPTIMAL_FLOPS,
|
2019-12-14 17:28:11 +00:00
|
|
|
expr.span,
|
|
|
|
"logarithm for bases 2, 10 and e can be computed more accurately",
|
|
|
|
"consider using",
|
2020-01-04 06:19:13 +00:00
|
|
|
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
|
2019-12-14 17:28:11 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 04:07:03 +00:00
|
|
|
// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
|
|
|
|
// suggest usage of `(x + (y - 1)).ln_1p()` instead
|
2020-02-17 20:56:55 +00:00
|
|
|
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
|
2020-02-24 04:48:57 +00:00
|
|
|
if let ExprKind::Binary(
|
|
|
|
Spanned {
|
|
|
|
node: BinOpKind::Add, ..
|
|
|
|
},
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
) = &args[0].kind
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
};
|
2019-12-14 17:28:11 +00:00
|
|
|
|
2020-02-24 04:48:57 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-24 05:06:55 +00:00
|
|
|
IMPRECISE_FLOPS,
|
2020-02-24 04:48:57 +00:00
|
|
|
expr.span,
|
|
|
|
"ln(1 + x) can be computed more accurately",
|
|
|
|
"consider using",
|
|
|
|
format!("{}.ln_1p()", prepare_receiver_sugg(cx, recv)),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2019-12-14 17:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 04:29:22 +00:00
|
|
|
// Returns an integer if the float constant is a whole number and it can be
|
|
|
|
// converted to an integer without loss of precision. For now we only check
|
|
|
|
// ranges [-16777215, 16777216) for type f32 as whole number floats outside
|
|
|
|
// this range are lossy and ambiguous.
|
2019-12-16 03:26:44 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2020-02-23 04:29:22 +00:00
|
|
|
fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
|
2019-12-16 03:26:44 +00:00
|
|
|
match value {
|
2020-02-23 04:29:22 +00:00
|
|
|
F32(num) if num.fract() == 0.0 => {
|
|
|
|
if (-16_777_215.0..16_777_216.0).contains(num) {
|
|
|
|
Some(num.round() as i32)
|
2019-12-16 03:26:44 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
2020-02-23 04:29:22 +00:00
|
|
|
F64(num) if num.fract() == 0.0 => {
|
|
|
|
if (-2_147_483_648.0..2_147_483_648.0).contains(num) {
|
|
|
|
Some(num.round() as i32)
|
2019-12-16 03:26:44 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 20:56:55 +00:00
|
|
|
fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
|
2019-12-14 17:28:11 +00:00
|
|
|
// Check receiver
|
|
|
|
if let Some((value, _)) = constant(cx, cx.tables, &args[0]) {
|
2020-02-23 04:29:22 +00:00
|
|
|
let method = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
|
|
|
|
"exp"
|
2019-12-14 17:28:11 +00:00
|
|
|
} else if F32(2.0) == value || F64(2.0) == value {
|
2020-02-23 04:29:22 +00:00
|
|
|
"exp2"
|
2019-12-14 17:28:11 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2020-02-23 04:29:22 +00:00
|
|
|
};
|
2019-12-14 17:28:11 +00:00
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-17 20:56:55 +00:00
|
|
|
SUBOPTIMAL_FLOPS,
|
2019-12-14 17:28:11 +00:00
|
|
|
expr.span,
|
2019-12-15 04:10:23 +00:00
|
|
|
"exponent for bases 2 and e can be computed more accurately",
|
2019-12-14 17:28:11 +00:00
|
|
|
"consider using",
|
2020-01-04 06:19:13 +00:00
|
|
|
format!("{}.{}()", prepare_receiver_sugg(cx, &args[1]), method),
|
2019-12-14 17:28:11 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check argument
|
|
|
|
if let Some((value, _)) = constant(cx, cx.tables, &args[1]) {
|
2020-02-24 05:06:55 +00:00
|
|
|
let (lint, help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
|
2020-02-23 04:29:22 +00:00
|
|
|
(
|
2020-02-24 05:06:55 +00:00
|
|
|
SUBOPTIMAL_FLOPS,
|
2020-02-23 04:29:22 +00:00
|
|
|
"square-root of a number can be computed more efficiently and accurately",
|
2020-02-23 08:04:11 +00:00
|
|
|
format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..")),
|
2020-02-23 04:29:22 +00:00
|
|
|
)
|
2019-12-14 17:28:11 +00:00
|
|
|
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
|
2020-02-23 04:29:22 +00:00
|
|
|
(
|
2020-02-24 05:06:55 +00:00
|
|
|
IMPRECISE_FLOPS,
|
2020-02-23 04:29:22 +00:00
|
|
|
"cube-root of a number can be computed more accurately",
|
2020-02-23 08:04:11 +00:00
|
|
|
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..")),
|
2020-02-23 04:29:22 +00:00
|
|
|
)
|
2019-12-16 03:26:44 +00:00
|
|
|
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
|
2020-02-23 04:29:22 +00:00
|
|
|
(
|
2020-02-24 05:06:55 +00:00
|
|
|
SUBOPTIMAL_FLOPS,
|
2019-12-16 03:26:44 +00:00
|
|
|
"exponentiation with integer powers can be computed more efficiently",
|
2020-02-23 04:29:22 +00:00
|
|
|
format!(
|
|
|
|
"{}.powi({})",
|
|
|
|
Sugg::hir(cx, &args[0], ".."),
|
|
|
|
format_numeric_literal(&exponent.to_string(), None, false)
|
2020-02-23 08:04:11 +00:00
|
|
|
),
|
2020-02-23 04:29:22 +00:00
|
|
|
)
|
2019-12-14 17:28:11 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2020-02-23 04:29:22 +00:00
|
|
|
};
|
2019-12-14 17:28:11 +00:00
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-24 05:06:55 +00:00
|
|
|
lint,
|
2019-12-14 17:28:11 +00:00
|
|
|
expr.span,
|
|
|
|
help,
|
|
|
|
"consider using",
|
2020-02-23 04:29:22 +00:00
|
|
|
suggestion,
|
2019-12-14 17:28:11 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Lint expressions of the form `x.exp() - y` where y > 1
|
|
|
|
// and suggest usage of `x.exp_m1() - (y - 1)` instead
|
2020-02-17 20:56:55 +00:00
|
|
|
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
2019-12-14 17:28:11 +00:00
|
|
|
if_chain! {
|
2020-02-24 04:48:57 +00:00
|
|
|
if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, ref lhs, ref rhs) = expr.kind;
|
2019-12-14 17:28:11 +00:00
|
|
|
if cx.tables.expr_ty(lhs).is_floating_point();
|
|
|
|
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;
|
2020-01-04 06:19:13 +00:00
|
|
|
if cx.tables.expr_ty(&method_args[0]).is_floating_point();
|
2019-12-14 17:28:11 +00:00
|
|
|
if path.ident.name.as_str() == "exp";
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-02-24 05:06:55 +00:00
|
|
|
IMPRECISE_FLOPS,
|
2019-12-14 17:28:11 +00:00
|
|
|
expr.span,
|
|
|
|
"(e.pow(x) - 1) can be computed more accurately",
|
|
|
|
"consider using",
|
|
|
|
format!(
|
|
|
|
"{}.exp_m1()",
|
2020-01-04 06:19:13 +00:00
|
|
|
Sugg::hir(cx, &method_args[0], "..")
|
2019-12-14 17:28:11 +00:00
|
|
|
),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:04:11 +00:00
|
|
|
fn is_float_mul_expr<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
|
|
|
|
if_chain! {
|
2020-02-24 04:48:57 +00:00
|
|
|
if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref lhs, ref rhs) = &expr.kind;
|
2020-02-23 08:04:11 +00:00
|
|
|
if cx.tables.expr_ty(lhs).is_floating_point();
|
|
|
|
if cx.tables.expr_ty(rhs).is_floating_point();
|
|
|
|
then {
|
|
|
|
return Some((lhs, rhs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fix rust-lang/rust-clippy#4735
|
2020-02-24 04:48:57 +00:00
|
|
|
fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
|
|
|
if let ExprKind::Binary(
|
|
|
|
Spanned {
|
|
|
|
node: BinOpKind::Add, ..
|
|
|
|
},
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
) = &expr.kind
|
|
|
|
{
|
|
|
|
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
|
|
|
|
(inner_lhs, inner_rhs, rhs)
|
|
|
|
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
|
|
|
|
(inner_lhs, inner_rhs, lhs)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2020-02-23 08:04:11 +00:00
|
|
|
|
2020-02-24 04:48:57 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
SUBOPTIMAL_FLOPS,
|
|
|
|
expr.span,
|
|
|
|
"multiply and add expressions can be calculated more efficiently and accurately",
|
|
|
|
"consider using",
|
|
|
|
format!(
|
|
|
|
"{}.mul_add({}, {})",
|
|
|
|
prepare_receiver_sugg(cx, recv),
|
|
|
|
Sugg::hir(cx, arg1, ".."),
|
|
|
|
Sugg::hir(cx, arg2, ".."),
|
|
|
|
),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2020-02-23 08:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 06:31:41 +00:00
|
|
|
/// Returns true iff expr is an expression which tests whether or not
|
|
|
|
/// test is positive or an expression which tests whether or not test
|
|
|
|
/// is nonnegative.
|
|
|
|
/// Used for check-custom-abs function below
|
|
|
|
fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
|
|
|
|
match op {
|
|
|
|
BinOpKind::Gt | BinOpKind::Ge => is_zero(right) && are_exprs_equal(cx, left, test),
|
|
|
|
BinOpKind::Lt | BinOpKind::Le => is_zero(left) && are_exprs_equal(cx, right, test),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 21:46:59 +00:00
|
|
|
fn is_testing_negative(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
|
|
|
|
match op {
|
|
|
|
BinOpKind::Gt | BinOpKind::Ge => is_zero(left) && are_exprs_equal(cx, right, test),
|
|
|
|
BinOpKind::Lt | BinOpKind::Le => is_zero(right) && are_exprs_equal(cx, left, test),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 06:31:41 +00:00
|
|
|
fn are_exprs_equal(cx: &LateContext<'_, '_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool {
|
|
|
|
SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true iff expr is some zero literal
|
|
|
|
fn is_zero(expr: &Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Lit(Lit { node: lit, .. }) = &expr.kind {
|
|
|
|
match lit {
|
|
|
|
LitKind::Int(0, _) => true,
|
|
|
|
LitKind::Float(symb, LitFloatType::Unsuffixed)
|
|
|
|
| LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F64)) => {
|
|
|
|
symb.as_str().parse::<f64>().unwrap() == 0.0
|
|
|
|
},
|
|
|
|
LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F32)) => symb.as_str().parse::<f32>().unwrap() == 0.0,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 06:37:37 +00:00
|
|
|
/// If the expressions are not opposites, return None
|
|
|
|
/// Otherwise, return true if expr2 = -expr1, false if expr1 = -expr2 and return the positive
|
|
|
|
/// expression
|
|
|
|
fn are_opposites<'a>(
|
|
|
|
cx: &LateContext<'_, '_>,
|
|
|
|
expr1: &'a Expr<'a>,
|
|
|
|
expr2: &'a Expr<'a>,
|
|
|
|
) -> Option<(bool, &'a Expr<'a>)> {
|
|
|
|
if let ExprKind::Block(
|
|
|
|
Block {
|
|
|
|
stmts: [],
|
|
|
|
expr: Some(expr1_inner),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
) = &expr1.kind
|
|
|
|
{
|
2020-03-01 02:51:39 +00:00
|
|
|
if let ExprKind::Block(
|
|
|
|
Block {
|
|
|
|
stmts: [],
|
2020-03-02 06:37:37 +00:00
|
|
|
expr: Some(expr2_inner),
|
2020-03-01 02:51:39 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
2020-03-02 06:37:37 +00:00
|
|
|
) = &expr2.kind
|
2020-03-01 02:51:39 +00:00
|
|
|
{
|
2020-03-02 06:37:37 +00:00
|
|
|
if let ExprKind::Unary(UnOp::UnNeg, expr1_neg) = &expr1_inner.kind {
|
|
|
|
if are_exprs_equal(cx, expr1_neg, expr2_inner) {
|
|
|
|
return Some((false, expr2_inner));
|
2020-02-29 21:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-02 06:37:37 +00:00
|
|
|
if let ExprKind::Unary(UnOp::UnNeg, expr2_neg) = &expr2_inner.kind {
|
|
|
|
if are_exprs_equal(cx, expr1_inner, expr2_neg) {
|
|
|
|
return Some((true, expr1_inner));
|
2020-02-28 20:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 06:37:37 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
|
|
|
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
|
|
|
|
if let Some((expr1_pos, body)) = are_opposites(cx, body, else_body) {
|
|
|
|
let pos_abs_sugg = (
|
|
|
|
"This looks like you've implemented your own absolute value function",
|
|
|
|
format!("{}.abs()", Sugg::hir(cx, body, "..")),
|
|
|
|
);
|
|
|
|
let neg_abs_sugg = (
|
|
|
|
"This looks like you've implemented your own negative absolute value function",
|
|
|
|
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
|
|
|
|
);
|
|
|
|
let sugg = if is_testing_positive(cx, cond, body) {
|
|
|
|
if expr1_pos {
|
|
|
|
pos_abs_sugg
|
|
|
|
} else {
|
|
|
|
neg_abs_sugg
|
|
|
|
}
|
|
|
|
} else if is_testing_negative(cx, cond, body) {
|
|
|
|
if expr1_pos {
|
|
|
|
neg_abs_sugg
|
|
|
|
} else {
|
|
|
|
pos_abs_sugg
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
SUBOPTIMAL_FLOPS,
|
|
|
|
expr.span,
|
|
|
|
sugg.0,
|
|
|
|
"try",
|
|
|
|
sugg.1,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 06:31:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 17:28:11 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
|
2020-02-17 20:56:55 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2019-12-14 17:28:11 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
|
|
|
|
let recv_ty = cx.tables.expr_ty(&args[0]);
|
|
|
|
|
|
|
|
if recv_ty.is_floating_point() {
|
|
|
|
match &*path.ident.name.as_str() {
|
|
|
|
"ln" => check_ln1p(cx, expr, args),
|
|
|
|
"log" => check_log_base(cx, expr, args),
|
|
|
|
"powf" => check_powf(cx, expr, args),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check_expm1(cx, expr);
|
2020-02-24 04:48:57 +00:00
|
|
|
check_mul_add(cx, expr);
|
2020-02-28 06:31:41 +00:00
|
|
|
check_custom_abs(cx, expr);
|
2019-12-14 17:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|