From ce0dc9b70e1f021043bb3b4edfb8068590bc8325 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Thu, 27 Feb 2020 22:31:41 -0800 Subject: [PATCH 01/11] Created floating point abs lint and test, but not yet run --- clippy_lints/src/floating_point_arithmetic.rs | 75 ++++++++++++++++++- tests/ui/floating_point_abs.rs | 59 +++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 tests/ui/floating_point_abs.rs diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index eed4f58cf90..810c6e1412a 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -2,11 +2,11 @@ use crate::consts::{ constant, Constant, Constant::{F32, F64}, }; -use crate::utils::{span_lint_and_sugg, sugg}; +use crate::utils::{higher, span_lint_and_sugg, sugg, SpanlessEq}; use if_chain::if_chain; use rustc::ty; use rustc_errors::Applicability; -use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; +use rustc_hir::{BinOpKind, Expr, ExprKind, Lit, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; @@ -14,7 +14,7 @@ use rustc_span::source_map::Spanned; use std::f32::consts as f32_consts; use std::f64::consts as f64_consts; use sugg::{format_numeric_literal, Sugg}; -use syntax::ast; +use syntax::ast::{self, FloatTy, LitFloatType, LitKind}; declare_clippy_lint! { /// **What it does:** Looks for floating-point expressions that @@ -72,6 +72,16 @@ declare_clippy_lint! { /// let _ = a.log(E); /// let _ = a.powf(2.0); /// let _ = a * 2.0 + 4.0; + /// let _ = if a < 0.0 { + /// -a + /// } else { + /// a + /// } + /// let _ = if a < 0.0 { + /// a + /// } else { + /// -a + /// } /// ``` /// /// is better expressed as @@ -88,6 +98,8 @@ declare_clippy_lint! { /// let _ = a.ln(); /// let _ = a.powi(2); /// let _ = a.mul_add(2.0, 4.0); + /// let _ = a.abs(); + /// let _ = -a.abs(); /// ``` pub SUBOPTIMAL_FLOPS, nursery, @@ -359,6 +371,62 @@ fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { } } +/// Returns true iff expr is an expression which tests whether or not +/// test is positive or an expression which tests whether or not test +/// is nonnegative. +/// Used for check-custom-abs function below +fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool { + if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind { + match op { + BinOpKind::Gt | BinOpKind::Ge => is_zero(right) && are_exprs_equal(cx, left, test), + BinOpKind::Lt | BinOpKind::Le => is_zero(left) && are_exprs_equal(cx, right, test), + _ => false, + } + } else { + false + } +} + +fn are_exprs_equal(cx: &LateContext<'_, '_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool { + SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2) +} + +/// Returns true iff expr is some zero literal +fn is_zero(expr: &Expr<'_>) -> bool { + if let ExprKind::Lit(Lit { node: lit, .. }) = &expr.kind { + match lit { + LitKind::Int(0, _) => true, + LitKind::Float(symb, LitFloatType::Unsuffixed) + | LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F64)) => { + symb.as_str().parse::().unwrap() == 0.0 + }, + LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F32)) => symb.as_str().parse::().unwrap() == 0.0, + _ => false, + } + } else { + false + } +} + +fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { +// if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { + // Check for the positive-first variant +// if let ExprKind::Unary(UnOp::UnNeg, expr) = else_body.kind { +// if are_exprs_equal(cx, expr, body) && is_testing_positive(cx, cond, body) { + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + "This looks like you've implemented your own absolute value function", + "try", + "a.abs()".to_string(),//format!("{:?}.abs()", body), + 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 { @@ -375,6 +443,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic { } else { check_expm1(cx, expr); check_mul_add(cx, expr); + check_custom_abs(cx, expr); } } } diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs new file mode 100644 index 00000000000..2cb1db09541 --- /dev/null +++ b/tests/ui/floating_point_abs.rs @@ -0,0 +1,59 @@ +#[warn(clippy::suboptimal_flops)] + +fn fake_abs1(num: f64) -> f64 { + if num >= 0.0 { + num + } else { + -num + } +} + +fn fake_abs2(num: f64) -> f64 { + if 0.0 < num { + num + } else { + -num + } +} + +fn fake_nabs1(num: f64) -> f64 { + if num < 0.0 { + num + } else { + -num + } +} + +fn fake_nabs2(num: f64) -> f64 { + if 0.0 >= num { + num + } else { + -num + } +} + +fn not_fake_abs1(num: f64) -> f64 { + if num > 0.0 { + num + } else { + -num - 1f64 + } +} + +fn not_fake_abs2(num: f64) -> f64 { + if num > 0.0 { + num + 1.0 + } else { + -(num + 1.0) + } +} + +fn not_fake_abs3(num1: f64, num2: f64) -> f64 { + if num1 > 0.0 { + num2 + } else { + -num2 + } +} + +fn main() {} From 5a21661ce54bf2485e90d21282e1fe7be45879af Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Fri, 28 Feb 2020 12:40:13 -0800 Subject: [PATCH 02/11] Some bugfixing --- clippy_lints/src/floating_point_arithmetic.rs | 58 +++++++++++++------ tests/ui/floating_point_abs.rs | 36 ++++++++++++ tests/ui/floating_point_abs.stderr | 14 +++++ 3 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 tests/ui/floating_point_abs.stderr diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index 810c6e1412a..970aeef623e 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -6,7 +6,7 @@ use crate::utils::{higher, span_lint_and_sugg, sugg, SpanlessEq}; use if_chain::if_chain; use rustc::ty; use rustc_errors::Applicability; -use rustc_hir::{BinOpKind, Expr, ExprKind, Lit, UnOp}; +use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Lit, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; @@ -409,22 +409,46 @@ fn is_zero(expr: &Expr<'_>) -> bool { } fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { -// if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { - // Check for the positive-first variant -// if let ExprKind::Unary(UnOp::UnNeg, expr) = else_body.kind { -// if are_exprs_equal(cx, expr, body) && is_testing_positive(cx, cond, body) { - span_lint_and_sugg( - cx, - SUBOPTIMAL_FLOPS, - expr.span, - "This looks like you've implemented your own absolute value function", - "try", - "a.abs()".to_string(),//format!("{:?}.abs()", body), - Applicability::MachineApplicable, - ); -// } -// } -// } + if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { + if let ExprKind::Block( + Block { + stmts: [], + expr: + Some(Expr { + kind: ExprKind::Unary(UnOp::UnNeg, else_expr), + .. + }), + .. + }, + _, + ) = else_body.kind + { + if let ExprKind::Block( + Block { + stmts: [], + expr: Some(body), + .. + }, + _, + ) = &body.kind + { + if are_exprs_equal(cx, else_expr, body) { + dbg!("if (cond) body else -body\nbody: {:?}", &body.kind); + if is_testing_positive(cx, cond, body) { + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + "This looks like you've implemented your own absolute value function", + "try", + format!("{}.abs()", Sugg::hir(cx, body, "..")), + Applicability::MachineApplicable, + ); + } + } + } + } + } } impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic { diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs index 2cb1db09541..0efc7092899 100644 --- a/tests/ui/floating_point_abs.rs +++ b/tests/ui/floating_point_abs.rs @@ -1,4 +1,8 @@ #[warn(clippy::suboptimal_flops)] +struct A { + a: f64, + b: f64 +} fn fake_abs1(num: f64) -> f64 { if num >= 0.0 { @@ -16,6 +20,14 @@ fn fake_abs2(num: f64) -> f64 { } } +fn fake_abs3(a: A) -> f64 { + if a.a > 0.0 { + a.a + } else { + -a.a + } +} + fn fake_nabs1(num: f64) -> f64 { if num < 0.0 { num @@ -32,6 +44,14 @@ fn fake_nabs2(num: f64) -> f64 { } } +fn fake_nabs3(a: A) -> A { + A { a: if a.a >= 0.0 { + a.a + } else { + -a.a + }, b: a.b } +} + fn not_fake_abs1(num: f64) -> f64 { if num > 0.0 { num @@ -56,4 +76,20 @@ fn not_fake_abs3(num1: f64, num2: f64) -> f64 { } } +fn not_fake_abs4(a: A) -> f64 { + if a.a > 0.0 { + a.b + } else { + -a.b + } +} + +fn not_fake_abs5(a: A) -> f64 { + if a.a > 0.0 { + a.a + } else { + -a.b + } +} + fn main() {} diff --git a/tests/ui/floating_point_abs.stderr b/tests/ui/floating_point_abs.stderr new file mode 100644 index 00000000000..bbd67de17c0 --- /dev/null +++ b/tests/ui/floating_point_abs.stderr @@ -0,0 +1,14 @@ +error: This looks like you've implemented your own absolute value function + --> $DIR/floating_point_abs.rs:4:5 + | +LL | / if num >= 0.0 { +LL | | num +LL | | } else { +LL | | -num +LL | | } + | |_____^ help: try: `num.abs()` + | + = note: `-D clippy::suboptimal-flops` implied by `-D warnings` + +error: aborting due to previous error + From 028cddb95628252180bf6146b445e146dcdef8b2 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sat, 29 Feb 2020 13:46:59 -0800 Subject: [PATCH 03/11] Finished checking for cases of absolute values --- clippy_lints/src/floating_point_arithmetic.rs | 75 +++++++++++++------ tests/ui/floating_point_abs.rs | 23 +++++- tests/ui/floating_point_abs.stderr | 75 ++++++++++++++++++- 3 files changed, 145 insertions(+), 28 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index 970aeef623e..8a6ae10ab0b 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -387,6 +387,18 @@ fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_ } } +fn is_testing_negative(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool { + if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind { + match op { + BinOpKind::Gt | BinOpKind::Ge => is_zero(left) && are_exprs_equal(cx, right, test), + BinOpKind::Lt | BinOpKind::Le => is_zero(right) && are_exprs_equal(cx, left, test), + _ => false, + } + } else { + false + } +} + fn are_exprs_equal(cx: &LateContext<'_, '_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool { SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2) } @@ -410,30 +422,9 @@ fn is_zero(expr: &Expr<'_>) -> bool { fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { - if let ExprKind::Block( - Block { - stmts: [], - expr: - Some(Expr { - kind: ExprKind::Unary(UnOp::UnNeg, else_expr), - .. - }), - .. - }, - _, - ) = else_body.kind - { - if let ExprKind::Block( - Block { - stmts: [], - expr: Some(body), - .. - }, - _, - ) = &body.kind - { + if let ExprKind::Block( Block { stmts: [], expr: Some(Expr { kind: ExprKind::Unary(UnOp::UnNeg, else_expr), .. }), .. }, _,) = else_body.kind { + if let ExprKind::Block( Block { stmts: [], expr: Some(body), .. }, _,) = &body.kind { if are_exprs_equal(cx, else_expr, body) { - dbg!("if (cond) body else -body\nbody: {:?}", &body.kind); if is_testing_positive(cx, cond, body) { span_lint_and_sugg( cx, @@ -444,6 +435,44 @@ fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { format!("{}.abs()", Sugg::hir(cx, body, "..")), Applicability::MachineApplicable, ); + } else if is_testing_negative(cx, cond, body) { + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + "This looks like you've implemented your own negative absolute value function", + "try", + format!("-{}.abs()", Sugg::hir(cx, body, "..")), + Applicability::MachineApplicable, + ); + } + } + } + } + if let ExprKind::Block( Block { stmts: [], expr: Some(Expr { kind: ExprKind::Unary(UnOp::UnNeg, else_expr), .. }), .. }, _,) = &body.kind + { + if let ExprKind::Block( Block { stmts: [], expr: Some(body), .. }, _,) = &else_body.kind { + if are_exprs_equal(cx, else_expr, body) { + if is_testing_negative(cx, cond, body) { + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + "This looks like you've implemented your own absolute value function", + "try", + format!("{}.abs()", Sugg::hir(cx, body, "..")), + Applicability::MachineApplicable, + ); + } else if is_testing_positive(cx, cond, body) { + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + "This looks like you've implemented your own negative absolute value function", + "try", + format!("-{}.abs()", Sugg::hir(cx, body, "..")), + Applicability::MachineApplicable, + ); } } } diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs index 0efc7092899..40d2ff7e859 100644 --- a/tests/ui/floating_point_abs.rs +++ b/tests/ui/floating_point_abs.rs @@ -1,4 +1,5 @@ -#[warn(clippy::suboptimal_flops)] +#![warn(clippy::suboptimal_flops)] + struct A { a: f64, b: f64 @@ -28,6 +29,22 @@ fn fake_abs3(a: A) -> f64 { } } +fn fake_abs4(num: f64) -> f64 { + if 0.0 >= num { + -num + } else { + num + } +} + +fn fake_abs5(a: A) -> f64 { + if a.a < 0.0 { + -a.a + } else { + a.a + } +} + fn fake_nabs1(num: f64) -> f64 { if num < 0.0 { num @@ -46,9 +63,9 @@ fn fake_nabs2(num: f64) -> f64 { fn fake_nabs3(a: A) -> A { A { a: if a.a >= 0.0 { - a.a - } else { -a.a + } else { + a.a }, b: a.b } } diff --git a/tests/ui/floating_point_abs.stderr b/tests/ui/floating_point_abs.stderr index bbd67de17c0..dd648a8a272 100644 --- a/tests/ui/floating_point_abs.stderr +++ b/tests/ui/floating_point_abs.stderr @@ -1,5 +1,5 @@ error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:4:5 + --> $DIR/floating_point_abs.rs:9:5 | LL | / if num >= 0.0 { LL | | num @@ -10,5 +10,76 @@ LL | | } | = note: `-D clippy::suboptimal-flops` implied by `-D warnings` -error: aborting due to previous error +error: This looks like you've implemented your own absolute value function + --> $DIR/floating_point_abs.rs:17:5 + | +LL | / if 0.0 < num { +LL | | num +LL | | } else { +LL | | -num +LL | | } + | |_____^ help: try: `num.abs()` + +error: This looks like you've implemented your own absolute value function + --> $DIR/floating_point_abs.rs:25:5 + | +LL | / if a.a > 0.0 { +LL | | a.a +LL | | } else { +LL | | -a.a +LL | | } + | |_____^ help: try: `a.a.abs()` + +error: This looks like you've implemented your own absolute value function + --> $DIR/floating_point_abs.rs:33:5 + | +LL | / if 0.0 >= num { +LL | | -num +LL | | } else { +LL | | num +LL | | } + | |_____^ help: try: `num.abs()` + +error: This looks like you've implemented your own absolute value function + --> $DIR/floating_point_abs.rs:41:5 + | +LL | / if a.a < 0.0 { +LL | | -a.a +LL | | } else { +LL | | a.a +LL | | } + | |_____^ help: try: `a.a.abs()` + +error: This looks like you've implemented your own negative absolute value function + --> $DIR/floating_point_abs.rs:49:5 + | +LL | / if num < 0.0 { +LL | | num +LL | | } else { +LL | | -num +LL | | } + | |_____^ help: try: `-num.abs()` + +error: This looks like you've implemented your own negative absolute value function + --> $DIR/floating_point_abs.rs:57:5 + | +LL | / if 0.0 >= num { +LL | | num +LL | | } else { +LL | | -num +LL | | } + | |_____^ help: try: `-num.abs()` + +error: This looks like you've implemented your own negative absolute value function + --> $DIR/floating_point_abs.rs:65:12 + | +LL | A { a: if a.a >= 0.0 { + | ____________^ +LL | | -a.a +LL | | } else { +LL | | a.a +LL | | }, b: a.b } + | |_________^ help: try: `-a.a.abs()` + +error: aborting due to 8 previous errors From bfa2691559bf9dd26dc90aa564305aef1f164aec Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sat, 29 Feb 2020 18:51:39 -0800 Subject: [PATCH 04/11] Run cargo dev fmt --- clippy_lints/src/floating_point_arithmetic.rs | 47 +++++++++++++++++-- tests/ui/floating_point_abs.rs | 11 ++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index 8a6ae10ab0b..443ccf17896 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -422,8 +422,28 @@ fn is_zero(expr: &Expr<'_>) -> bool { fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { - if let ExprKind::Block( Block { stmts: [], expr: Some(Expr { kind: ExprKind::Unary(UnOp::UnNeg, else_expr), .. }), .. }, _,) = else_body.kind { - if let ExprKind::Block( Block { stmts: [], expr: Some(body), .. }, _,) = &body.kind { + if let ExprKind::Block( + Block { + stmts: [], + expr: + Some(Expr { + kind: ExprKind::Unary(UnOp::UnNeg, else_expr), + .. + }), + .. + }, + _, + ) = else_body.kind + { + if let ExprKind::Block( + Block { + stmts: [], + expr: Some(body), + .. + }, + _, + ) = &body.kind + { if are_exprs_equal(cx, else_expr, body) { if is_testing_positive(cx, cond, body) { span_lint_and_sugg( @@ -449,9 +469,28 @@ fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { } } } - if let ExprKind::Block( Block { stmts: [], expr: Some(Expr { kind: ExprKind::Unary(UnOp::UnNeg, else_expr), .. }), .. }, _,) = &body.kind + if let ExprKind::Block( + Block { + stmts: [], + expr: + Some(Expr { + kind: ExprKind::Unary(UnOp::UnNeg, else_expr), + .. + }), + .. + }, + _, + ) = &body.kind { - if let ExprKind::Block( Block { stmts: [], expr: Some(body), .. }, _,) = &else_body.kind { + if let ExprKind::Block( + Block { + stmts: [], + expr: Some(body), + .. + }, + _, + ) = &else_body.kind + { if are_exprs_equal(cx, else_expr, body) { if is_testing_negative(cx, cond, body) { span_lint_and_sugg( diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs index 40d2ff7e859..b0c15e57e40 100644 --- a/tests/ui/floating_point_abs.rs +++ b/tests/ui/floating_point_abs.rs @@ -2,7 +2,7 @@ struct A { a: f64, - b: f64 + b: f64, } fn fake_abs1(num: f64) -> f64 { @@ -62,11 +62,10 @@ fn fake_nabs2(num: f64) -> f64 { } fn fake_nabs3(a: A) -> A { - A { a: if a.a >= 0.0 { - -a.a - } else { - a.a - }, b: a.b } + A { + a: if a.a >= 0.0 { -a.a } else { a.a }, + b: a.b, + } } fn not_fake_abs1(num: f64) -> f64 { From ee739725378105db6f3d8593b12fde6998c9104c Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sat, 29 Feb 2020 19:11:38 -0800 Subject: [PATCH 05/11] Changed test output to reflect cargo fmt --- tests/ui/floating_point_abs.stderr | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/ui/floating_point_abs.stderr b/tests/ui/floating_point_abs.stderr index dd648a8a272..44a9dbee5bb 100644 --- a/tests/ui/floating_point_abs.stderr +++ b/tests/ui/floating_point_abs.stderr @@ -71,15 +71,10 @@ LL | | } | |_____^ help: try: `-num.abs()` error: This looks like you've implemented your own negative absolute value function - --> $DIR/floating_point_abs.rs:65:12 + --> $DIR/floating_point_abs.rs:66:12 | -LL | A { a: if a.a >= 0.0 { - | ____________^ -LL | | -a.a -LL | | } else { -LL | | a.a -LL | | }, b: a.b } - | |_________^ help: try: `-a.a.abs()` +LL | a: if a.a >= 0.0 { -a.a } else { a.a }, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-a.a.abs()` error: aborting due to 8 previous errors From 0a6d29940979aeb6bed98dcce67c4faa4e9df312 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sun, 1 Mar 2020 13:12:56 -0800 Subject: [PATCH 06/11] Fixed compile error from merging --- clippy_lints/src/floating_point_arithmetic.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index 6a6583f6610..b3b81ddb57c 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -11,11 +11,10 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; -use rustc_ast::ast; +use rustc_ast::ast::{self, FloatTy, LitFloatType, LitKind}; use std::f32::consts as f32_consts; use std::f64::consts as f64_consts; use sugg::{format_numeric_literal, Sugg}; -use syntax::ast::{self, FloatTy, LitFloatType, LitKind}; declare_clippy_lint! { /// **What it does:** Looks for floating-point expressions that From d88750371dcd333482037dd87cfaaddd1b301685 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sun, 1 Mar 2020 22:37:37 -0800 Subject: [PATCH 07/11] Refactor suggested by krishna-veerareddy --- clippy_lints/src/floating_point_arithmetic.rs | 149 ++++++++---------- 1 file changed, 65 insertions(+), 84 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index b3b81ddb57c..f2e6bd7da17 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -420,101 +420,82 @@ fn is_zero(expr: &Expr<'_>) -> bool { } } -fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { - if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { +/// If the expressions are not opposites, return None +/// Otherwise, return true if expr2 = -expr1, false if expr1 = -expr2 and return the positive +/// expression +fn are_opposites<'a>( + cx: &LateContext<'_, '_>, + expr1: &'a Expr<'a>, + expr2: &'a Expr<'a>, +) -> Option<(bool, &'a Expr<'a>)> { + if let ExprKind::Block( + Block { + stmts: [], + expr: Some(expr1_inner), + .. + }, + _, + ) = &expr1.kind + { if let ExprKind::Block( Block { stmts: [], - expr: - Some(Expr { - kind: ExprKind::Unary(UnOp::UnNeg, else_expr), - .. - }), + expr: Some(expr2_inner), .. }, _, - ) = else_body.kind + ) = &expr2.kind { - if let ExprKind::Block( - Block { - stmts: [], - expr: Some(body), - .. - }, - _, - ) = &body.kind - { - if are_exprs_equal(cx, else_expr, body) { - if is_testing_positive(cx, cond, body) { - span_lint_and_sugg( - cx, - SUBOPTIMAL_FLOPS, - expr.span, - "This looks like you've implemented your own absolute value function", - "try", - format!("{}.abs()", Sugg::hir(cx, body, "..")), - Applicability::MachineApplicable, - ); - } else if is_testing_negative(cx, cond, body) { - span_lint_and_sugg( - cx, - SUBOPTIMAL_FLOPS, - expr.span, - "This looks like you've implemented your own negative absolute value function", - "try", - format!("-{}.abs()", Sugg::hir(cx, body, "..")), - Applicability::MachineApplicable, - ); - } + if let ExprKind::Unary(UnOp::UnNeg, expr1_neg) = &expr1_inner.kind { + if are_exprs_equal(cx, expr1_neg, expr2_inner) { + return Some((false, expr2_inner)); + } + } + if let ExprKind::Unary(UnOp::UnNeg, expr2_neg) = &expr2_inner.kind { + if are_exprs_equal(cx, expr1_inner, expr2_neg) { + return Some((true, expr1_inner)); } } } - if let ExprKind::Block( - Block { - stmts: [], - expr: - Some(Expr { - kind: ExprKind::Unary(UnOp::UnNeg, else_expr), - .. - }), - .. - }, - _, - ) = &body.kind - { - if let ExprKind::Block( - Block { - stmts: [], - expr: Some(body), - .. - }, - _, - ) = &else_body.kind - { - if are_exprs_equal(cx, else_expr, body) { - if is_testing_negative(cx, cond, body) { - span_lint_and_sugg( - cx, - SUBOPTIMAL_FLOPS, - expr.span, - "This looks like you've implemented your own absolute value function", - "try", - format!("{}.abs()", Sugg::hir(cx, body, "..")), - Applicability::MachineApplicable, - ); - } else if is_testing_positive(cx, cond, body) { - span_lint_and_sugg( - cx, - SUBOPTIMAL_FLOPS, - expr.span, - "This looks like you've implemented your own negative absolute value function", - "try", - format!("-{}.abs()", Sugg::hir(cx, body, "..")), - Applicability::MachineApplicable, - ); - } + } + None +} + +fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { + if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { + if let Some((expr1_pos, body)) = are_opposites(cx, body, else_body) { + let pos_abs_sugg = ( + "This looks like you've implemented your own absolute value function", + format!("{}.abs()", Sugg::hir(cx, body, "..")), + ); + let neg_abs_sugg = ( + "This looks like you've implemented your own negative absolute value function", + format!("-{}.abs()", Sugg::hir(cx, body, "..")), + ); + let sugg = if is_testing_positive(cx, cond, body) { + if expr1_pos { + pos_abs_sugg + } else { + neg_abs_sugg } - } + } else if is_testing_negative(cx, cond, body) { + if expr1_pos { + neg_abs_sugg + } else { + pos_abs_sugg + } + } else { + return; + }; + span_lint_and_sugg( + cx, + SUBOPTIMAL_FLOPS, + expr.span, + sugg.0, + "try", + sugg.1, + Applicability::MachineApplicable, + ); } } } From 0d584f3ff760b14c8b23ae52f8ae7631305e43af Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sun, 1 Mar 2020 23:11:29 -0800 Subject: [PATCH 08/11] Fix one last test issue --- clippy_lints/src/floating_point_arithmetic.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index f2e6bd7da17..de2c245f451 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -76,12 +76,12 @@ declare_clippy_lint! { /// -a /// } else { /// a - /// } + /// }; /// let _ = if a < 0.0 { /// a /// } else { /// -a - /// } + /// }; /// ``` /// /// is better expressed as From f8e949fa1cc9af57f88babe74ea65e51730e7a86 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Tue, 3 Mar 2020 15:52:53 -0800 Subject: [PATCH 09/11] Recommended changes from flip1995 --- clippy_lints/src/floating_point_arithmetic.rs | 113 ++++++++---------- tests/ui/floating_point_abs.fixed | 98 +++++++++++++++ tests/ui/floating_point_abs.rs | 17 ++- tests/ui/floating_point_abs.stderr | 32 ++--- 4 files changed, 179 insertions(+), 81 deletions(-) create mode 100644 tests/ui/floating_point_abs.fixed diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index de2c245f451..6a58b27f065 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -1,17 +1,17 @@ use crate::consts::{ - constant, Constant, + constant, constant_simple, Constant, Constant::{F32, F64}, }; use crate::utils::{higher, span_lint_and_sugg, sugg, SpanlessEq}; use if_chain::if_chain; use rustc::ty; use rustc_errors::Applicability; -use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Lit, UnOp}; +use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; -use rustc_ast::ast::{self, FloatTy, LitFloatType, LitKind}; +use rustc_ast::ast; use std::f32::consts as f32_consts; use std::f64::consts as f64_consts; use sugg::{format_numeric_literal, Sugg}; @@ -378,8 +378,8 @@ fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool { if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind { match op { - BinOpKind::Gt | BinOpKind::Ge => is_zero(right) && are_exprs_equal(cx, left, test), - BinOpKind::Lt | BinOpKind::Le => is_zero(left) && are_exprs_equal(cx, right, test), + BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && are_exprs_equal(cx, left, test), + BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && are_exprs_equal(cx, right, test), _ => false, } } else { @@ -387,11 +387,12 @@ fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_ } } +/// See [`is_testing_positive`] fn is_testing_negative(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool { if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind { match op { - BinOpKind::Gt | BinOpKind::Ge => is_zero(left) && are_exprs_equal(cx, right, test), - BinOpKind::Lt | BinOpKind::Le => is_zero(right) && are_exprs_equal(cx, left, test), + BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && are_exprs_equal(cx, right, test), + BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && are_exprs_equal(cx, left, test), _ => false, } } else { @@ -404,85 +405,69 @@ fn are_exprs_equal(cx: &LateContext<'_, '_>, expr1: &Expr<'_>, expr2: &Expr<'_>) } /// Returns true iff expr is some zero literal -fn is_zero(expr: &Expr<'_>) -> bool { - if let ExprKind::Lit(Lit { node: lit, .. }) = &expr.kind { - match lit { - LitKind::Int(0, _) => true, - LitKind::Float(symb, LitFloatType::Unsuffixed) - | LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F64)) => { - symb.as_str().parse::().unwrap() == 0.0 - }, - LitKind::Float(symb, LitFloatType::Suffixed(FloatTy::F32)) => symb.as_str().parse::().unwrap() == 0.0, - _ => false, - } - } else { - false +fn is_zero(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { + match constant_simple(cx, cx.tables, expr) { + Some(Constant::Int(i)) => i == 0, + Some(Constant::F32(f)) => f == 0.0, + Some(Constant::F64(f)) => f == 0.0, + _ => false, } } -/// If the expressions are not opposites, return None -/// Otherwise, return true if expr2 = -expr1, false if expr1 = -expr2 and return the positive -/// expression -fn are_opposites<'a>( +/// If the two expressions are negations of each other, then it returns +/// a tuple, in which the first element is true iff expr1 is the +/// positive expressions, and the second element is the positive +/// one of the two expressions +/// If the two expressions are not negations of each other, then it +/// returns None. +fn are_negated<'a>( cx: &LateContext<'_, '_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>, ) -> Option<(bool, &'a Expr<'a>)> { - if let ExprKind::Block( - Block { - stmts: [], - expr: Some(expr1_inner), - .. - }, - _, - ) = &expr1.kind - { - if let ExprKind::Block( - Block { - stmts: [], - expr: Some(expr2_inner), - .. - }, - _, - ) = &expr2.kind - { - if let ExprKind::Unary(UnOp::UnNeg, expr1_neg) = &expr1_inner.kind { - if are_exprs_equal(cx, expr1_neg, expr2_inner) { - return Some((false, expr2_inner)); - } - } - if let ExprKind::Unary(UnOp::UnNeg, expr2_neg) = &expr2_inner.kind { - if are_exprs_equal(cx, expr1_inner, expr2_neg) { - return Some((true, expr1_inner)); - } - } + if let ExprKind::Unary(UnOp::UnNeg, expr1_negated) = &expr1.kind { + if are_exprs_equal(cx, expr1_negated, expr2) { + return Some((false, expr2)); + } + } + if let ExprKind::Unary(UnOp::UnNeg, expr2_negated) = &expr2.kind { + if are_exprs_equal(cx, expr1, expr2_negated) { + return Some((true, expr1)); } } None } fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { - if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { - if let Some((expr1_pos, body)) = are_opposites(cx, body, else_body) { - let pos_abs_sugg = ( - "This looks like you've implemented your own absolute value function", + if_chain! { + if let Some((cond, body, Some(else_body))) = higher::if_block(&expr); + if let ExprKind::Block(block, _) = body.kind; + if block.stmts.is_empty(); + if let Some(if_body_expr) = block.expr; + if let ExprKind::Block(else_block, _) = else_body.kind; + if else_block.stmts.is_empty(); + if let Some(else_body_expr) = else_block.expr; + if let Some((if_expr_positive, body)) = are_negated(cx, if_body_expr, else_body_expr); + then { + let positive_abs_sugg = ( + "manual implementation of `abs` method", format!("{}.abs()", Sugg::hir(cx, body, "..")), ); - let neg_abs_sugg = ( - "This looks like you've implemented your own negative absolute value function", + let negative_abs_sugg = ( + "manual implementation of negation of `abs` method", format!("-{}.abs()", Sugg::hir(cx, body, "..")), ); let sugg = if is_testing_positive(cx, cond, body) { - if expr1_pos { - pos_abs_sugg + if if_expr_positive { + positive_abs_sugg } else { - neg_abs_sugg + negative_abs_sugg } } else if is_testing_negative(cx, cond, body) { - if expr1_pos { - neg_abs_sugg + if if_expr_positive { + negative_abs_sugg } else { - pos_abs_sugg + positive_abs_sugg } } else { return; diff --git a/tests/ui/floating_point_abs.fixed b/tests/ui/floating_point_abs.fixed new file mode 100644 index 00000000000..39cec0727a1 --- /dev/null +++ b/tests/ui/floating_point_abs.fixed @@ -0,0 +1,98 @@ +// run-rustfix +#![warn(clippy::suboptimal_flops)] + +struct A { + a: f64, + b: f64, +} + +fn fake_abs1(num: f64) -> f64 { + num.abs() +} + +fn fake_abs2(num: f64) -> f64 { + num.abs() +} + +fn fake_abs3(a: A) -> f64 { + a.a.abs() +} + +fn fake_abs4(num: f64) -> f64 { + num.abs() +} + +fn fake_abs5(a: A) -> f64 { + a.a.abs() +} + +fn fake_nabs1(num: f64) -> f64 { + -num.abs() +} + +fn fake_nabs2(num: f64) -> f64 { + -num.abs() +} + +fn fake_nabs3(a: A) -> A { + A { + a: -a.a.abs(), + b: a.b, + } +} + +fn not_fake_abs1(num: f64) -> f64 { + if num > 0.0 { + num + } else { + -num - 1f64 + } +} + +fn not_fake_abs2(num: f64) -> f64 { + if num > 0.0 { + num + 1.0 + } else { + -(num + 1.0) + } +} + +fn not_fake_abs3(num1: f64, num2: f64) -> f64 { + if num1 > 0.0 { + num2 + } else { + -num2 + } +} + +fn not_fake_abs4(a: A) -> f64 { + if a.a > 0.0 { + a.b + } else { + -a.b + } +} + +fn not_fake_abs5(a: A) -> f64 { + if a.a > 0.0 { + a.a + } else { + -a.b + } +} + +fn main() { + fake_abs1(5.0); + fake_abs2(5.0); + fake_abs3(A { a: 5.0, b: 5.0 } ); + fake_abs4(5.0); + fake_abs5(A { a: 5.0, b: 5.0 } ); + fake_nabs1(5.0); + fake_nabs2(5.0); + fake_nabs3(A { a: 5.0, b: 5.0 } ); + not_fake_abs1(5.0); + not_fake_abs2(5.0); + not_fake_abs3(5.0, 5.0); + not_fake_abs4(A { a: 5.0, b: 5.0 } ); + not_fake_abs5(A { a: 5.0, b: 5.0 } ); +} diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs index b0c15e57e40..780eb354715 100644 --- a/tests/ui/floating_point_abs.rs +++ b/tests/ui/floating_point_abs.rs @@ -1,3 +1,4 @@ +// run-rustfix #![warn(clippy::suboptimal_flops)] struct A { @@ -108,4 +109,18 @@ fn not_fake_abs5(a: A) -> f64 { } } -fn main() {} +fn main() { + fake_abs1(5.0); + fake_abs2(5.0); + fake_abs3(A { a: 5.0, b: 5.0 } ); + fake_abs4(5.0); + fake_abs5(A { a: 5.0, b: 5.0 } ); + fake_nabs1(5.0); + fake_nabs2(5.0); + fake_nabs3(A { a: 5.0, b: 5.0 } ); + not_fake_abs1(5.0); + not_fake_abs2(5.0); + not_fake_abs3(5.0, 5.0); + not_fake_abs4(A { a: 5.0, b: 5.0 } ); + not_fake_abs5(A { a: 5.0, b: 5.0 } ); +} diff --git a/tests/ui/floating_point_abs.stderr b/tests/ui/floating_point_abs.stderr index 44a9dbee5bb..74a71f2ca7c 100644 --- a/tests/ui/floating_point_abs.stderr +++ b/tests/ui/floating_point_abs.stderr @@ -1,5 +1,5 @@ -error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:9:5 +error: manual implementation of `abs` method + --> $DIR/floating_point_abs.rs:10:5 | LL | / if num >= 0.0 { LL | | num @@ -10,8 +10,8 @@ LL | | } | = note: `-D clippy::suboptimal-flops` implied by `-D warnings` -error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:17:5 +error: manual implementation of `abs` method + --> $DIR/floating_point_abs.rs:18:5 | LL | / if 0.0 < num { LL | | num @@ -20,8 +20,8 @@ LL | | -num LL | | } | |_____^ help: try: `num.abs()` -error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:25:5 +error: manual implementation of `abs` method + --> $DIR/floating_point_abs.rs:26:5 | LL | / if a.a > 0.0 { LL | | a.a @@ -30,8 +30,8 @@ LL | | -a.a LL | | } | |_____^ help: try: `a.a.abs()` -error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:33:5 +error: manual implementation of `abs` method + --> $DIR/floating_point_abs.rs:34:5 | LL | / if 0.0 >= num { LL | | -num @@ -40,8 +40,8 @@ LL | | num LL | | } | |_____^ help: try: `num.abs()` -error: This looks like you've implemented your own absolute value function - --> $DIR/floating_point_abs.rs:41:5 +error: manual implementation of `abs` method + --> $DIR/floating_point_abs.rs:42:5 | LL | / if a.a < 0.0 { LL | | -a.a @@ -50,8 +50,8 @@ LL | | a.a LL | | } | |_____^ help: try: `a.a.abs()` -error: This looks like you've implemented your own negative absolute value function - --> $DIR/floating_point_abs.rs:49:5 +error: manual implementation of negation of `abs` method + --> $DIR/floating_point_abs.rs:50:5 | LL | / if num < 0.0 { LL | | num @@ -60,8 +60,8 @@ LL | | -num LL | | } | |_____^ help: try: `-num.abs()` -error: This looks like you've implemented your own negative absolute value function - --> $DIR/floating_point_abs.rs:57:5 +error: manual implementation of negation of `abs` method + --> $DIR/floating_point_abs.rs:58:5 | LL | / if 0.0 >= num { LL | | num @@ -70,8 +70,8 @@ LL | | -num LL | | } | |_____^ help: try: `-num.abs()` -error: This looks like you've implemented your own negative absolute value function - --> $DIR/floating_point_abs.rs:66:12 +error: manual implementation of negation of `abs` method + --> $DIR/floating_point_abs.rs:67:12 | LL | a: if a.a >= 0.0 { -a.a } else { a.a }, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-a.a.abs()` From fe342f32912b3e5a6b48e9e99615319d8c13bd2b Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Tue, 3 Mar 2020 18:06:59 -0800 Subject: [PATCH 10/11] Ran cargo dev fmt --- clippy_lints/src/floating_point_arithmetic.rs | 6 +----- tests/ui/floating_point_abs.rs | 10 +++++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index 6a58b27f065..0fcfb7b8849 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -420,11 +420,7 @@ fn is_zero(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { /// one of the two expressions /// If the two expressions are not negations of each other, then it /// returns None. -fn are_negated<'a>( - cx: &LateContext<'_, '_>, - expr1: &'a Expr<'a>, - expr2: &'a Expr<'a>, -) -> Option<(bool, &'a Expr<'a>)> { +fn are_negated<'a>(cx: &LateContext<'_, '_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)> { if let ExprKind::Unary(UnOp::UnNeg, expr1_negated) = &expr1.kind { if are_exprs_equal(cx, expr1_negated, expr2) { return Some((false, expr2)); diff --git a/tests/ui/floating_point_abs.rs b/tests/ui/floating_point_abs.rs index 780eb354715..cbf9c94e41e 100644 --- a/tests/ui/floating_point_abs.rs +++ b/tests/ui/floating_point_abs.rs @@ -112,15 +112,15 @@ fn not_fake_abs5(a: A) -> f64 { fn main() { fake_abs1(5.0); fake_abs2(5.0); - fake_abs3(A { a: 5.0, b: 5.0 } ); + fake_abs3(A { a: 5.0, b: 5.0 }); fake_abs4(5.0); - fake_abs5(A { a: 5.0, b: 5.0 } ); + fake_abs5(A { a: 5.0, b: 5.0 }); fake_nabs1(5.0); fake_nabs2(5.0); - fake_nabs3(A { a: 5.0, b: 5.0 } ); + fake_nabs3(A { a: 5.0, b: 5.0 }); not_fake_abs1(5.0); not_fake_abs2(5.0); not_fake_abs3(5.0, 5.0); - not_fake_abs4(A { a: 5.0, b: 5.0 } ); - not_fake_abs5(A { a: 5.0, b: 5.0 } ); + not_fake_abs4(A { a: 5.0, b: 5.0 }); + not_fake_abs5(A { a: 5.0, b: 5.0 }); } From c3e96d14ab1b90b7e4ff1ea2222419a22c011ed5 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Tue, 3 Mar 2020 19:23:04 -0800 Subject: [PATCH 11/11] Update test case answers to match cargo dev fmt --- tests/ui/floating_point_abs.fixed | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ui/floating_point_abs.fixed b/tests/ui/floating_point_abs.fixed index 39cec0727a1..b623e4988e7 100644 --- a/tests/ui/floating_point_abs.fixed +++ b/tests/ui/floating_point_abs.fixed @@ -84,15 +84,15 @@ fn not_fake_abs5(a: A) -> f64 { fn main() { fake_abs1(5.0); fake_abs2(5.0); - fake_abs3(A { a: 5.0, b: 5.0 } ); + fake_abs3(A { a: 5.0, b: 5.0 }); fake_abs4(5.0); - fake_abs5(A { a: 5.0, b: 5.0 } ); + fake_abs5(A { a: 5.0, b: 5.0 }); fake_nabs1(5.0); fake_nabs2(5.0); - fake_nabs3(A { a: 5.0, b: 5.0 } ); + fake_nabs3(A { a: 5.0, b: 5.0 }); not_fake_abs1(5.0); not_fake_abs2(5.0); not_fake_abs3(5.0, 5.0); - not_fake_abs4(A { a: 5.0, b: 5.0 } ); - not_fake_abs5(A { a: 5.0, b: 5.0 } ); + not_fake_abs4(A { a: 5.0, b: 5.0 }); + not_fake_abs5(A { a: 5.0, b: 5.0 }); }