Skip to content

Stop suggesting a float truncation that is not shorter #7722

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

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/float_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
Applicability::MachineApplicable,
);
}
} else if digits > max as usize && sym_str != float_str {
} else if digits > max as usize && float_str.len() < sym_str.len() {
span_lint_and_sugg(
cx,
EXCESSIVE_PRECISION,
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/excessive_precision.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn main() {
const BAD32_3: f32 = 0.1;
const BAD32_EDGE: f32 = 1.000_001;

const BAD64_1: f64 = 0.123_456_789_012_345_66_f64;
const BAD64_2: f64 = 0.123_456_789_012_345_66;
const BAD64_1: f64 = 0.123_456_789_012_345_67f64;
const BAD64_2: f64 = 0.123_456_789_012_345_67;
const BAD64_3: f64 = 0.1;

// Literal as param
Expand All @@ -37,9 +37,9 @@ fn main() {
let bad32_suf: f32 = 1.123_456_8_f32;
let bad32_inf = 1.123_456_8_f32;

let bad64: f64 = 0.123_456_789_012_345_66;
let bad64_suf: f64 = 0.123_456_789_012_345_66_f64;
let bad64_inf = 0.123_456_789_012_345_66;
let bad64: f64 = 0.123_456_789_012_345_67;
let bad64_suf: f64 = 0.123_456_789_012_345_67f64;
let bad64_inf = 0.123_456_789_012_345_67;

// Vectors
let good_vec32: Vec<f32> = vec![0.123_456];
Expand Down
32 changes: 1 addition & 31 deletions tests/ui/excessive_precision.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ error: float has excessive precision
LL | const BAD32_EDGE: f32 = 1.000_000_9;
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001`

error: float has excessive precision
--> $DIR/excessive_precision.rs:20: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`

error: float has excessive precision
--> $DIR/excessive_precision.rs:21: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`

error: float has excessive precision
--> $DIR/excessive_precision.rs:22:26
|
Expand Down Expand Up @@ -66,24 +54,6 @@ error: float has excessive precision
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`

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
|
Expand All @@ -108,5 +78,5 @@ error: float has excessive precision
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 13 previous errors