Skip to content

Commit a2e396b

Browse files
committed
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 paremters, the suggestion was wrong. Fixes #11831
1 parent e8e9510 commit a2e396b

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

clippy_lints/src/floating_point_arithmetic.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,13 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
496496
if let BinOpKind::Sub = op { -sugg } else { sugg }
497497
};
498498

499-
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
499+
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs)
500+
&& cx.typeck_results().expr_ty(rhs).is_floating_point()
501+
{
500502
(inner_lhs, Sugg::hir(cx, inner_rhs, ".."), maybe_neg_sugg(rhs))
501-
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
503+
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs)
504+
&& cx.typeck_results().expr_ty(lhs).is_floating_point()
505+
{
502506
(inner_lhs, maybe_neg_sugg(inner_rhs), Sugg::hir(cx, lhs, ".."))
503507
} else {
504508
return;

tests/ui/floating_point_mul_add.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,21 @@ fn main() {
3939
// Cases where the lint shouldn't be applied
4040
let _ = (a * a + b * b).sqrt();
4141
}
42+
43+
fn _issue11831() {
44+
struct NotAFloat;
45+
46+
impl std::ops::Add<f64> for NotAFloat {
47+
type Output = Self;
48+
49+
fn add(self, _: f64) -> Self {
50+
NotAFloat
51+
}
52+
}
53+
54+
let a = NotAFloat;
55+
let b = 1.0_f64;
56+
let c = 1.0;
57+
58+
let _ = a + b * c;
59+
}

tests/ui/floating_point_mul_add.rs

+18
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,21 @@ fn main() {
3939
// Cases where the lint shouldn't be applied
4040
let _ = (a * a + b * b).sqrt();
4141
}
42+
43+
fn _issue11831() {
44+
struct NotAFloat;
45+
46+
impl std::ops::Add<f64> for NotAFloat {
47+
type Output = Self;
48+
49+
fn add(self, _: f64) -> Self {
50+
NotAFloat
51+
}
52+
}
53+
54+
let a = NotAFloat;
55+
let b = 1.0_f64;
56+
let c = 1.0;
57+
58+
let _ = a + b * c;
59+
}

0 commit comments

Comments
 (0)