Skip to content

Commit 5a21661

Browse files
committed
Some bugfixing
1 parent ce0dc9b commit 5a21661

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils::{higher, span_lint_and_sugg, sugg, SpanlessEq};
66
use if_chain::if_chain;
77
use rustc::ty;
88
use rustc_errors::Applicability;
9-
use rustc_hir::{BinOpKind, Expr, ExprKind, Lit, UnOp};
9+
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Lit, UnOp};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::source_map::Spanned;
@@ -409,22 +409,46 @@ fn is_zero(expr: &Expr<'_>) -> bool {
409409
}
410410

411411
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
412-
// if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
413-
// Check for the positive-first variant
414-
// if let ExprKind::Unary(UnOp::UnNeg, expr) = else_body.kind {
415-
// if are_exprs_equal(cx, expr, body) && is_testing_positive(cx, cond, body) {
416-
span_lint_and_sugg(
417-
cx,
418-
SUBOPTIMAL_FLOPS,
419-
expr.span,
420-
"This looks like you've implemented your own absolute value function",
421-
"try",
422-
"a.abs()".to_string(),//format!("{:?}.abs()", body),
423-
Applicability::MachineApplicable,
424-
);
425-
// }
426-
// }
427-
// }
412+
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
413+
if let ExprKind::Block(
414+
Block {
415+
stmts: [],
416+
expr:
417+
Some(Expr {
418+
kind: ExprKind::Unary(UnOp::UnNeg, else_expr),
419+
..
420+
}),
421+
..
422+
},
423+
_,
424+
) = else_body.kind
425+
{
426+
if let ExprKind::Block(
427+
Block {
428+
stmts: [],
429+
expr: Some(body),
430+
..
431+
},
432+
_,
433+
) = &body.kind
434+
{
435+
if are_exprs_equal(cx, else_expr, body) {
436+
dbg!("if (cond) body else -body\nbody: {:?}", &body.kind);
437+
if is_testing_positive(cx, cond, body) {
438+
span_lint_and_sugg(
439+
cx,
440+
SUBOPTIMAL_FLOPS,
441+
expr.span,
442+
"This looks like you've implemented your own absolute value function",
443+
"try",
444+
format!("{}.abs()", Sugg::hir(cx, body, "..")),
445+
Applicability::MachineApplicable,
446+
);
447+
}
448+
}
449+
}
450+
}
451+
}
428452
}
429453

430454
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {

tests/ui/floating_point_abs.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#[warn(clippy::suboptimal_flops)]
2+
struct A {
3+
a: f64,
4+
b: f64
5+
}
26

37
fn fake_abs1(num: f64) -> f64 {
48
if num >= 0.0 {
@@ -16,6 +20,14 @@ fn fake_abs2(num: f64) -> f64 {
1620
}
1721
}
1822

23+
fn fake_abs3(a: A) -> f64 {
24+
if a.a > 0.0 {
25+
a.a
26+
} else {
27+
-a.a
28+
}
29+
}
30+
1931
fn fake_nabs1(num: f64) -> f64 {
2032
if num < 0.0 {
2133
num
@@ -32,6 +44,14 @@ fn fake_nabs2(num: f64) -> f64 {
3244
}
3345
}
3446

47+
fn fake_nabs3(a: A) -> A {
48+
A { a: if a.a >= 0.0 {
49+
a.a
50+
} else {
51+
-a.a
52+
}, b: a.b }
53+
}
54+
3555
fn not_fake_abs1(num: f64) -> f64 {
3656
if num > 0.0 {
3757
num
@@ -56,4 +76,20 @@ fn not_fake_abs3(num1: f64, num2: f64) -> f64 {
5676
}
5777
}
5878

79+
fn not_fake_abs4(a: A) -> f64 {
80+
if a.a > 0.0 {
81+
a.b
82+
} else {
83+
-a.b
84+
}
85+
}
86+
87+
fn not_fake_abs5(a: A) -> f64 {
88+
if a.a > 0.0 {
89+
a.a
90+
} else {
91+
-a.b
92+
}
93+
}
94+
5995
fn main() {}

tests/ui/floating_point_abs.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: This looks like you've implemented your own absolute value function
2+
--> $DIR/floating_point_abs.rs:4:5
3+
|
4+
LL | / if num >= 0.0 {
5+
LL | | num
6+
LL | | } else {
7+
LL | | -num
8+
LL | | }
9+
| |_____^ help: try: `num.abs()`
10+
|
11+
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)