Auto merge of #9394 - lukaslueg:issue9391, r=Jarcho

Fix missing parens in `suboptimal_flops` suggestion

Fixes #9391. The problem is simple enough, I didn't check if the same problem occurs elsewhere, though.

changelog: fix missing parenthesis in `suboptimal_flops` suggestion
This commit is contained in:
bors 2022-08-30 00:37:45 +00:00
commit 4df6032100
19 changed files with 149 additions and 71 deletions

View file

@ -172,7 +172,7 @@ fn check_log_base(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
expr.span,
"logarithm for bases 2, 10 and e can be computed more accurately",
"consider using",
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method),
format!("{}.{}()", Sugg::hir(cx, &args[0], "..").maybe_par(), method),
Applicability::MachineApplicable,
);
}
@ -263,13 +263,13 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
(
SUBOPTIMAL_FLOPS,
"square-root of a number can be computed more efficiently and accurately",
format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..")),
format!("{}.sqrt()", Sugg::hir(cx, &args[0], "..").maybe_par()),
)
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
(
IMPRECISE_FLOPS,
"cube-root of a number can be computed more accurately",
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..")),
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..").maybe_par()),
)
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
(
@ -277,7 +277,7 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
"exponentiation with integer powers can be computed more efficiently",
format!(
"{}.powi({})",
Sugg::hir(cx, &args[0], ".."),
Sugg::hir(cx, &args[0], "..").maybe_par(),
numeric_literal::format(&exponent.to_string(), None, false)
),
)
@ -327,7 +327,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
"consider using",
format!(
"{}.mul_add({}, {})",
Sugg::hir(cx, &args[0], ".."),
Sugg::hir(cx, &args[0], "..").maybe_par(),
Sugg::hir(cx, &args[0], ".."),
Sugg::hir(cx, other_addend, ".."),
),
@ -418,7 +418,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
"consider using",
format!(
"{}.exp_m1()",
Sugg::hir(cx, self_arg, "..")
Sugg::hir(cx, self_arg, "..").maybe_par()
),
Applicability::MachineApplicable,
);
@ -550,11 +550,11 @@ fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
then {
let positive_abs_sugg = (
"manual implementation of `abs` method",
format!("{}.abs()", Sugg::hir(cx, body, "..")),
format!("{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
);
let negative_abs_sugg = (
"manual implementation of negation of `abs` method",
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
format!("-{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
);
let sugg = if is_testing_positive(cx, cond, body) {
if if_expr_positive {
@ -621,7 +621,7 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
expr.span,
"log base can be expressed more clearly",
"consider using",
format!("{}.log({})", Sugg::hir(cx, largs_self, ".."), Sugg::hir(cx, rargs_self, ".."),),
format!("{}.log({})", Sugg::hir(cx, largs_self, "..").maybe_par(), Sugg::hir(cx, rargs_self, ".."),),
Applicability::MachineApplicable,
);
}
@ -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;
@ -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;

View file

@ -5,6 +5,7 @@ fn main() {
let x = 2f32;
let _ = x.exp_m1();
let _ = x.exp_m1() + 2.0;
let _ = (x as f32).exp_m1() + 2.0;
// Cases where the lint shouldn't be applied
let _ = x.exp() - 2.0;
let _ = x.exp() - 1.0 * 2.0;

View file

@ -5,6 +5,7 @@ fn main() {
let x = 2f32;
let _ = x.exp() - 1.0;
let _ = x.exp() - 1.0 + 2.0;
let _ = (x as f32).exp() - 1.0 + 2.0;
// Cases where the lint shouldn't be applied
let _ = x.exp() - 2.0;
let _ = x.exp() - 1.0 * 2.0;

View file

@ -13,16 +13,22 @@ LL | let _ = x.exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:13:13
--> $DIR/floating_point_exp.rs:8:13
|
LL | let _ = (x as f32).exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).exp_m1()`
error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:14:13
|
LL | let _ = x.exp() - 1.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:14:13
--> $DIR/floating_point_exp.rs:15:13
|
LL | let _ = x.exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

View file

@ -12,6 +12,7 @@ fn check_log_base() {
let _ = x.ln();
let _ = x.log2();
let _ = x.ln();
let _ = (x as f32).log2();
let x = 1f64;
let _ = x.log2();

View file

@ -12,6 +12,7 @@ fn check_log_base() {
let _ = x.log(std::f32::consts::E);
let _ = x.log(TWO);
let _ = x.log(E);
let _ = (x as f32).log(2f32);
let x = 1f64;
let _ = x.log(2f64);

View file

@ -31,25 +31,31 @@ LL | let _ = x.log(E);
| ^^^^^^^^ help: consider using: `x.ln()`
error: logarithm for bases 2, 10 and e can be computed more accurately
--> $DIR/floating_point_log.rs:17:13
--> $DIR/floating_point_log.rs:15:13
|
LL | let _ = (x as f32).log(2f32);
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).log2()`
error: logarithm for bases 2, 10 and e can be computed more accurately
--> $DIR/floating_point_log.rs:18:13
|
LL | let _ = x.log(2f64);
| ^^^^^^^^^^^ help: consider using: `x.log2()`
error: logarithm for bases 2, 10 and e can be computed more accurately
--> $DIR/floating_point_log.rs:18:13
--> $DIR/floating_point_log.rs:19:13
|
LL | let _ = x.log(10f64);
| ^^^^^^^^^^^^ help: consider using: `x.log10()`
error: logarithm for bases 2, 10 and e can be computed more accurately
--> $DIR/floating_point_log.rs:19:13
--> $DIR/floating_point_log.rs:20:13
|
LL | let _ = x.log(std::f64::consts::E);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:24:13
--> $DIR/floating_point_log.rs:25:13
|
LL | let _ = (1f32 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
@ -57,118 +63,118 @@ LL | let _ = (1f32 + 2.).ln();
= note: `-D clippy::imprecise-flops` implied by `-D warnings`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:25:13
--> $DIR/floating_point_log.rs:26:13
|
LL | let _ = (1f32 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:26:13
--> $DIR/floating_point_log.rs:27:13
|
LL | let _ = (1.0 + x).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:27:13
--> $DIR/floating_point_log.rs:28:13
|
LL | let _ = (1.0 + x / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:28:13
--> $DIR/floating_point_log.rs:29:13
|
LL | let _ = (1.0 + x.powi(3)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:29:13
--> $DIR/floating_point_log.rs:30:13
|
LL | let _ = (1.0 + x.powi(3) / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(3) / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:30:13
--> $DIR/floating_point_log.rs:31:13
|
LL | let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(std::f32::consts::E - 1.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:31:13
--> $DIR/floating_point_log.rs:32:13
|
LL | let _ = (x + 1.0).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:32:13
--> $DIR/floating_point_log.rs:33:13
|
LL | let _ = (x.powi(3) + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:33:13
--> $DIR/floating_point_log.rs:34:13
|
LL | let _ = (x + 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:34:13
--> $DIR/floating_point_log.rs:35:13
|
LL | let _ = (x / 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:42:13
--> $DIR/floating_point_log.rs:43:13
|
LL | let _ = (1f64 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:43:13
--> $DIR/floating_point_log.rs:44:13
|
LL | let _ = (1f64 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:44:13
--> $DIR/floating_point_log.rs:45:13
|
LL | let _ = (1.0 + x).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:45:13
--> $DIR/floating_point_log.rs:46:13
|
LL | let _ = (1.0 + x / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:46:13
--> $DIR/floating_point_log.rs:47:13
|
LL | let _ = (1.0 + x.powi(3)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:47:13
--> $DIR/floating_point_log.rs:48:13
|
LL | let _ = (x + 1.0).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:48:13
--> $DIR/floating_point_log.rs:49:13
|
LL | let _ = (x.powi(3) + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:49:13
--> $DIR/floating_point_log.rs:50:13
|
LL | let _ = (x + 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:50:13
--> $DIR/floating_point_log.rs:51:13
|
LL | let _ = (x / 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: aborting due to 28 previous errors
error: aborting due to 29 previous errors

View file

@ -5,6 +5,7 @@ fn main() {
let x = 3f32;
let y = 5f32;
let _ = x.log(y);
let _ = (x as f32).log(y);
let _ = x.log(y);
let _ = x.log(y);
let _ = x.log(y);

View file

@ -5,6 +5,7 @@ fn main() {
let x = 3f32;
let y = 5f32;
let _ = x.ln() / y.ln();
let _ = (x as f32).ln() / y.ln();
let _ = x.log2() / y.log2();
let _ = x.log10() / y.log10();
let _ = x.log(5f32) / y.log(5f32);

View file

@ -9,20 +9,26 @@ LL | let _ = x.ln() / y.ln();
error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:8:13
|
LL | let _ = (x as f32).ln() / y.ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).log(y)`
error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:9:13
|
LL | let _ = x.log2() / y.log2();
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:9:13
--> $DIR/floating_point_logbase.rs:10:13
|
LL | let _ = x.log10() / y.log10();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:10:13
--> $DIR/floating_point_logbase.rs:11:13
|
LL | let _ = x.log(5f32) / y.log(5f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

View file

@ -11,10 +11,13 @@ fn main() {
let _ = (-3.1f32).exp();
let _ = x.sqrt();
let _ = x.cbrt();
let _ = (x as f32).cbrt();
let _ = x.powi(3);
let _ = x.powi(-2);
let _ = x.powi(16_777_215);
let _ = x.powi(-16_777_215);
let _ = (x as f32).powi(-16_777_215);
let _ = (x as f32).powi(3);
// Cases where the lint shouldn't be applied
let _ = x.powf(2.1);
let _ = x.powf(-2.1);

View file

@ -11,10 +11,13 @@ fn main() {
let _ = std::f32::consts::E.powf(-3.1);
let _ = x.powf(1.0 / 2.0);
let _ = x.powf(1.0 / 3.0);
let _ = (x as f32).powf(1.0 / 3.0);
let _ = x.powf(3.0);
let _ = x.powf(-2.0);
let _ = x.powf(16_777_215.0);
let _ = x.powf(-16_777_215.0);
let _ = (x as f32).powf(-16_777_215.0);
let _ = (x as f32).powf(3.0);
// Cases where the lint shouldn't be applied
let _ = x.powf(2.1);
let _ = x.powf(-2.1);

View file

@ -50,101 +50,119 @@ LL | let _ = x.powf(1.0 / 3.0);
|
= note: `-D clippy::imprecise-flops` implied by `-D warnings`
error: exponentiation with integer powers can be computed more efficiently
error: cube-root of a number can be computed more accurately
--> $DIR/floating_point_powf.rs:14:13
|
LL | let _ = (x as f32).powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).cbrt()`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:15:13
|
LL | let _ = x.powf(3.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(3)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:15:13
--> $DIR/floating_point_powf.rs:16:13
|
LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:16:13
--> $DIR/floating_point_powf.rs:17:13
|
LL | let _ = x.powf(16_777_215.0);
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(16_777_215)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:17:13
--> $DIR/floating_point_powf.rs:18:13
|
LL | let _ = x.powf(-16_777_215.0);
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-16_777_215)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:19:13
|
LL | let _ = (x as f32).powf(-16_777_215.0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).powi(-16_777_215)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:20:13
|
LL | let _ = (x as f32).powf(3.0);
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).powi(3)`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:25:13
--> $DIR/floating_point_powf.rs:28:13
|
LL | let _ = 2f64.powf(x);
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:26:13
--> $DIR/floating_point_powf.rs:29:13
|
LL | let _ = 2f64.powf(3.1);
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:27:13
--> $DIR/floating_point_powf.rs:30:13
|
LL | let _ = 2f64.powf(-3.1);
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:28:13
--> $DIR/floating_point_powf.rs:31:13
|
LL | let _ = std::f64::consts::E.powf(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:29:13
--> $DIR/floating_point_powf.rs:32:13
|
LL | let _ = std::f64::consts::E.powf(3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:30:13
--> $DIR/floating_point_powf.rs:33:13
|
LL | let _ = std::f64::consts::E.powf(-3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
error: square-root of a number can be computed more efficiently and accurately
--> $DIR/floating_point_powf.rs:31:13
--> $DIR/floating_point_powf.rs:34:13
|
LL | let _ = x.powf(1.0 / 2.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
error: cube-root of a number can be computed more accurately
--> $DIR/floating_point_powf.rs:32:13
--> $DIR/floating_point_powf.rs:35:13
|
LL | let _ = x.powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:33:13
--> $DIR/floating_point_powf.rs:36:13
|
LL | let _ = x.powf(3.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(3)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:34:13
--> $DIR/floating_point_powf.rs:37:13
|
LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:35:13
--> $DIR/floating_point_powf.rs:38:13
|
LL | let _ = x.powf(-2_147_483_648.0);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-2_147_483_648)`
error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:36:13
--> $DIR/floating_point_powf.rs:39:13
|
LL | let _ = x.powf(2_147_483_647.0);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2_147_483_647)`
error: aborting due to 24 previous errors
error: aborting due to 27 previous errors

View file

@ -8,6 +8,7 @@ fn main() {
let y = 4f32;
let _ = x.mul_add(x, y);
let _ = y.mul_add(y, x);
let _ = (y as f32).mul_add(y as f32, x);
let _ = x.mul_add(x, y).sqrt();
let _ = y.mul_add(y, x).sqrt();
// Cases where the lint shouldn't be applied

View file

@ -8,6 +8,7 @@ fn main() {
let y = 4f32;
let _ = x.powi(2) + y;
let _ = x + y.powi(2);
let _ = x + (y as f32).powi(2);
let _ = (x.powi(2) + y).sqrt();
let _ = (x + y.powi(2)).sqrt();
// Cases where the lint shouldn't be applied

View file

@ -15,14 +15,20 @@ LL | let _ = x + y.powi(2);
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:11:13
|
LL | let _ = (x.powi(2) + y).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`
LL | let _ = x + (y as f32).powi(2);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y as f32).mul_add(y as f32, x)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:12:13
|
LL | let _ = (x.powi(2) + y).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:13:13
|
LL | let _ = (x + y.powi(2)).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`
error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

View file

@ -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();

View file

@ -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;

View file

@ -1,40 +1,52 @@
error: conversion to degrees can be done more accurately
--> $DIR/floating_point_rad.rs:13:13
error: conversion to radians can be done more accurately
--> $DIR/floating_point_rad.rs:12:13
|
LL | let _ = x * 180f32 / std::f32::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
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:14:13
--> $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()`
error: conversion to degrees can be done more accurately
--> $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