Move manual_mul_add into suboptimal_flops lint

This commit is contained in:
Krishna Sai Veera Reddy 2020-02-23 00:04:11 -08:00
parent 454e505c12
commit 4065ca9c8c
16 changed files with 179 additions and 290 deletions

View File

@ -1210,7 +1210,6 @@ Released 2018-09-13
[`lossy_float_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#lossy_float_literal
[`main_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#main_recursion
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
[`manual_mul_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_mul_add
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
[`manual_swap`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
[`many_single_char_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#many_single_char_names

View File

@ -2,11 +2,11 @@ use crate::consts::{
constant, Constant,
Constant::{F32, F64},
};
use crate::utils::*;
use crate::utils::{span_lint_and_sugg, sugg};
use if_chain::if_chain;
use rustc::ty;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::f32::consts as f32_consts;
@ -39,6 +39,7 @@ declare_clippy_lint! {
/// let _ = (1.0 + a).ln();
/// let _ = a.exp() - 1.0;
/// let _ = a.powf(2.0);
/// let _ = a * 2.0 + 4.0;
/// ```
///
/// is better expressed as
@ -57,6 +58,7 @@ declare_clippy_lint! {
/// let _ = a.ln_1p();
/// let _ = a.exp_m1();
/// let _ = a.powi(2);
/// let _ = a.mul_add(2.0, 4.0);
/// ```
pub SUBOPTIMAL_FLOPS,
nursery,
@ -211,12 +213,12 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
let (help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
(
"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], "..")),
)
} else if F32(1.0 / 3.0) == value || F64(1.0 / 3.0) == value {
(
"cube-root of a number can be computed more accurately",
format!("{}.cbrt()", Sugg::hir(cx, &args[0], ".."))
format!("{}.cbrt()", Sugg::hir(cx, &args[0], "..")),
)
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
(
@ -225,7 +227,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
"{}.powi({})",
Sugg::hir(cx, &args[0], ".."),
format_numeric_literal(&exponent.to_string(), None, false)
)
),
)
} else {
return;
@ -272,6 +274,52 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
}
}
fn is_float_mul_expr<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
if_chain! {
if let ExprKind::Binary(op, ref lhs, ref rhs) = &expr.kind;
if let BinOpKind::Mul = op.node;
if cx.tables.expr_ty(lhs).is_floating_point();
if cx.tables.expr_ty(rhs).is_floating_point();
then {
return Some((lhs, rhs));
}
}
None
}
// TODO: Fix rust-lang/rust-clippy#4735
fn check_fma(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
if_chain! {
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind;
if let BinOpKind::Add = op.node;
then {
let (recv, arg1, arg2) = if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, lhs) {
(inner_lhs, inner_rhs, rhs)
} else if let Some((inner_lhs, inner_rhs)) = is_float_mul_expr(cx, rhs) {
(inner_lhs, inner_rhs, lhs)
} else {
return;
};
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"multiply and add expressions can be calculated more efficiently and accurately",
"consider using",
format!(
"{}.mul_add({}, {})",
prepare_receiver_sugg(cx, recv),
Sugg::hir(cx, arg1, ".."),
Sugg::hir(cx, arg2, ".."),
),
Applicability::MachineApplicable,
);
}
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
@ -287,6 +335,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
}
} else {
check_expm1(cx, expr);
check_fma(cx, expr);
}
}
}

View File

@ -208,6 +208,7 @@ pub mod exit;
pub mod explicit_write;
pub mod fallible_impl_from;
pub mod float_literal;
pub mod floating_point_arithmetic;
pub mod format;
pub mod formatting;
pub mod functions;
@ -248,7 +249,6 @@ pub mod missing_const_for_fn;
pub mod missing_doc;
pub mod missing_inline;
pub mod modulo_arithmetic;
pub mod mul_add;
pub mod multiple_crate_versions;
pub mod mut_key;
pub mod mut_mut;
@ -691,7 +691,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS,
&missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS,
&modulo_arithmetic::MODULO_ARITHMETIC,
&mul_add::MANUAL_MUL_ADD,
&multiple_crate_versions::MULTIPLE_CRATE_VERSIONS,
&mut_key::MUTABLE_KEY_TYPE,
&mut_mut::MUT_MUT,
@ -967,7 +966,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box inherent_to_string::InherentToString);
store.register_late_pass(|| box trait_bounds::TraitBounds);
store.register_late_pass(|| box comparison_chain::ComparisonChain);
store.register_late_pass(|| box mul_add::MulAddCheck);
store.register_late_pass(|| box mut_key::MutableKeyType);
store.register_late_pass(|| box modulo_arithmetic::ModuloArithmetic);
store.register_early_pass(|| box reference::DerefAddrOf);
@ -1652,7 +1650,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
LintId::of(&mul_add::MANUAL_MUL_ADD),
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
LintId::of(&mutex_atomic::MUTEX_INTEGER),
LintId::of(&needless_borrow::NEEDLESS_BORROW),

View File

@ -1,111 +0,0 @@
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use crate::utils::{snippet, span_lint_and_sugg};
declare_clippy_lint! {
/// **What it does:** Checks for expressions of the form `a * b + c`
/// or `c + a * b` where `a`, `b`, `c` are floats and suggests using
/// `a.mul_add(b, c)` instead.
///
/// **Why is this bad?** Calculating `a * b + c` may lead to slight
/// numerical inaccuracies as `a * b` is rounded before being added to
/// `c`. Depending on the target architecture, `mul_add()` may be more
/// performant.
///
/// **Known problems:** This lint can emit semantic incorrect suggestions.
/// For example, for `a * b * c + d` the suggestion `a * b.mul_add(c, d)`
/// is emitted, which is equivalent to `a * (b * c + d)`. (#4735)
///
/// **Example:**
///
/// ```rust
/// # let a = 0_f32;
/// # let b = 0_f32;
/// # let c = 0_f32;
/// let foo = (a * b) + c;
/// ```
///
/// can be written as
///
/// ```rust
/// # let a = 0_f32;
/// # let b = 0_f32;
/// # let c = 0_f32;
/// let foo = a.mul_add(b, c);
/// ```
pub MANUAL_MUL_ADD,
nursery,
"Using `a.mul_add(b, c)` for floating points has higher numerical precision than `a * b + c`"
}
declare_lint_pass!(MulAddCheck => [MANUAL_MUL_ADD]);
fn is_float<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>) -> bool {
cx.tables.expr_ty(expr).is_floating_point()
}
// Checks whether expression is multiplication of two floats
fn is_float_mult_expr<'a, 'tcx, 'b>(
cx: &LateContext<'a, 'tcx>,
expr: &'b Expr<'b>,
) -> Option<(&'b Expr<'b>, &'b Expr<'b>)> {
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind {
if let BinOpKind::Mul = op.node {
if is_float(cx, &lhs) && is_float(cx, &rhs) {
return Some((&lhs, &rhs));
}
}
}
None
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MulAddCheck {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind {
if let BinOpKind::Add = op.node {
//Converts mult_lhs * mult_rhs + rhs to mult_lhs.mult_add(mult_rhs, rhs)
if let Some((mult_lhs, mult_rhs)) = is_float_mult_expr(cx, lhs) {
if is_float(cx, rhs) {
span_lint_and_sugg(
cx,
MANUAL_MUL_ADD,
expr.span,
"consider using `mul_add()` for better numerical precision",
"try",
format!(
"{}.mul_add({}, {})",
snippet(cx, mult_lhs.span, "_"),
snippet(cx, mult_rhs.span, "_"),
snippet(cx, rhs.span, "_"),
),
Applicability::MaybeIncorrect,
);
}
}
//Converts lhs + mult_lhs * mult_rhs to mult_lhs.mult_add(mult_rhs, lhs)
if let Some((mult_lhs, mult_rhs)) = is_float_mult_expr(cx, rhs) {
if is_float(cx, lhs) {
span_lint_and_sugg(
cx,
MANUAL_MUL_ADD,
expr.span,
"consider using `mul_add()` for better numerical precision",
"try",
format!(
"{}.mul_add({}, {})",
snippet(cx, mult_lhs.span, "_"),
snippet(cx, mult_rhs.span, "_"),
snippet(cx, lhs.span, "_"),
),
Applicability::MaybeIncorrect,
);
}
}
}
}
}
}

View File

@ -1022,13 +1022,6 @@ pub const ALL_LINTS: [Lint; 357] = [
deprecation: None,
module: "loops",
},
Lint {
name: "manual_mul_add",
group: "nursery",
desc: "Using `a.mul_add(b, c)` for floating points has higher numerical precision than `a * b + c`",
deprecation: None,
module: "mul_add",
},
Lint {
name: "manual_saturating_arithmetic",
group: "style",

View File

@ -0,0 +1,21 @@
// run-rustfix
#![warn(clippy::suboptimal_flops)]
fn main() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
let d: f64 = 0.0001;
let _ = a.mul_add(b, c);
let _ = a.mul_add(b, c);
let _ = 2.0f64.mul_add(4.0, a);
let _ = 2.0f64.mul_add(4., a);
let _ = a.mul_add(b, c);
let _ = a.mul_add(b, c);
let _ = (a * b).mul_add(c, d);
let _ = a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c)) + c;
let _ = 1234.567_f64.mul_add(45.67834_f64, 0.0004_f64);
}

View File

@ -0,0 +1,21 @@
// run-rustfix
#![warn(clippy::suboptimal_flops)]
fn main() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
let d: f64 = 0.0001;
let _ = a * b + c;
let _ = c + a * b;
let _ = a + 2.0 * 4.0;
let _ = a + 2. * 4.;
let _ = (a * b) + c;
let _ = c + (a * b);
let _ = a * b * c + d;
let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
}

View File

@ -0,0 +1,58 @@
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:10:13
|
LL | let _ = a * b + c;
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:11:13
|
LL | let _ = c + a * b;
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:12:13
|
LL | let _ = a + 2.0 * 4.0;
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4.0, a)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:13:13
|
LL | let _ = a + 2. * 4.;
| ^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4., a)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:15:13
|
LL | let _ = (a * b) + c;
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:16:13
|
LL | let _ = c + (a * b);
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:17:13
|
LL | let _ = a * b * c + d;
| ^^^^^^^^^^^^^ help: consider using: `(a * b).mul_add(c, d)`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:19:13
|
LL | let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c))`
error: multiply and add expressions can be calculated more efficiently and accurately
--> $DIR/floating_point_fma.rs:20:13
|
LL | let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`
error: aborting due to 9 previous errors

View File

@ -24,34 +24,34 @@ fn check_ln1p() {
let _ = 2.0f32.ln_1p();
let _ = 2.0f32.ln_1p();
let _ = x.ln_1p();
let _ = (x * 2.0).ln_1p();
let _ = (x / 2.0).ln_1p();
let _ = x.powi(2).ln_1p();
let _ = (x.powi(2) * 2.0).ln_1p();
let _ = (x.powi(2) / 2.0).ln_1p();
let _ = ((std::f32::consts::E - 1.0)).ln_1p();
let _ = x.ln_1p();
let _ = x.powi(2).ln_1p();
let _ = (x + 2.0).ln_1p();
let _ = (x * 2.0).ln_1p();
let _ = (x / 2.0).ln_1p();
// Cases where the lint shouldn't be applied
let _ = (1.0 + x + 2.0).ln();
let _ = (x + 1.0 + 2.0).ln();
let _ = (x + 1.0 * 2.0).ln();
let _ = (x + 1.0 / 2.0).ln();
let _ = (1.0 + x - 2.0).ln();
let x = 1f64;
let _ = 2.0f64.ln_1p();
let _ = 2.0f64.ln_1p();
let _ = x.ln_1p();
let _ = (x * 2.0).ln_1p();
let _ = (x / 2.0).ln_1p();
let _ = x.powi(2).ln_1p();
let _ = x.ln_1p();
let _ = x.powi(2).ln_1p();
let _ = (x + 2.0).ln_1p();
let _ = (x * 2.0).ln_1p();
let _ = (x / 2.0).ln_1p();
// Cases where the lint shouldn't be applied
let _ = (1.0 + x + 2.0).ln();
let _ = (x + 1.0 + 2.0).ln();
let _ = (x + 1.0 * 2.0).ln();
let _ = (x + 1.0 / 2.0).ln();
let _ = (1.0 + x - 2.0).ln();
}

View File

@ -24,34 +24,34 @@ fn check_ln1p() {
let _ = (1f32 + 2.).ln();
let _ = (1f32 + 2.0).ln();
let _ = (1.0 + x).ln();
let _ = (1.0 + x * 2.0).ln();
let _ = (1.0 + x / 2.0).ln();
let _ = (1.0 + x.powi(2)).ln();
let _ = (1.0 + x.powi(2) * 2.0).ln();
let _ = (1.0 + x.powi(2) / 2.0).ln();
let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
let _ = (x + 1.0).ln();
let _ = (x.powi(2) + 1.0).ln();
let _ = (x + 2.0 + 1.0).ln();
let _ = (x * 2.0 + 1.0).ln();
let _ = (x / 2.0 + 1.0).ln();
// Cases where the lint shouldn't be applied
let _ = (1.0 + x + 2.0).ln();
let _ = (x + 1.0 + 2.0).ln();
let _ = (x + 1.0 * 2.0).ln();
let _ = (x + 1.0 / 2.0).ln();
let _ = (1.0 + x - 2.0).ln();
let x = 1f64;
let _ = (1f64 + 2.).ln();
let _ = (1f64 + 2.0).ln();
let _ = (1.0 + x).ln();
let _ = (1.0 + x * 2.0).ln();
let _ = (1.0 + x / 2.0).ln();
let _ = (1.0 + x.powi(2)).ln();
let _ = (x + 1.0).ln();
let _ = (x.powi(2) + 1.0).ln();
let _ = (x + 2.0 + 1.0).ln();
let _ = (x * 2.0 + 1.0).ln();
let _ = (x / 2.0 + 1.0).ln();
// Cases where the lint shouldn't be applied
let _ = (1.0 + x + 2.0).ln();
let _ = (x + 1.0 + 2.0).ln();
let _ = (x + 1.0 * 2.0).ln();
let _ = (x + 1.0 / 2.0).ln();
let _ = (1.0 + x - 2.0).ln();
}

View File

@ -69,8 +69,8 @@ LL | let _ = (1.0 + x).ln();
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:27:13
|
LL | let _ = (1.0 + x * 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
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
@ -81,8 +81,8 @@ LL | let _ = (1.0 + x.powi(2)).ln();
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:29:13
|
LL | let _ = (1.0 + x.powi(2) * 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(2) * 2.0).ln_1p()`
LL | let _ = (1.0 + x.powi(2) / 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x.powi(2) / 2.0).ln_1p()`
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:30:13
@ -111,8 +111,8 @@ LL | let _ = (x + 2.0 + 1.0).ln();
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:34:13
|
LL | let _ = (x * 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
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
@ -135,8 +135,8 @@ LL | let _ = (1.0 + x).ln();
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:45:13
|
LL | let _ = (1.0 + x * 2.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
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
@ -165,8 +165,8 @@ LL | let _ = (x + 2.0 + 1.0).ln();
error: ln(1 + x) can be computed more accurately
--> $DIR/floating_point_log.rs:50:13
|
LL | let _ = (x * 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x * 2.0).ln_1p()`
LL | let _ = (x / 2.0 + 1.0).ln();
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x / 2.0).ln_1p()`
error: aborting due to 28 previous errors

View File

@ -1,16 +0,0 @@
#![warn(clippy::manual_mul_add)]
#![allow(unused_variables)]
fn mul_add_test() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
// Examples of not auto-fixable expressions
let test1 = (a * b + c) * (c + a * b) + (c + (a * b) + c);
let test2 = 1234.567 * 45.67834 + 0.0004;
}
fn main() {
mul_add_test();
}

View File

@ -1,34 +0,0 @@
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add.rs:10:17
|
LL | let test1 = (a * b + c) * (c + a * b) + (c + (a * b) + c);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(a * b + c).mul_add((c + a * b), (c + (a * b) + c))`
|
= note: `-D clippy::manual-mul-add` implied by `-D warnings`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add.rs:10:17
|
LL | let test1 = (a * b + c) * (c + a * b) + (c + (a * b) + c);
| ^^^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add.rs:10:31
|
LL | let test1 = (a * b + c) * (c + a * b) + (c + (a * b) + c);
| ^^^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add.rs:10:46
|
LL | let test1 = (a * b + c) * (c + a * b) + (c + (a * b) + c);
| ^^^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add.rs:11:17
|
LL | let test2 = 1234.567 * 45.67834 + 0.0004;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1234.567.mul_add(45.67834, 0.0004)`
error: aborting due to 5 previous errors

View File

@ -1,24 +0,0 @@
// run-rustfix
#![warn(clippy::manual_mul_add)]
#![allow(unused_variables)]
fn mul_add_test() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
// Auto-fixable examples
let test1 = a.mul_add(b, c);
let test2 = a.mul_add(b, c);
let test3 = a.mul_add(b, c);
let test4 = a.mul_add(b, c);
let test5 = a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c)) + c;
let test6 = 1234.567_f64.mul_add(45.67834_f64, 0.0004_f64);
}
fn main() {
mul_add_test();
}

View File

@ -1,24 +0,0 @@
// run-rustfix
#![warn(clippy::manual_mul_add)]
#![allow(unused_variables)]
fn mul_add_test() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
// Auto-fixable examples
let test1 = a * b + c;
let test2 = c + a * b;
let test3 = (a * b) + c;
let test4 = c + (a * b);
let test5 = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
let test6 = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
}
fn main() {
mul_add_test();
}

View File

@ -1,40 +0,0 @@
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:12:17
|
LL | let test1 = a * b + c;
| ^^^^^^^^^ help: try: `a.mul_add(b, c)`
|
= note: `-D clippy::manual-mul-add` implied by `-D warnings`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:13:17
|
LL | let test2 = c + a * b;
| ^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:15:17
|
LL | let test3 = (a * b) + c;
| ^^^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:16:17
|
LL | let test4 = c + (a * b);
| ^^^^^^^^^^^ help: try: `a.mul_add(b, c)`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:18:17
|
LL | let test5 = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c))`
error: consider using `mul_add()` for better numerical precision
--> $DIR/mul_add_fixable.rs:19:17
|
LL | let test6 = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`
error: aborting due to 6 previous errors