Skip to content

Commit 26a6891

Browse files
committed
Fix missing parens in suboptimal_flops sugg
Fixes rust-lang#9391
1 parent 28ec27b commit 26a6891

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
651651
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
652652
(F32(180_f32) == lvalue || F64(180_f64) == lvalue)
653653
{
654-
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
654+
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
655655
if_chain! {
656656
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
657657
if let ast::LitKind::Float(ref value, float_type) = literal.node;
@@ -677,7 +677,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
677677
(F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
678678
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
679679
{
680-
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
680+
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
681681
if_chain! {
682682
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
683683
if let ast::LitKind::Float(ref value, float_type) = literal.node;

tests/ui/floating_point_rad.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub const fn const_context() {
88
let _ = x * 180f32 / std::f32::consts::PI;
99
}
1010

11+
pub fn issue9391(degrees: i64) {
12+
let _ = (degrees as f64).to_radians();
13+
let _ = (degrees as f64).to_degrees();
14+
}
15+
1116
fn main() {
1217
let x = 3f32;
1318
let _ = x.to_degrees();

tests/ui/floating_point_rad.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub const fn const_context() {
88
let _ = x * 180f32 / std::f32::consts::PI;
99
}
1010

11+
pub fn issue9391(degrees: i64) {
12+
let _ = degrees as f64 * std::f64::consts::PI / 180.0;
13+
let _ = degrees as f64 * 180.0 / std::f64::consts::PI;
14+
}
15+
1116
fn main() {
1217
let x = 3f32;
1318
let _ = x * 180f32 / std::f32::consts::PI;

tests/ui/floating_point_rad.stderr

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1+
error: conversion to radians can be done more accurately
2+
--> $DIR/floating_point_rad.rs:12:13
3+
|
4+
LL | let _ = degrees as f64 * std::f64::consts::PI / 180.0;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_radians()`
6+
|
7+
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
8+
19
error: conversion to degrees can be done more accurately
210
--> $DIR/floating_point_rad.rs:13:13
311
|
12+
LL | let _ = degrees as f64 * 180.0 / std::f64::consts::PI;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_degrees()`
14+
15+
error: conversion to degrees can be done more accurately
16+
--> $DIR/floating_point_rad.rs:18:13
17+
|
418
LL | let _ = x * 180f32 / std::f32::consts::PI;
519
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
6-
|
7-
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
820

921
error: conversion to degrees can be done more accurately
10-
--> $DIR/floating_point_rad.rs:14:13
22+
--> $DIR/floating_point_rad.rs:19:13
1123
|
1224
LL | let _ = 90. * 180f64 / std::f64::consts::PI;
1325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_degrees()`
1426

1527
error: conversion to degrees can be done more accurately
16-
--> $DIR/floating_point_rad.rs:15:13
28+
--> $DIR/floating_point_rad.rs:20:13
1729
|
1830
LL | let _ = 90.5 * 180f64 / std::f64::consts::PI;
1931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_degrees()`
2032

2133
error: conversion to radians can be done more accurately
22-
--> $DIR/floating_point_rad.rs:16:13
34+
--> $DIR/floating_point_rad.rs:21:13
2335
|
2436
LL | let _ = x * std::f32::consts::PI / 180f32;
2537
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()`
2638

2739
error: conversion to radians can be done more accurately
28-
--> $DIR/floating_point_rad.rs:17:13
40+
--> $DIR/floating_point_rad.rs:22:13
2941
|
3042
LL | let _ = 90. * std::f32::consts::PI / 180f32;
3143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_radians()`
3244

3345
error: conversion to radians can be done more accurately
34-
--> $DIR/floating_point_rad.rs:18:13
46+
--> $DIR/floating_point_rad.rs:23:13
3547
|
3648
LL | let _ = 90.5 * std::f32::consts::PI / 180f32;
3749
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_radians()`
3850

39-
error: aborting due to 6 previous errors
51+
error: aborting due to 8 previous errors
4052

0 commit comments

Comments
 (0)