diff options
| author | bors <bors@rust-lang.org> | 2023-11-27 20:37:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-27 20:37:34 +0000 |
| commit | e6f33905e81e232abc7682e79d2e54b4fde7444a (patch) | |
| tree | 91c1a520c99110e92838cc357159fb435c02079a | |
| parent | 003e910760ef75196ca26e29c4fc83c2f418f693 (diff) | |
| parent | a2e396badfb1e36ba885480da4f7994fec1e2deb (diff) | |
| download | rust-e6f33905e81e232abc7682e79d2e54b4fde7444a.tar.gz rust-e6f33905e81e232abc7682e79d2e54b4fde7444a.zip | |
Auto merge of #11836 - lukaslueg:issue11831, r=Alexendoo
Don't suggest `a.mul_add(b, c)` if parameters are not float clippy::suboptimal_flops used to not check if the second parameter to f32/f64.mul_add() was float. Since the method is only defined to take `Self` as parameters, the suggestion was wrong. Fixes #11831 changelog: [`suboptimal_float`]: Don't suggest `a.mul_add(b, c)` if parameters are not f32/f64
| -rw-r--r-- | clippy_lints/src/floating_point_arithmetic.rs | 8 | ||||
| -rw-r--r-- | tests/ui/floating_point_mul_add.fixed | 18 | ||||
| -rw-r--r-- | tests/ui/floating_point_mul_add.rs | 18 |
3 files changed, 42 insertions, 2 deletions
diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index aaf1fee3f00..c8b87e510ed 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -496,9 +496,13 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) { if let BinOpKind::Sub = op { -sugg } else { sugg } }; - let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) { + let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) + && cx.typeck_results().expr_ty(rhs).is_floating_point() + { (inner_lhs, Sugg::hir(cx, inner_rhs, ".."), maybe_neg_sugg(rhs)) - } else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) { + } else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) + && cx.typeck_results().expr_ty(lhs).is_floating_point() + { (inner_lhs, maybe_neg_sugg(inner_rhs), Sugg::hir(cx, lhs, "..")) } else { return; diff --git a/tests/ui/floating_point_mul_add.fixed b/tests/ui/floating_point_mul_add.fixed index a4d6d49e57c..3ce2edf2c71 100644 --- a/tests/ui/floating_point_mul_add.fixed +++ b/tests/ui/floating_point_mul_add.fixed @@ -39,3 +39,21 @@ fn main() { // Cases where the lint shouldn't be applied let _ = (a * a + b * b).sqrt(); } + +fn _issue11831() { + struct NotAFloat; + + impl std::ops::Add<f64> for NotAFloat { + type Output = Self; + + fn add(self, _: f64) -> Self { + NotAFloat + } + } + + let a = NotAFloat; + let b = 1.0_f64; + let c = 1.0; + + let _ = a + b * c; +} diff --git a/tests/ui/floating_point_mul_add.rs b/tests/ui/floating_point_mul_add.rs index 262a20f0f55..b5e4a8db4db 100644 --- a/tests/ui/floating_point_mul_add.rs +++ b/tests/ui/floating_point_mul_add.rs @@ -39,3 +39,21 @@ fn main() { // Cases where the lint shouldn't be applied let _ = (a * a + b * b).sqrt(); } + +fn _issue11831() { + struct NotAFloat; + + impl std::ops::Add<f64> for NotAFloat { + type Output = Self; + + fn add(self, _: f64) -> Self { + NotAFloat + } + } + + let a = NotAFloat; + let b = 1.0_f64; + let c = 1.0; + + let _ = a + b * c; +} |
