Skip to content

Don't lint excessive_precision on inf #10952

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
Jun 20, 2023
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
11 changes: 8 additions & 3 deletions clippy_lints/src/float_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,24 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
LitFloatType::Unsuffixed => None
};
let (is_whole, mut float_str) = match fty {
let (is_whole, is_inf, mut float_str) = match fty {
FloatTy::F32 => {
let value = sym_str.parse::<f32>().unwrap();

(value.fract() == 0.0, formatter.format(value))
(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
},
FloatTy::F64 => {
let value = sym_str.parse::<f64>().unwrap();

(value.fract() == 0.0, formatter.format(value))

(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
},
};

if is_inf {
return;
}

if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
// Normalize the literal by stripping the fractional portion
if sym_str.split('.').next().unwrap() != float_str {
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/excessive_precision.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//@run-rustfix
#![warn(clippy::excessive_precision)]
#![allow(dead_code, unused_variables, clippy::print_literal, clippy::useless_vec)]
#![allow(
dead_code,
overflowing_literals,
unused_variables,
clippy::print_literal,
clippy::useless_vec
)]

fn main() {
// Consts
Expand Down Expand Up @@ -66,4 +72,11 @@ fn main() {

// issue #7745
let _ = 0_f64;

// issue #9910
const INF1: f32 = 1.0e+33f32;
const INF2: f64 = 1.0e+3300f64;
const NEG_INF1: f32 = -1.0e+33f32;
const NEG_INF2: f64 = -1.0e+3300f64;
const NEG_INF3: f32 = -3.40282357e+38_f32;
}
15 changes: 14 additions & 1 deletion tests/ui/excessive_precision.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//@run-rustfix
#![warn(clippy::excessive_precision)]
#![allow(dead_code, unused_variables, clippy::print_literal, clippy::useless_vec)]
#![allow(
dead_code,
overflowing_literals,
unused_variables,
clippy::print_literal,
clippy::useless_vec
)]

fn main() {
// Consts
Expand Down Expand Up @@ -66,4 +72,11 @@ fn main() {

// issue #7745
let _ = 1.000_000_000_000_001e-324_f64;

// issue #9910
const INF1: f32 = 1.0e+33f32;
const INF2: f64 = 1.0e+3300f64;
const NEG_INF1: f32 = -1.0e+33f32;
const NEG_INF2: f64 = -1.0e+3300f64;
const NEG_INF3: f32 = -3.40282357e+38_f32;
}
30 changes: 15 additions & 15 deletions tests/ui/excessive_precision.stderr
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
error: float has excessive precision
--> $DIR/excessive_precision.rs:15:26
--> $DIR/excessive_precision.rs:21: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
--> $DIR/excessive_precision.rs:22: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
--> $DIR/excessive_precision.rs:23:26
|
LL | const BAD32_3: 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
--> $DIR/excessive_precision.rs:24:29
|
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:22:26
--> $DIR/excessive_precision.rs:28: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`

error: float has excessive precision
--> $DIR/excessive_precision.rs:25:22
--> $DIR/excessive_precision.rs:31: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:42: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:43: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:44: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:48:36
--> $DIR/excessive_precision.rs:54: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`

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

error: float has excessive precision
--> $DIR/excessive_precision.rs:53:24
--> $DIR/excessive_precision.rs:59: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:62: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: float has excessive precision
--> $DIR/excessive_precision.rs:65:13
--> $DIR/excessive_precision.rs:71:13
|
LL | let _ = 2.225_073_858_507_201_1e-308_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `2.225_073_858_507_201e-308_f64`

error: float has excessive precision
--> $DIR/excessive_precision.rs:68:13
--> $DIR/excessive_precision.rs:74:13
|
LL | let _ = 1.000_000_000_000_001e-324_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0_f64`
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/lossy_float_literal.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@run-rustfix
#![warn(clippy::lossy_float_literal)]
#![allow(overflowing_literals, unused)]

fn main() {
// Lossy whole-number float literals
Expand Down Expand Up @@ -32,4 +33,7 @@ fn main() {
let _: f64 = 1e99;
let _: f64 = 1E99;
let _: f32 = 0.1;

const INF1: f32 = 1000000000000000000000000000000000f32;
const NEG_INF1: f32 = -340282357000000000000000000000000000001_f32;
}
4 changes: 4 additions & 0 deletions tests/ui/lossy_float_literal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@run-rustfix
#![warn(clippy::lossy_float_literal)]
#![allow(overflowing_literals, unused)]

fn main() {
// Lossy whole-number float literals
Expand Down Expand Up @@ -32,4 +33,7 @@ fn main() {
let _: f64 = 1e99;
let _: f64 = 1E99;
let _: f32 = 0.1;

const INF1: f32 = 1000000000000000000000000000000000f32;
const NEG_INF1: f32 = -340282357000000000000000000000000000001_f32;
}
22 changes: 11 additions & 11 deletions tests/ui/lossy_float_literal.stderr
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:6:18
--> $DIR/lossy_float_literal.rs:7:18
|
LL | let _: f32 = 16_777_217.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
|
= note: `-D clippy::lossy-float-literal` implied by `-D warnings`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:7:18
--> $DIR/lossy_float_literal.rs:8:18
|
LL | let _: f32 = 16_777_219.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:8:18
--> $DIR/lossy_float_literal.rs:9:18
|
LL | let _: f32 = 16_777_219.;
| ^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:9:18
--> $DIR/lossy_float_literal.rs:10:18
|
LL | let _: f32 = 16_777_219.000;
| ^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:10:13
--> $DIR/lossy_float_literal.rs:11:13
|
LL | let _ = 16_777_219f32;
| ^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220_f32`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:11:19
--> $DIR/lossy_float_literal.rs:12:19
|
LL | let _: f32 = -16_777_219.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:12:18
--> $DIR/lossy_float_literal.rs:13:18
|
LL | let _: f64 = 9_007_199_254_740_993.0;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:13:18
--> $DIR/lossy_float_literal.rs:14:18
|
LL | let _: f64 = 9_007_199_254_740_993.;
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:14:18
--> $DIR/lossy_float_literal.rs:15:18
|
LL | let _: f64 = 9_007_199_254_740_993.00;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:15:13
--> $DIR/lossy_float_literal.rs:16:13
|
LL | let _ = 9_007_199_254_740_993f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992_f64`

error: literal cannot be represented as the underlying type without loss of precision
--> $DIR/lossy_float_literal.rs:16:19
--> $DIR/lossy_float_literal.rs:17:19
|
LL | let _: f64 = -9_007_199_254_740_993.0;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
Expand Down