Skip to content

Commit 5fbfdfa

Browse files
committed
Auto merge of rust-lang#8009 - xFrednet:8004-suboptimal-flops-in-const, r=giraffate
Allow `suboptimal_flops` in const functions This PR allows `clippy::suboptimal_flops` in constant functions. The check also effects the `clippy::imprecise_flops` lint logic. However, this doesn't have any effects as all functions checked for are not const and can therefore not be found in such functions. --- changelog: [`suboptimal_flops`]: No longer triggers in constant functions Closes: rust-lang/rust-clippy#8004
2 parents de2208a + 1c8085d commit 5fbfdfa

10 files changed

+74
-21
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::consts::{
44
};
55
use clippy_utils::diagnostics::span_lint_and_sugg;
66
use clippy_utils::higher;
7-
use clippy_utils::{eq_expr_value, get_parent_expr, numeric_literal, sugg};
7+
use clippy_utils::{eq_expr_value, get_parent_expr, in_constant, numeric_literal, sugg};
88
use if_chain::if_chain;
99
use rustc_errors::Applicability;
1010
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -687,6 +687,11 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
687687

688688
impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
689689
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
690+
// All of these operations are currently not const.
691+
if in_constant(cx, expr.hir_id) {
692+
return;
693+
}
694+
690695
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind {
691696
let recv_ty = cx.typeck_results().expr_ty(&args[0]);
692697

tests/ui/floating_point_abs.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal ops in constant context
6+
pub const fn in_const_context(num: f64) -> f64 {
7+
if num >= 0.0 { num } else { -num }
8+
}
9+
410
struct A {
511
a: f64,
612
b: f64,

tests/ui/floating_point_abs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal ops in constant context
6+
pub const fn in_const_context(num: f64) -> f64 {
7+
if num >= 0.0 { num } else { -num }
8+
}
9+
410
struct A {
511
a: f64,
612
b: f64,

tests/ui/floating_point_abs.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: manual implementation of `abs` method
2-
--> $DIR/floating_point_abs.rs:10:5
2+
--> $DIR/floating_point_abs.rs:16:5
33
|
44
LL | if num >= 0.0 { num } else { -num }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

99
error: manual implementation of `abs` method
10-
--> $DIR/floating_point_abs.rs:14:5
10+
--> $DIR/floating_point_abs.rs:20:5
1111
|
1212
LL | if 0.0 < num { num } else { -num }
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
1414

1515
error: manual implementation of `abs` method
16-
--> $DIR/floating_point_abs.rs:18:5
16+
--> $DIR/floating_point_abs.rs:24:5
1717
|
1818
LL | if a.a > 0.0 { a.a } else { -a.a }
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.a.abs()`
2020

2121
error: manual implementation of `abs` method
22-
--> $DIR/floating_point_abs.rs:22:5
22+
--> $DIR/floating_point_abs.rs:28:5
2323
|
2424
LL | if 0.0 >= num { -num } else { num }
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
2626

2727
error: manual implementation of `abs` method
28-
--> $DIR/floating_point_abs.rs:26:5
28+
--> $DIR/floating_point_abs.rs:32:5
2929
|
3030
LL | if a.a < 0.0 { -a.a } else { a.a }
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.a.abs()`
3232

3333
error: manual implementation of negation of `abs` method
34-
--> $DIR/floating_point_abs.rs:30:5
34+
--> $DIR/floating_point_abs.rs:36:5
3535
|
3636
LL | if num < 0.0 { num } else { -num }
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-num.abs()`
3838

3939
error: manual implementation of negation of `abs` method
40-
--> $DIR/floating_point_abs.rs:34:5
40+
--> $DIR/floating_point_abs.rs:40:5
4141
|
4242
LL | if 0.0 >= num { num } else { -num }
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-num.abs()`
4444

4545
error: manual implementation of negation of `abs` method
46-
--> $DIR/floating_point_abs.rs:39:12
46+
--> $DIR/floating_point_abs.rs:45:12
4747
|
4848
LL | a: if a.a >= 0.0 { -a.a } else { a.a },
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-a.a.abs()`

tests/ui/floating_point_mul_add.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal_ops in constant context
6+
pub const fn in_const_context() {
7+
let a: f64 = 1234.567;
8+
let b: f64 = 45.67834;
9+
let c: f64 = 0.0004;
10+
11+
let _ = a * b + c;
12+
let _ = c + a * b;
13+
}
14+
415
fn main() {
516
let a: f64 = 1234.567;
617
let b: f64 = 45.67834;

tests/ui/floating_point_mul_add.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal_ops in constant context
6+
pub const fn in_const_context() {
7+
let a: f64 = 1234.567;
8+
let b: f64 = 45.67834;
9+
let c: f64 = 0.0004;
10+
11+
let _ = a * b + c;
12+
let _ = c + a * b;
13+
}
14+
415
fn main() {
516
let a: f64 = 1234.567;
617
let b: f64 = 45.67834;

tests/ui/floating_point_mul_add.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
error: multiply and add expressions can be calculated more efficiently and accurately
2-
--> $DIR/floating_point_mul_add.rs:10:13
2+
--> $DIR/floating_point_mul_add.rs:21:13
33
|
44
LL | let _ = a * b + c;
55
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

99
error: multiply and add expressions can be calculated more efficiently and accurately
10-
--> $DIR/floating_point_mul_add.rs:11:13
10+
--> $DIR/floating_point_mul_add.rs:22:13
1111
|
1212
LL | let _ = c + a * b;
1313
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
1414

1515
error: multiply and add expressions can be calculated more efficiently and accurately
16-
--> $DIR/floating_point_mul_add.rs:12:13
16+
--> $DIR/floating_point_mul_add.rs:23:13
1717
|
1818
LL | let _ = a + 2.0 * 4.0;
1919
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4.0, a)`
2020

2121
error: multiply and add expressions can be calculated more efficiently and accurately
22-
--> $DIR/floating_point_mul_add.rs:13:13
22+
--> $DIR/floating_point_mul_add.rs:24:13
2323
|
2424
LL | let _ = a + 2. * 4.;
2525
| ^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4., a)`
2626

2727
error: multiply and add expressions can be calculated more efficiently and accurately
28-
--> $DIR/floating_point_mul_add.rs:15:13
28+
--> $DIR/floating_point_mul_add.rs:26:13
2929
|
3030
LL | let _ = (a * b) + c;
3131
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
3232

3333
error: multiply and add expressions can be calculated more efficiently and accurately
34-
--> $DIR/floating_point_mul_add.rs:16:13
34+
--> $DIR/floating_point_mul_add.rs:27:13
3535
|
3636
LL | let _ = c + (a * b);
3737
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
3838

3939
error: multiply and add expressions can be calculated more efficiently and accurately
40-
--> $DIR/floating_point_mul_add.rs:17:13
40+
--> $DIR/floating_point_mul_add.rs:28:13
4141
|
4242
LL | let _ = a * b * c + d;
4343
| ^^^^^^^^^^^^^ help: consider using: `(a * b).mul_add(c, d)`
4444

4545
error: multiply and add expressions can be calculated more efficiently and accurately
46-
--> $DIR/floating_point_mul_add.rs:19:13
46+
--> $DIR/floating_point_mul_add.rs:30:13
4747
|
4848
LL | let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c))`
5050

5151
error: multiply and add expressions can be calculated more efficiently and accurately
52-
--> $DIR/floating_point_mul_add.rs:20:13
52+
--> $DIR/floating_point_mul_add.rs:31:13
5353
|
5454
LL | let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`
5656

5757
error: multiply and add expressions can be calculated more efficiently and accurately
58-
--> $DIR/floating_point_mul_add.rs:22:13
58+
--> $DIR/floating_point_mul_add.rs:33:13
5959
|
6060
LL | let _ = (a * a + b).sqrt();
6161
| ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`

tests/ui/floating_point_rad.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal_flops in constant context
6+
pub const fn const_context() {
7+
let x = 3f32;
8+
let _ = x * 180f32 / std::f32::consts::PI;
9+
}
10+
411
fn main() {
512
let x = 3f32;
613
let _ = x.to_degrees();

tests/ui/floating_point_rad.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// run-rustfix
2+
#![feature(const_fn_floating_point_arithmetic)]
23
#![warn(clippy::suboptimal_flops)]
34

5+
/// Allow suboptimal_flops in constant context
6+
pub const fn const_context() {
7+
let x = 3f32;
8+
let _ = x * 180f32 / std::f32::consts::PI;
9+
}
10+
411
fn main() {
512
let x = 3f32;
613
let _ = x * 180f32 / std::f32::consts::PI;

tests/ui/floating_point_rad.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: conversion to degrees can be done more accurately
2-
--> $DIR/floating_point_rad.rs:6:13
2+
--> $DIR/floating_point_rad.rs:13:13
33
|
44
LL | let _ = x * 180f32 / std::f32::consts::PI;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

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

0 commit comments

Comments
 (0)