Skip to content

Commit 1eb2920

Browse files
committed
add more test cases and clauses
1 parent 71ff9a0 commit 1eb2920

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,22 +462,51 @@ fn has_ambiguous_float_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
462462
ExprKind::Path(qpath) => {
463463
if let Res::Local(hir_id) = cx.qpath_res(qpath, expr.hir_id) {
464464
if let Node::LetStmt(local) = cx.tcx.parent_hir_node(hir_id) {
465-
// If the local has no type annotation and the initializer is an unsuffixed float literal,
466-
// then the type is ambiguous
465+
// If the local has no type annotation, check if the initializer has ambiguous float literals
467466
if local.ty.is_none() {
468467
if let Some(init) = local.init {
469-
if let ExprKind::Lit(lit) = &init.kind {
470-
if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node {
471-
return true;
472-
}
473-
}
468+
return has_ambiguous_float_literal_in_expr(cx, init);
474469
}
475470
}
476471
}
477472
}
478473
false
479474
},
480-
ExprKind::Binary(_, lhs, rhs) => has_ambiguous_float_type(cx, lhs) || has_ambiguous_float_type(cx, rhs),
475+
_ => false, // only check path
476+
}
477+
}
478+
479+
// Recursively check if an expression contains any unsuffixed float literals
480+
fn has_ambiguous_float_literal_in_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
481+
match &expr.kind {
482+
ExprKind::Lit(lit) => {
483+
if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node {
484+
return true;
485+
}
486+
false
487+
},
488+
ExprKind::Binary(_, lhs, rhs) => {
489+
has_ambiguous_float_literal_in_expr(cx, lhs) || has_ambiguous_float_literal_in_expr(cx, rhs)
490+
},
491+
ExprKind::Unary(_, expr) => has_ambiguous_float_literal_in_expr(cx, expr),
492+
ExprKind::If(_, then, else_) => {
493+
has_ambiguous_float_literal_in_expr(cx, then)
494+
|| else_
495+
.as_ref()
496+
.map_or(false, |else_expr| has_ambiguous_float_literal_in_expr(cx, else_expr))
497+
},
498+
ExprKind::Block(block, _) => block
499+
.expr
500+
.as_ref()
501+
.map_or(false, |expr| has_ambiguous_float_literal_in_expr(cx, expr)),
502+
ExprKind::MethodCall(_, receiver, args, _) => {
503+
has_ambiguous_float_literal_in_expr(cx, receiver)
504+
|| args.iter().any(|arg| has_ambiguous_float_literal_in_expr(cx, arg))
505+
},
506+
ExprKind::Call(func, args) => {
507+
has_ambiguous_float_literal_in_expr(cx, func)
508+
|| args.iter().any(|arg| has_ambiguous_float_literal_in_expr(cx, arg))
509+
},
481510
_ => false,
482511
}
483512
}

tests/ui/floating_point_mul_add.fixed

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,24 @@ fn _issue11831() {
7070
let _ = a + b * c;
7171
}
7272

73-
fn _issue12331() {
73+
fn _issue14897() {
7474
let x = 1.0;
7575
let _ = x * 2.0 + 0.5; // should not suggest mul_add
7676
let _ = 0.5 + x * 2.0; // should not suggest mul_add
77+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
78+
let _ = 1.2 + x * 1.2; // should not suggest mul_add
79+
80+
let x = -1.0;
81+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
82+
83+
let x = { 4.0 };
84+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
85+
86+
let x = if 1 > 2 { 1.0 } else { 2.0 };
87+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
88+
89+
let x = 2.4 + 1.2;
90+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
7791

7892
let _ = 2.0f64.mul_add(x, 0.5);
7993
//~^ suboptimal_flops

tests/ui/floating_point_mul_add.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ fn _issue14897() {
7474
let x = 1.0;
7575
let _ = x * 2.0 + 0.5; // should not suggest mul_add
7676
let _ = 0.5 + x * 2.0; // should not suggest mul_add
77+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
78+
let _ = 1.2 + x * 1.2; // should not suggest mul_add
79+
80+
let x = -1.0;
81+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
82+
83+
let x = { 4.0 };
84+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
85+
86+
let x = if 1 > 2 { 1.0 } else { 2.0 };
87+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
88+
89+
let x = 2.4 + 1.2;
90+
let _ = 0.5 + x * 1.2; // should not suggest mul_add
7791

7892
let _ = 0.5 + 2.0 * x;
7993
//~^ suboptimal_flops

tests/ui/floating_point_mul_add.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,34 +80,40 @@ LL | let _ = a - (b * u as f64);
8080
| ^^^^^^^^^^^^^^^^^^ help: consider using: `b.mul_add(-(u as f64), a)`
8181

8282
error: multiply and add expressions can be calculated more efficiently and accurately
83-
--> tests/ui/floating_point_mul_add.rs:78:13
83+
--> tests/ui/floating_point_mul_add.rs:85:13
84+
|
85+
LL | let _ = 0.5 + x * 1.2; // should not suggest mul_add
86+
| ^^^^^^^^^^^^^ help: consider using: `x.mul_add(1.2, 0.5)`
87+
88+
error: multiply and add expressions can be calculated more efficiently and accurately
89+
--> tests/ui/floating_point_mul_add.rs:96:13
8490
|
8591
LL | let _ = 0.5 + 2.0 * x;
8692
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(x, 0.5)`
8793

8894
error: multiply and add expressions can be calculated more efficiently and accurately
89-
--> tests/ui/floating_point_mul_add.rs:80:13
95+
--> tests/ui/floating_point_mul_add.rs:98:13
9096
|
9197
LL | let _ = 2.0 * x + 0.5;
9298
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(x, 0.5)`
9399

94100
error: multiply and add expressions can be calculated more efficiently and accurately
95-
--> tests/ui/floating_point_mul_add.rs:83:13
101+
--> tests/ui/floating_point_mul_add.rs:101:13
96102
|
97103
LL | let _ = x + 2.0 * 4.0;
98104
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4.0, x)`
99105

100106
error: multiply and add expressions can be calculated more efficiently and accurately
101-
--> tests/ui/floating_point_mul_add.rs:87:13
107+
--> tests/ui/floating_point_mul_add.rs:105:13
102108
|
103109
LL | let _ = y * 2.0 + 0.5;
104110
| ^^^^^^^^^^^^^ help: consider using: `y.mul_add(2.0, 0.5)`
105111

106112
error: multiply and add expressions can be calculated more efficiently and accurately
107-
--> tests/ui/floating_point_mul_add.rs:89:13
113+
--> tests/ui/floating_point_mul_add.rs:107:13
108114
|
109115
LL | let _ = 1.0 * 2.0 + 0.5;
110116
| ^^^^^^^^^^^^^^^ help: consider using: `1.0f64.mul_add(2.0, 0.5)`
111117

112-
error: aborting due to 18 previous errors
118+
error: aborting due to 19 previous errors
113119

0 commit comments

Comments
 (0)