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, expr.span,
"logarithm for bases 2, 10 and e can be computed more accurately", "logarithm for bases 2, 10 and e can be computed more accurately",
"consider using", "consider using",
format!("{}.{}()", Sugg::hir(cx, &args[0], ".."), method), format!("{}.{}()", Sugg::hir(cx, &args[0], "..").maybe_par(), method),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
@ -263,13 +263,13 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
( (
SUBOPTIMAL_FLOPS, SUBOPTIMAL_FLOPS,
"square-root of a number can be computed more efficiently and accurately", "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 { } else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
( (
IMPRECISE_FLOPS, IMPRECISE_FLOPS,
"cube-root of a number can be computed more accurately", "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) { } 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", "exponentiation with integer powers can be computed more efficiently",
format!( format!(
"{}.powi({})", "{}.powi({})",
Sugg::hir(cx, &args[0], ".."), Sugg::hir(cx, &args[0], "..").maybe_par(),
numeric_literal::format(&exponent.to_string(), None, false) numeric_literal::format(&exponent.to_string(), None, false)
), ),
) )
@ -327,7 +327,7 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
"consider using", "consider using",
format!( format!(
"{}.mul_add({}, {})", "{}.mul_add({}, {})",
Sugg::hir(cx, &args[0], ".."), Sugg::hir(cx, &args[0], "..").maybe_par(),
Sugg::hir(cx, &args[0], ".."), Sugg::hir(cx, &args[0], ".."),
Sugg::hir(cx, other_addend, ".."), Sugg::hir(cx, other_addend, ".."),
), ),
@ -418,7 +418,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
"consider using", "consider using",
format!( format!(
"{}.exp_m1()", "{}.exp_m1()",
Sugg::hir(cx, self_arg, "..") Sugg::hir(cx, self_arg, "..").maybe_par()
), ),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
@ -550,11 +550,11 @@ fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
then { then {
let positive_abs_sugg = ( let positive_abs_sugg = (
"manual implementation of `abs` method", "manual implementation of `abs` method",
format!("{}.abs()", Sugg::hir(cx, body, "..")), format!("{}.abs()", Sugg::hir(cx, body, "..").maybe_par()),
); );
let negative_abs_sugg = ( let negative_abs_sugg = (
"manual implementation of negation of `abs` method", "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) { let sugg = if is_testing_positive(cx, cond, body) {
if if_expr_positive { if if_expr_positive {
@ -621,7 +621,7 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
expr.span, expr.span,
"log base can be expressed more clearly", "log base can be expressed more clearly",
"consider using", "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, Applicability::MachineApplicable,
); );
} }
@ -651,7 +651,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) && if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&
(F32(180_f32) == lvalue || F64(180_f64) == lvalue) (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_chain! {
if let ExprKind::Lit(ref literal) = mul_lhs.kind; if let ExprKind::Lit(ref literal) = mul_lhs.kind;
if let ast::LitKind::Float(ref value, float_type) = literal.node; 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(180_f32) == rvalue || F64(180_f64) == rvalue) &&
(F32(f32_consts::PI) == lvalue || F64(f64_consts::PI) == lvalue) (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_chain! {
if let ExprKind::Lit(ref literal) = mul_lhs.kind; if let ExprKind::Lit(ref literal) = mul_lhs.kind;
if let ast::LitKind::Float(ref value, float_type) = literal.node; if let ast::LitKind::Float(ref value, float_type) = literal.node;

View File

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

View File

@ -5,6 +5,7 @@ fn main() {
let x = 2f32; let x = 2f32;
let _ = x.exp() - 1.0; let _ = x.exp() - 1.0;
let _ = x.exp() - 1.0 + 2.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 // Cases where the lint shouldn't be applied
let _ = x.exp() - 2.0; let _ = x.exp() - 2.0;
let _ = x.exp() - 1.0 * 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()` | ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
error: (e.pow(x) - 1) can be computed more accurately 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; LL | let _ = x.exp() - 1.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()` | ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
error: (e.pow(x) - 1) can be computed more accurately 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; LL | let _ = x.exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()` | ^^^^^^^^^^^^^ 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.ln();
let _ = x.log2(); let _ = x.log2();
let _ = x.ln(); let _ = x.ln();
let _ = (x as f32).log2();
let x = 1f64; let x = 1f64;
let _ = x.log2(); let _ = x.log2();

View File

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

View File

@ -31,25 +31,31 @@ LL | let _ = x.log(E);
| ^^^^^^^^ help: consider using: `x.ln()` | ^^^^^^^^ help: consider using: `x.ln()`
error: logarithm for bases 2, 10 and e can be computed more accurately 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); LL | let _ = x.log(2f64);
| ^^^^^^^^^^^ help: consider using: `x.log2()` | ^^^^^^^^^^^ help: consider using: `x.log2()`
error: logarithm for bases 2, 10 and e can be computed more accurately 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); LL | let _ = x.log(10f64);
| ^^^^^^^^^^^^ help: consider using: `x.log10()` | ^^^^^^^^^^^^ help: consider using: `x.log10()`
error: logarithm for bases 2, 10 and e can be computed more accurately 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); LL | let _ = x.log(std::f64::consts::E);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.ln()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1f32 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()` | ^^^^^^^^^^^^^^^^ 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` = note: `-D clippy::imprecise-flops` implied by `-D warnings`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1f32 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()` | ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f32.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()` | ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x.powi(3)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x.powi(3) / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(3) / 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(3) / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(std::f32::consts::E - 1.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(std::f32::consts::E - 1.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x + 1.0).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()` | ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x.powi(3) + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x + 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x / 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1f64 + 2.).ln();
| ^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()` | ^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1f64 + 2.0).ln();
| ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()` | ^^^^^^^^^^^^^^^^^ help: consider using: `2.0f64.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()` | ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (1.0 + x.powi(3)).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x + 1.0).ln();
| ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()` | ^^^^^^^^^^^^^^ help: consider using: `x.ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x.powi(3) + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(3).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x + 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately 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(); LL | let _ = (x / 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()` | ^^^^^^^^^^^^^^^^^^^^ 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 x = 3f32;
let y = 5f32; let y = 5f32;
let _ = x.log(y); let _ = x.log(y);
let _ = (x as f32).log(y);
let _ = x.log(y); let _ = x.log(y);
let _ = x.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 x = 3f32;
let y = 5f32; let y = 5f32;
let _ = x.ln() / y.ln(); let _ = x.ln() / y.ln();
let _ = (x as f32).ln() / y.ln();
let _ = x.log2() / y.log2(); let _ = x.log2() / y.log2();
let _ = x.log10() / y.log10(); let _ = x.log10() / y.log10();
let _ = x.log(5f32) / y.log(5f32); 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 error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:8:13 --> $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(); LL | let _ = x.log2() / y.log2();
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: log base can be expressed more clearly 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(); LL | let _ = x.log10() / y.log10();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
error: log base can be expressed more clearly 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); LL | let _ = x.log(5f32) / y.log(5f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)` | ^^^^^^^^^^^^^^^^^^^^^^^^^ 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 _ = (-3.1f32).exp();
let _ = x.sqrt(); let _ = x.sqrt();
let _ = x.cbrt(); let _ = x.cbrt();
let _ = (x as f32).cbrt();
let _ = x.powi(3); let _ = x.powi(3);
let _ = x.powi(-2); let _ = x.powi(-2);
let _ = x.powi(16_777_215); let _ = x.powi(16_777_215);
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 // Cases where the lint shouldn't be applied
let _ = x.powf(2.1); let _ = x.powf(2.1);
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 _ = std::f32::consts::E.powf(-3.1);
let _ = x.powf(1.0 / 2.0); let _ = x.powf(1.0 / 2.0);
let _ = x.powf(1.0 / 3.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(3.0);
let _ = x.powf(-2.0); let _ = x.powf(-2.0);
let _ = x.powf(16_777_215.0); let _ = x.powf(16_777_215.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 // Cases where the lint shouldn't be applied
let _ = x.powf(2.1); let _ = x.powf(2.1);
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` = 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 --> $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); LL | let _ = x.powf(3.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(3)` | ^^^^^^^^^^^ help: consider using: `x.powi(3)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)` | ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(16_777_215.0);
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(16_777_215)` | ^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(16_777_215)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(-16_777_215.0);
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-16_777_215)` | ^^^^^^^^^^^^^^^^^^^^^ 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 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); LL | let _ = 2f64.powf(x);
| ^^^^^^^^^^^^ help: consider using: `x.exp2()` | ^^^^^^^^^^^^ help: consider using: `x.exp2()`
error: exponent for bases 2 and e can be computed more accurately 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); LL | let _ = 2f64.powf(3.1);
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()` | ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
error: exponent for bases 2 and e can be computed more accurately 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); LL | let _ = 2f64.powf(-3.1);
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()` | ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
error: exponent for bases 2 and e can be computed more accurately 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); LL | let _ = std::f64::consts::E.powf(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
error: exponent for bases 2 and e can be computed more accurately 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); LL | let _ = std::f64::consts::E.powf(3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
error: exponent for bases 2 and e can be computed more accurately 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); LL | let _ = std::f64::consts::E.powf(-3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
error: square-root of a number can be computed more efficiently and accurately 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); LL | let _ = x.powf(1.0 / 2.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()` | ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
error: cube-root of a number can be computed more accurately 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); LL | let _ = x.powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()` | ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(3.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(3)` | ^^^^^^^^^^^ help: consider using: `x.powi(3)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)` | ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(-2_147_483_648.0);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-2_147_483_648)` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-2_147_483_648)`
error: exponentiation with integer powers can be computed more efficiently 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); LL | let _ = x.powf(2_147_483_647.0);
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2_147_483_647)` | ^^^^^^^^^^^^^^^^^^^^^^^ 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 y = 4f32;
let _ = x.mul_add(x, y); let _ = x.mul_add(x, y);
let _ = y.mul_add(y, x); let _ = y.mul_add(y, x);
let _ = (y as f32).mul_add(y as f32, x);
let _ = x.mul_add(x, y).sqrt(); let _ = x.mul_add(x, y).sqrt();
let _ = y.mul_add(y, x).sqrt(); let _ = y.mul_add(y, x).sqrt();
// Cases where the lint shouldn't be applied // Cases where the lint shouldn't be applied

View File

@ -8,6 +8,7 @@ fn main() {
let y = 4f32; let y = 4f32;
let _ = x.powi(2) + y; let _ = x.powi(2) + y;
let _ = x + y.powi(2); let _ = x + y.powi(2);
let _ = x + (y as f32).powi(2);
let _ = (x.powi(2) + y).sqrt(); let _ = (x.powi(2) + y).sqrt();
let _ = (x + y.powi(2)).sqrt(); let _ = (x + y.powi(2)).sqrt();
// Cases where the lint shouldn't be applied // 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 error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:11:13 --> $DIR/floating_point_powi.rs:11:13
| |
LL | let _ = (x.powi(2) + y).sqrt(); LL | let _ = x + (y as f32).powi(2);
| ^^^^^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(y as f32).mul_add(y as f32, x)`
error: multiply and add expressions can be calculated more efficiently and accurately error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_powi.rs:12:13 --> $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(); LL | let _ = (x + y.powi(2)).sqrt();
| ^^^^^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)` | ^^^^^^^^^^^^^^^ 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; 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() { fn main() {
let x = 3f32; let x = 3f32;
let _ = x.to_degrees(); let _ = x.to_degrees();

View File

@ -8,6 +8,11 @@ pub const fn const_context() {
let _ = x * 180f32 / std::f32::consts::PI; 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() { fn main() {
let x = 3f32; let x = 3f32;
let _ = x * 180f32 / std::f32::consts::PI; let _ = x * 180f32 / std::f32::consts::PI;

View File

@ -1,40 +1,52 @@
error: conversion to degrees can be done more accurately error: conversion to radians can be done more accurately
--> $DIR/floating_point_rad.rs:13:13 --> $DIR/floating_point_rad.rs:12:13
| |
LL | let _ = x * 180f32 / std::f32::consts::PI; LL | let _ = degrees as f64 * std::f64::consts::PI / 180.0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_radians()`
| |
= note: `-D clippy::suboptimal-flops` implied by `-D warnings` = note: `-D clippy::suboptimal-flops` implied by `-D warnings`
error: conversion to degrees can be done more accurately 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; LL | let _ = 90. * 180f64 / std::f64::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_degrees()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_degrees()`
error: conversion to degrees can be done more accurately 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; LL | let _ = 90.5 * 180f64 / std::f64::consts::PI;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_degrees()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_degrees()`
error: conversion to radians can be done more accurately 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; LL | let _ = x * std::f32::consts::PI / 180f32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()`
error: conversion to radians can be done more accurately 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; LL | let _ = 90. * std::f32::consts::PI / 180f32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_radians()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_radians()`
error: conversion to radians can be done more accurately 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; LL | let _ = 90.5 * std::f32::consts::PI / 180f32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_radians()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_radians()`
error: aborting due to 6 previous errors error: aborting due to 8 previous errors