Skip to content

Commit a139949

Browse files
committed
Auto merge of #8187 - ApamNapat:fix_7651, r=llogiq
Fixed issues with to_radians and to_degrees lints fixes #7651 I fixed the original problem as described in the issue, but the bug remains for complex expressions (the commented out TC I added is an example). I would also love some feedback on how to cleanup my code and reduce duplication. I hope it's not a problem that the issue has been claimed by someone else - that was over two months ago. changelog: ``[`suboptimal_flops`]`` no longer proposes broken code with `to_radians` and `to_degrees`
2 parents 56ccd30 + d5c4119 commit a139949

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,26 +654,52 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
654654
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
655655
(F32(180_f32) == lvalue || F64(180_f64) == lvalue)
656656
{
657+
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
658+
if_chain! {
659+
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
660+
if let ast::LitKind::Float(ref value, float_type) = literal.node;
661+
if float_type == ast::LitFloatType::Unsuffixed;
662+
then {
663+
if value.as_str().ends_with('.') {
664+
proposal = format!("{}0_f64.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
665+
} else {
666+
proposal = format!("{}_f64.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
667+
}
668+
}
669+
}
657670
span_lint_and_sugg(
658671
cx,
659672
SUBOPTIMAL_FLOPS,
660673
expr.span,
661674
"conversion to degrees can be done more accurately",
662675
"consider using",
663-
format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, "..")),
676+
proposal,
664677
Applicability::MachineApplicable,
665678
);
666679
} else if
667680
(F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
668681
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
669682
{
683+
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
684+
if_chain! {
685+
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
686+
if let ast::LitKind::Float(ref value, float_type) = literal.node;
687+
if float_type == ast::LitFloatType::Unsuffixed;
688+
then {
689+
if value.as_str().ends_with('.') {
690+
proposal = format!("{}0_f64.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
691+
} else {
692+
proposal = format!("{}_f64.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
693+
}
694+
}
695+
}
670696
span_lint_and_sugg(
671697
cx,
672698
SUBOPTIMAL_FLOPS,
673699
expr.span,
674700
"conversion to radians can be done more accurately",
675701
"consider using",
676-
format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, "..")),
702+
proposal,
677703
Applicability::MachineApplicable,
678704
);
679705
}

tests/ui/floating_point_rad.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ pub const fn const_context() {
1111
fn main() {
1212
let x = 3f32;
1313
let _ = x.to_degrees();
14+
let _ = 90.0_f64.to_degrees();
15+
let _ = 90.5_f64.to_degrees();
1416
let _ = x.to_radians();
17+
let _ = 90.0_f64.to_radians();
18+
let _ = 90.5_f64.to_radians();
19+
// let _ = 90.5 * 80. * std::f32::consts::PI / 180f32;
1520
// Cases where the lint shouldn't be applied
1621
let _ = x * 90f32 / std::f32::consts::PI;
1722
let _ = x * std::f32::consts::PI / 90f32;

tests/ui/floating_point_rad.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ pub const fn const_context() {
1111
fn main() {
1212
let x = 3f32;
1313
let _ = x * 180f32 / std::f32::consts::PI;
14+
let _ = 90. * 180f64 / std::f64::consts::PI;
15+
let _ = 90.5 * 180f64 / std::f64::consts::PI;
1416
let _ = x * std::f32::consts::PI / 180f32;
17+
let _ = 90. * std::f32::consts::PI / 180f32;
18+
let _ = 90.5 * std::f32::consts::PI / 180f32;
19+
// let _ = 90.5 * 80. * std::f32::consts::PI / 180f32;
1520
// Cases where the lint shouldn't be applied
1621
let _ = x * 90f32 / std::f32::consts::PI;
1722
let _ = x * std::f32::consts::PI / 90f32;

tests/ui/floating_point_rad.stderr

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,35 @@ LL | let _ = x * 180f32 / std::f32::consts::PI;
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

9-
error: conversion to radians can be done more accurately
9+
error: conversion to degrees can be done more accurately
1010
--> $DIR/floating_point_rad.rs:14:13
1111
|
12+
LL | let _ = 90. * 180f64 / std::f64::consts::PI;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_degrees()`
14+
15+
error: conversion to degrees can be done more accurately
16+
--> $DIR/floating_point_rad.rs:15:13
17+
|
18+
LL | let _ = 90.5 * 180f64 / std::f64::consts::PI;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_degrees()`
20+
21+
error: conversion to radians can be done more accurately
22+
--> $DIR/floating_point_rad.rs:16:13
23+
|
1224
LL | let _ = x * std::f32::consts::PI / 180f32;
1325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()`
1426

15-
error: aborting due to 2 previous errors
27+
error: conversion to radians can be done more accurately
28+
--> $DIR/floating_point_rad.rs:17:13
29+
|
30+
LL | let _ = 90. * std::f32::consts::PI / 180f32;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_radians()`
32+
33+
error: conversion to radians can be done more accurately
34+
--> $DIR/floating_point_rad.rs:18:13
35+
|
36+
LL | let _ = 90.5 * std::f32::consts::PI / 180f32;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_radians()`
38+
39+
error: aborting due to 6 previous errors
1640

0 commit comments

Comments
 (0)