Skip to content

Commit 97ad4cb

Browse files
committed
Lint for to_radians and to_degrees
1 parent 237309b commit 97ad4cb

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+50
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,55 @@ fn check_logbase(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
617617
}
618618
}
619619

620+
fn check_radians(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
621+
if_chain! {
622+
if let ExprKind::Binary(
623+
Spanned {
624+
node: BinOpKind::Div, ..
625+
},
626+
div_lhs,
627+
div_rhs,
628+
) = &expr.kind;
629+
if let ExprKind::Binary(
630+
Spanned {
631+
node: BinOpKind::Mul, ..
632+
},
633+
mul_lhs,
634+
mul_rhs,
635+
) = &div_lhs.kind;
636+
if let Some((rvalue, _)) = constant(cx, cx.tables, div_rhs);
637+
if let Some((lvalue, _)) = constant(cx, cx.tables, mul_rhs);
638+
then {
639+
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
640+
(F32(180f32) == lvalue || F64(180f64) == lvalue)
641+
{
642+
span_lint_and_sugg(
643+
cx,
644+
IMPRECISE_FLOPS,
645+
expr.span,
646+
"conversion to degrees can be done more accurately",
647+
"consider using",
648+
format!("{}.to_degrees()", Sugg::hir(cx, &mul_lhs, "..")),
649+
Applicability::MachineApplicable,
650+
);
651+
} else if
652+
(F32(180f32) == rvalue || F64(180f64) == rvalue) &&
653+
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
654+
{
655+
span_lint_and_sugg(
656+
cx,
657+
IMPRECISE_FLOPS,
658+
expr.span,
659+
"conversion to radians can be done more accurately",
660+
"consider using",
661+
format!("{}.to_radians()", Sugg::hir(cx, &mul_lhs, "..")),
662+
Applicability::MachineApplicable,
663+
);
664+
}
665+
}
666+
}
667+
}
668+
620669
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
621670
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
622671
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
@@ -637,6 +686,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
637686
check_mul_add(cx, expr);
638687
check_custom_abs(cx, expr);
639688
check_logbase(cx, expr);
689+
check_radians(cx, expr);
640690
}
641691
}
642692
}

tests/ui/floating_point_rad.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![warn(clippy::imprecise_flops)]
3+
4+
fn main() {
5+
let x = 3f32;
6+
let _ = x.to_degrees();
7+
let _ = x.to_radians();
8+
// Cases where the lint shouldn't be applied
9+
let _ = x * 90f32 / std::f32::consts::PI;
10+
let _ = x * std::f32::consts::PI / 90f32;
11+
let _ = x * 180f32 / std::f32::consts::E;
12+
let _ = x * std::f32::consts::E / 180f32;
13+
}

tests/ui/floating_point_rad.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
#![warn(clippy::imprecise_flops)]
3+
4+
fn main() {
5+
let x = 3f32;
6+
let _ = x * 180f32 / std::f32::consts::PI;
7+
let _ = x * std::f32::consts::PI / 180f32;
8+
// Cases where the lint shouldn't be applied
9+
let _ = x * 90f32 / std::f32::consts::PI;
10+
let _ = x * std::f32::consts::PI / 90f32;
11+
let _ = x * 180f32 / std::f32::consts::E;
12+
let _ = x * std::f32::consts::E / 180f32;
13+
}

tests/ui/floating_point_rad.stderr

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

0 commit comments

Comments
 (0)