Skip to content

Commit 0695f21

Browse files
committed
Auto merge of rust-lang#6043 - HaramanJohal:margin_of_error, r=matthiaskrgr
clarify margin of error in wording of float comparison operator lint messages fixes rust-lang#6040 changelog: change wording of float comparison operator to make margin of error less ambiguous
2 parents 190c6ea + 16b6ceb commit 0695f21

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

clippy_lints/src/misc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ declare_clippy_lint! {
9999
/// if y != x {} // where both are floats
100100
///
101101
/// // Good
102-
/// let error = f64::EPSILON; // Use an epsilon for comparison
102+
/// let error_margin = f64::EPSILON; // Use an epsilon for comparison
103103
/// // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead.
104-
/// // let error = std::f64::EPSILON;
105-
/// if (y - 1.23f64).abs() < error { }
106-
/// if (y - x).abs() > error { }
104+
/// // let error_margin = std::f64::EPSILON;
105+
/// if (y - 1.23f64).abs() < error_margin { }
106+
/// if (y - x).abs() > error_margin { }
107107
/// ```
108108
pub FLOAT_CMP,
109109
correctness,
@@ -242,10 +242,10 @@ declare_clippy_lint! {
242242
/// if x == ONE { } // where both are floats
243243
///
244244
/// // Good
245-
/// let error = f64::EPSILON; // Use an epsilon for comparison
245+
/// let error_margin = f64::EPSILON; // Use an epsilon for comparison
246246
/// // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead.
247-
/// // let error = std::f64::EPSILON;
248-
/// if (x - ONE).abs() < error { }
247+
/// // let error_margin = std::f64::EPSILON;
248+
/// if (x - ONE).abs() < error_margin { }
249249
/// ```
250250
pub FLOAT_CMP_CONST,
251251
restriction,
@@ -411,16 +411,16 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
411411
if !is_comparing_arrays {
412412
diag.span_suggestion(
413413
expr.span,
414-
"consider comparing them within some error",
414+
"consider comparing them within some margin of error",
415415
format!(
416-
"({}).abs() {} error",
416+
"({}).abs() {} error_margin",
417417
lhs - rhs,
418418
if op == BinOpKind::Eq { '<' } else { '>' }
419419
),
420420
Applicability::HasPlaceholders, // snippet
421421
);
422422
}
423-
diag.note("`f32::EPSILON` and `f64::EPSILON` are available for the `error`");
423+
diag.note("`f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`");
424424
});
425425
} else if op == BinOpKind::Rem && is_integer_const(cx, right, 1) {
426426
span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");

tests/ui/float_cmp.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@ error: strict comparison of `f32` or `f64`
22
--> $DIR/float_cmp.rs:65:5
33
|
44
LL | ONE as f64 != 2.0;
5-
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() > error`
5+
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f64 - 2.0).abs() > error_margin`
66
|
77
= note: `-D clippy::float-cmp` implied by `-D warnings`
8-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
8+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
99

1010
error: strict comparison of `f32` or `f64`
1111
--> $DIR/float_cmp.rs:70:5
1212
|
1313
LL | x == 1.0;
14-
| ^^^^^^^^ help: consider comparing them within some error: `(x - 1.0).abs() < error`
14+
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin`
1515
|
16-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
16+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
1717

1818
error: strict comparison of `f32` or `f64`
1919
--> $DIR/float_cmp.rs:73:5
2020
|
2121
LL | twice(x) != twice(ONE as f64);
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() > error`
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(twice(x) - twice(ONE as f64)).abs() > error_margin`
2323
|
24-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
24+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
2525

2626
error: strict comparison of `f32` or `f64`
2727
--> $DIR/float_cmp.rs:93:5
2828
|
2929
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error`
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error_margin`
3131
|
32-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
32+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
3333

3434
error: strict comparison of `f32` or `f64` arrays
3535
--> $DIR/float_cmp.rs:98:5
3636
|
3737
LL | a1 == a2;
3838
| ^^^^^^^^
3939
|
40-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
40+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
4141

4242
error: strict comparison of `f32` or `f64`
4343
--> $DIR/float_cmp.rs:99:5
4444
|
4545
LL | a1[0] == a2[0];
46-
| ^^^^^^^^^^^^^^ help: consider comparing them within some error: `(a1[0] - a2[0]).abs() < error`
46+
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`
4747
|
48-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
48+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
4949

5050
error: aborting due to 6 previous errors
5151

tests/ui/float_cmp_const.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,66 @@ error: strict comparison of `f32` or `f64` constant
22
--> $DIR/float_cmp_const.rs:20:5
33
|
44
LL | 1f32 == ONE;
5-
| ^^^^^^^^^^^ help: consider comparing them within some error: `(1f32 - ONE).abs() < error`
5+
| ^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1f32 - ONE).abs() < error_margin`
66
|
77
= note: `-D clippy::float-cmp-const` implied by `-D warnings`
8-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
8+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
99

1010
error: strict comparison of `f32` or `f64` constant
1111
--> $DIR/float_cmp_const.rs:21:5
1212
|
1313
LL | TWO == ONE;
14-
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() < error`
14+
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() < error_margin`
1515
|
16-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
16+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
1717

1818
error: strict comparison of `f32` or `f64` constant
1919
--> $DIR/float_cmp_const.rs:22:5
2020
|
2121
LL | TWO != ONE;
22-
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() > error`
22+
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() > error_margin`
2323
|
24-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
24+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
2525

2626
error: strict comparison of `f32` or `f64` constant
2727
--> $DIR/float_cmp_const.rs:23:5
2828
|
2929
LL | ONE + ONE == TWO;
30-
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE + ONE - TWO).abs() < error`
30+
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE + ONE - TWO).abs() < error_margin`
3131
|
32-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
32+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
3333

3434
error: strict comparison of `f32` or `f64` constant
3535
--> $DIR/float_cmp_const.rs:25:5
3636
|
3737
LL | x as f32 == ONE;
38-
| ^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(x as f32 - ONE).abs() < error`
38+
| ^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x as f32 - ONE).abs() < error_margin`
3939
|
40-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
40+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
4141

4242
error: strict comparison of `f32` or `f64` constant
4343
--> $DIR/float_cmp_const.rs:28:5
4444
|
4545
LL | v == ONE;
46-
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() < error`
46+
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() < error_margin`
4747
|
48-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
48+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
4949

5050
error: strict comparison of `f32` or `f64` constant
5151
--> $DIR/float_cmp_const.rs:29:5
5252
|
5353
LL | v != ONE;
54-
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() > error`
54+
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() > error_margin`
5555
|
56-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
56+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
5757

5858
error: strict comparison of `f32` or `f64` constant arrays
5959
--> $DIR/float_cmp_const.rs:61:5
6060
|
6161
LL | NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6363
|
64-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error`
64+
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
6565

6666
error: aborting due to 8 previous errors
6767

0 commit comments

Comments
 (0)