Skip to content

Commit

Permalink
Fix missing parens in suboptimal_flops sugg
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslueg committed Aug 29, 2022
1 parent 28ec27b commit 26a6891
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
(F32(180_f32) == lvalue || F64(180_f64) == lvalue)
{
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, ".."));
let mut proposal = format!("{}.to_degrees()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
if_chain! {
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
if let ast::LitKind::Float(ref value, float_type) = literal.node;
Expand All @@ -677,7 +677,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
(F32(180_f32) == rvalue || F64(180_f64) == rvalue) &&
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue)
{
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, ".."));
let mut proposal = format!("{}.to_radians()", Sugg::hir(cx, mul_lhs, "..").maybe_par());
if_chain! {
if let ExprKind::Lit(ref literal) = mul_lhs.kind;
if let ast::LitKind::Float(ref value, float_type) = literal.node;
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/floating_point_rad.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ pub const fn const_context() {
let _ = x * 180f32 / std::f32::consts::PI;
}

pub fn issue9391(degrees: i64) {
let _ = (degrees as f64).to_radians();
let _ = (degrees as f64).to_degrees();
}

fn main() {
let x = 3f32;
let _ = x.to_degrees();
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/floating_point_rad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ pub const fn const_context() {
let _ = x * 180f32 / std::f32::consts::PI;
}

pub fn issue9391(degrees: i64) {
let _ = degrees as f64 * std::f64::consts::PI / 180.0;
let _ = degrees as f64 * 180.0 / std::f64::consts::PI;
}

fn main() {
let x = 3f32;
let _ = x * 180f32 / std::f32::consts::PI;
Expand Down
28 changes: 20 additions & 8 deletions tests/ui/floating_point_rad.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
error: conversion to radians can be done more accurately
--> $DIR/floating_point_rad.rs:12:13
|
LL | let _ = degrees as f64 * std::f64::consts::PI / 180.0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_radians()`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: conversion to degrees can be done more accurately
--> $DIR/floating_point_rad.rs:13:13
|
LL | let _ = degrees as f64 * 180.0 / std::f64::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_degrees()`

error: conversion to degrees can be done more accurately
--> $DIR/floating_point_rad.rs:18:13
|
LL | let _ = x * 180f32 / std::f32::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

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

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

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

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

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

error: aborting due to 6 previous errors
error: aborting due to 8 previous errors

0 comments on commit 26a6891

Please sign in to comment.