Skip to content

Fix missing parens in suboptimal_flops suggestion #9394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down Expand Up @@ -263,21 +263,21 @@ 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) {
(
SUBOPTIMAL_FLOPS,
"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)
),
)
Expand Down Expand Up @@ -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, ".."),
),
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
);
}
Expand Down 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
1 change: 1 addition & 0 deletions tests/ui/floating_point_exp.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/floating_point_exp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions tests/ui/floating_point_log.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 30 additions & 24 deletions tests/ui/floating_point_log.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,144 +31,150 @@ 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()`
|
= 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

1 change: 1 addition & 0 deletions tests/ui/floating_point_logbase.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_logbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/floating_point_logbase.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

3 changes: 3 additions & 0 deletions tests/ui/floating_point_powf.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/floating_point_powf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading