-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Reduce clippy's complaints about valid decimals #7691
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
Closed
Closed
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,112 +1,76 @@ | ||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:15:26 | ||
| | ||
LL | const BAD32_1: f32 = 0.123_456_789_f32; | ||
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79_f32` | ||
| | ||
= note: `-D clippy::excessive-precision` implied by `-D warnings` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:16:26 | ||
| | ||
LL | const BAD32_2: f32 = 0.123_456_789; | ||
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:17:26 | ||
| | ||
LL | const BAD32_3: f32 = 0.100_000_000_000_1; | ||
LL | const BAD32_1: f32 = 0.100_000_000_000_1; | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:18:29 | ||
| | ||
LL | const BAD32_EDGE: f32 = 1.000_000_9; | ||
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001` | ||
= note: `-D clippy::excessive-precision` implied by `-D warnings` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:20:26 | ||
--> $DIR/excessive_precision.rs:17:26 | ||
| | ||
LL | const BAD64_1: f64 = 0.123_456_789_012_345_67f64; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64` | ||
LL | const BAD32_2: f32 = 0.123_456_789_012; | ||
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:21:26 | ||
--> $DIR/excessive_precision.rs:25:26 | ||
| | ||
LL | const BAD64_2: f64 = 0.123_456_789_012_345_67; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66` | ||
LL | const BAD64_1: f64 = 0.100_000_000_000_000_000_1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:22:26 | ||
--> $DIR/excessive_precision.rs:26:26 | ||
| | ||
LL | const BAD64_3: f64 = 0.100_000_000_000_000_000_1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1` | ||
LL | const BAD64_2: f64 = 0.123_456_789_012_345_678_9; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_68` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:25:22 | ||
--> $DIR/excessive_precision.rs:29:22 | ||
| | ||
LL | println!("{:?}", 8.888_888_888_888_888_888_888); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:36:22 | ||
--> $DIR/excessive_precision.rs:40:22 | ||
| | ||
LL | let bad32: f32 = 1.123_456_789; | ||
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:37:26 | ||
--> $DIR/excessive_precision.rs:41:26 | ||
| | ||
LL | let bad32_suf: f32 = 1.123_456_789_f32; | ||
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:38:21 | ||
--> $DIR/excessive_precision.rs:42:21 | ||
| | ||
LL | let bad32_inf = 1.123_456_789_f32; | ||
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:40:22 | ||
| | ||
LL | let bad64: f64 = 0.123_456_789_012_345_67; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:41:26 | ||
| | ||
LL | let bad64_suf: f64 = 0.123_456_789_012_345_67f64; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64` | ||
Comment on lines
-75
to
-79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cases like this are fixed a different way by #7722. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. That change does make me considerably happier with the lint. |
||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:42:21 | ||
| | ||
LL | let bad64_inf = 0.123_456_789_012_345_67; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:48:36 | ||
--> $DIR/excessive_precision.rs:50:36 | ||
| | ||
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789]; | ||
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79` | ||
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789_012]; | ||
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:49:36 | ||
--> $DIR/excessive_precision.rs:53:36 | ||
| | ||
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_789]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_123_456_78` | ||
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_012_345_678_9]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_68` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:53:24 | ||
--> $DIR/excessive_precision.rs:57:24 | ||
| | ||
LL | let bad_e32: f32 = 1.123_456_788_888e-10; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8e-10` | ||
|
||
error: float has excessive precision | ||
--> $DIR/excessive_precision.rs:56:27 | ||
--> $DIR/excessive_precision.rs:60:27 | ||
| | ||
LL | let bad_bige32: f32 = 1.123_456_788_888E-10; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10` | ||
|
||
error: aborting due to 18 previous errors | ||
error: aborting due to 12 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 don't think this change is faithful to the purpose of this lint. Why would we not suggest replacing
0.123_456_789
with0.123_456_79
, which eliminates the extraneous precision and still represents the identical floating point quantity.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.
Hmm, I have been wondering about that a little to myself, on reading the code at first I was kind of stunned and confused by the naming of the vars and fn so I didn't examine it closely enough to find all the hidden catches. So let me explain my reasoning more thoroughly. Part of it is somewhat esoteric and may be a better case for recommending this be altered in category, from a style lint to a restriction lint.
It is actually considered "good style" in many situations to use 9 or 17 significant digits. It is necessary for encoding the longer sequences, and faithful replication of the binary value with those 9 or 17 digits is what IEEE754 guarantees. This also matters for, well, the reason that I was alarmed at just using the constant directly: it is common for the character strings of numbers to be blithely copied from language to language. Indeed, most of Rust's float-related constants seem to be lifted from C's
<float.h>
.While most language's
atoi
or equivalent is fairly faithful, some languages have less correct float parsers than Rust's, especially at the edges. So it is possible to introduce floating point error due to variations in parsing, rounding, and formatting between compilers and runtimes. As a general guard against this problem, "over" specifying by a few chars is common, or at least using all 9 or 17 digits. For instance, consider the string values used in Boost, Rust, and OpenCL for pi:Thus, I suppose my disagreement is with the lint per se, or at least what it currently encourages and allows to be machine-applied, since it would likely be preferable to upgrade the type to f64 for many f32 triggers of this lint.