-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add suggestion for mul_add #4602
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_errors::Applicability; | ||
|
||
use crate::utils::*; | ||
|
||
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:** None. | ||
/// | ||
/// **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, | ||
perf, | ||
"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) -> Option<(&'b Expr, &'b Expr)> { | ||
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, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this will work, because some of the suggestions have to be applied one after another. If that's the case, just split up the test in a
run-rustfix
file and a non-run-rustfix
file.