Lint for to_radians and to_degrees

This commit is contained in:
Thiago Arrais 2020-06-10 13:52:00 -03:00
parent 0c8afa39ce
commit 076ec872ce
4 changed files with 104 additions and 2 deletions

View File

@ -350,8 +350,18 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
// check if expression of the form x.powi(2) + y.powi(2)
if_chain! {
if let ExprKind::MethodCall(PathSegment { ident: lmethod_name, .. }, ref _lspan, ref largs, _) = add_lhs.kind;
if let ExprKind::MethodCall(PathSegment { ident: rmethod_name, .. }, ref _rspan, ref rargs, _) = add_rhs.kind;
if let ExprKind::MethodCall(
PathSegment { ident: lmethod_name, .. },
ref _lspan,
ref largs,
_
) = add_lhs.kind;
if let ExprKind::MethodCall(
PathSegment { ident: rmethod_name, .. },
ref _rspan,
ref rargs,
_
) = add_rhs.kind;
if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
if let Some((lvalue, _)) = constant(cx, cx.tables(), &largs[1]);
if let Some((rvalue, _)) = constant(cx, cx.tables(), &rargs[1]);
@ -617,6 +627,55 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
}
}
fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
if_chain! {
if let ExprKind::Binary(
Spanned {
node: BinOpKind::Div, ..
},
div_lhs,
div_rhs,
) = &expr.kind;
if let ExprKind::Binary(
Spanned {
node: BinOpKind::Mul, ..
},
mul_lhs,
mul_rhs,
) = &div_lhs.kind;
if let Some((rvalue, _)) = constant(cx, cx.tables(), div_rhs);
if let Some((lvalue, _)) = constant(cx, cx.tables(), mul_rhs);
then {
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
(F32(180_f32) == lvalue || F64(180_f64) == lvalue)
{
span_lint_and_sugg(
cx,
IMPRECISE_FLOPS,
expr.span,
"conversion to degrees can be done more accurately",
"consider using",
format!("{}.to_degrees()", Sugg::hir(cx, &mul_lhs, "..")),
Applicability::MachineApplicable,
);
} else if
(F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
{
span_lint_and_sugg(
cx,
IMPRECISE_FLOPS,
expr.span,
"conversion to radians can be done more accurately",
"consider using",
format!("{}.to_radians()", Sugg::hir(cx, &mul_lhs, "..")),
Applicability::MachineApplicable,
);
}
}
}
}
impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(ref path, _, args, _) = &expr.kind {
@ -637,6 +696,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
check_mul_add(cx, expr);
check_custom_abs(cx, expr);
check_log_division(cx, expr);
check_radians(cx, expr);
}
}
}

View File

@ -0,0 +1,13 @@
// run-rustfix
#![warn(clippy::imprecise_flops)]
fn main() {
let x = 3f32;
let _ = x.to_degrees();
let _ = x.to_radians();
// Cases where the lint shouldn't be applied
let _ = x * 90f32 / std::f32::consts::PI;
let _ = x * std::f32::consts::PI / 90f32;
let _ = x * 180f32 / std::f32::consts::E;
let _ = x * std::f32::consts::E / 180f32;
}

View File

@ -0,0 +1,13 @@
// run-rustfix
#![warn(clippy::imprecise_flops)]
fn main() {
let x = 3f32;
let _ = x * 180f32 / std::f32::consts::PI;
let _ = x * std::f32::consts::PI / 180f32;
// Cases where the lint shouldn't be applied
let _ = x * 90f32 / std::f32::consts::PI;
let _ = x * std::f32::consts::PI / 90f32;
let _ = x * 180f32 / std::f32::consts::E;
let _ = x * std::f32::consts::E / 180f32;
}

View File

@ -0,0 +1,16 @@
error: conversion to degrees can be done more accurately
--> $DIR/floating_point_rad.rs:6:13
|
LL | let _ = x * 180f32 / std::f32::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
|
= note: `-D clippy::imprecise-flops` implied by `-D warnings`
error: conversion to radians can be done more accurately
--> $DIR/floating_point_rad.rs:7:13
|
LL | let _ = x * std::f32::consts::PI / 180f32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()`
error: aborting due to 2 previous errors