Skip to content

Commit 2d85314

Browse files
committed
Changed the location of the suggestion as well as the way the suggestion is assembled
1 parent 179df0b commit 2d85314

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

clippy_lints/src/float_equality_without_abs.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
1+
use crate::utils::{match_qpath, paths, span_lint_and_then, sugg};
22
use if_chain::if_chain;
3+
use rustc_ast::util::parser::AssocOp;
34
use rustc_errors::Applicability;
45
use rustc_hir::{BinOpKind, Expr, ExprKind};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -84,27 +85,24 @@ impl<'tcx> LateLintPass<'tcx> for FloatEqualityWithoutAbs {
8485
if let ty::Float(_) = t_val_r.kind;
8586

8687
then {
87-
// get the snippet string
88-
let lhs_string = snippet(
89-
cx,
90-
lhs.span,
91-
"(...)",
92-
);
88+
let sug_l = sugg::Sugg::hir(cx, &val_l, "..");
89+
let sug_r = sugg::Sugg::hir(cx, &val_r, "..");
9390
// format the suggestion
94-
let suggestion = if lhs_string.starts_with('(') {
95-
format!("{}.abs()", lhs_string)
96-
} else {
97-
format!("({}).abs()", lhs_string)
98-
};
91+
let suggestion = format!("{}.abs()", sugg::make_assoc(AssocOp::Subtract, &sug_l, &sug_r).maybe_par());
9992
// spans the lint
100-
span_lint_and_sugg(
93+
span_lint_and_then(
10194
cx,
10295
FLOAT_EQUALITY_WITHOUT_ABS,
10396
expr.span,
10497
"float equality check without `.abs()`",
105-
"add `.abs()`",
106-
suggestion,
107-
Applicability::MaybeIncorrect,
98+
| diag | {
99+
diag.span_suggestion(
100+
lhs.span,
101+
"add `.abs()`",
102+
suggestion,
103+
Applicability::MaybeIncorrect,
104+
);
105+
}
108106
);
109107
}
110108
}

tests/ui/float_equality_without_abs.stderr

+33-11
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,91 @@ error: float equality check without `.abs()`
22
--> $DIR/float_equality_without_abs.rs:4:5
33
|
44
LL | (a - b) < f32::EPSILON
5-
| ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
5+
| -------^^^^^^^^^^^^^^^
6+
| |
7+
| help: add `.abs()`: `(a - b).abs()`
68
|
79
= note: `-D clippy::float-equality-without-abs` implied by `-D warnings`
810

911
error: float equality check without `.abs()`
1012
--> $DIR/float_equality_without_abs.rs:13:13
1113
|
1214
LL | let _ = (a - b) < f32::EPSILON;
13-
| ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
15+
| -------^^^^^^^^^^^^^^^
16+
| |
17+
| help: add `.abs()`: `(a - b).abs()`
1418

1519
error: float equality check without `.abs()`
1620
--> $DIR/float_equality_without_abs.rs:14:13
1721
|
1822
LL | let _ = a - b < f32::EPSILON;
19-
| ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
23+
| -----^^^^^^^^^^^^^^^
24+
| |
25+
| help: add `.abs()`: `(a - b).abs()`
2026

2127
error: float equality check without `.abs()`
2228
--> $DIR/float_equality_without_abs.rs:15:13
2329
|
2430
LL | let _ = a - b.abs() < f32::EPSILON;
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b.abs()).abs()`
31+
| -----------^^^^^^^^^^^^^^^
32+
| |
33+
| help: add `.abs()`: `(a - b.abs()).abs()`
2634

2735
error: float equality check without `.abs()`
2836
--> $DIR/float_equality_without_abs.rs:16:13
2937
|
3038
LL | let _ = (a as f64 - b as f64) < f64::EPSILON;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a as f64 - b as f64).abs()`
39+
| ---------------------^^^^^^^^^^^^^^^
40+
| |
41+
| help: add `.abs()`: `(a as f64 - b as f64).abs()`
3242

3343
error: float equality check without `.abs()`
3444
--> $DIR/float_equality_without_abs.rs:17:13
3545
|
3646
LL | let _ = 1.0 - 2.0 < f32::EPSILON;
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(1.0 - 2.0).abs()`
47+
| ---------^^^^^^^^^^^^^^^
48+
| |
49+
| help: add `.abs()`: `(1.0 - 2.0).abs()`
3850

3951
error: float equality check without `.abs()`
4052
--> $DIR/float_equality_without_abs.rs:19:13
4153
|
4254
LL | let _ = f32::EPSILON > (a - b);
43-
| ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
55+
| ^^^^^^^^^^^^^^^-------
56+
| |
57+
| help: add `.abs()`: `(a - b).abs()`
4458

4559
error: float equality check without `.abs()`
4660
--> $DIR/float_equality_without_abs.rs:20:13
4761
|
4862
LL | let _ = f32::EPSILON > a - b;
49-
| ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
63+
| ^^^^^^^^^^^^^^^-----
64+
| |
65+
| help: add `.abs()`: `(a - b).abs()`
5066

5167
error: float equality check without `.abs()`
5268
--> $DIR/float_equality_without_abs.rs:21:13
5369
|
5470
LL | let _ = f32::EPSILON > a - b.abs();
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b.abs()).abs()`
71+
| ^^^^^^^^^^^^^^^-----------
72+
| |
73+
| help: add `.abs()`: `(a - b.abs()).abs()`
5674

5775
error: float equality check without `.abs()`
5876
--> $DIR/float_equality_without_abs.rs:22:13
5977
|
6078
LL | let _ = f64::EPSILON > (a as f64 - b as f64);
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a as f64 - b as f64).abs()`
79+
| ^^^^^^^^^^^^^^^---------------------
80+
| |
81+
| help: add `.abs()`: `(a as f64 - b as f64).abs()`
6282

6383
error: float equality check without `.abs()`
6484
--> $DIR/float_equality_without_abs.rs:23:13
6585
|
6686
LL | let _ = f32::EPSILON > 1.0 - 2.0;
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(1.0 - 2.0).abs()`
87+
| ^^^^^^^^^^^^^^^---------
88+
| |
89+
| help: add `.abs()`: `(1.0 - 2.0).abs()`
6890

6991
error: aborting due to 11 previous errors
7092

0 commit comments

Comments
 (0)