Skip to content

Commit 62972ae

Browse files
committed
Auto merge of rust-lang#10952 - Centri3:excessive_precision, r=dswij
Don't lint `excessive_precision` on inf Fixes rust-lang#9910 changelog: [`excessive_precision`]: No longer lints overflowing literals
2 parents 5da6174 + a899034 commit 62972ae

7 files changed

+70
-31
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,24 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
8282
LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
8383
LitFloatType::Unsuffixed => None
8484
};
85-
let (is_whole, mut float_str) = match fty {
85+
let (is_whole, is_inf, mut float_str) = match fty {
8686
FloatTy::F32 => {
8787
let value = sym_str.parse::<f32>().unwrap();
8888

89-
(value.fract() == 0.0, formatter.format(value))
89+
(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
9090
},
9191
FloatTy::F64 => {
9292
let value = sym_str.parse::<f64>().unwrap();
9393

94-
(value.fract() == 0.0, formatter.format(value))
94+
95+
(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
9596
},
9697
};
9798

99+
if is_inf {
100+
return;
101+
}
102+
98103
if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
99104
// Normalize the literal by stripping the fractional portion
100105
if sym_str.split('.').next().unwrap() != float_str {

tests/ui/excessive_precision.fixed

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//@run-rustfix
22
#![warn(clippy::excessive_precision)]
3-
#![allow(dead_code, unused_variables, clippy::print_literal, clippy::useless_vec)]
3+
#![allow(
4+
dead_code,
5+
overflowing_literals,
6+
unused_variables,
7+
clippy::print_literal,
8+
clippy::useless_vec
9+
)]
410

511
fn main() {
612
// Consts
@@ -66,4 +72,11 @@ fn main() {
6672

6773
// issue #7745
6874
let _ = 0_f64;
75+
76+
// issue #9910
77+
const INF1: f32 = 1.0e+33f32;
78+
const INF2: f64 = 1.0e+3300f64;
79+
const NEG_INF1: f32 = -1.0e+33f32;
80+
const NEG_INF2: f64 = -1.0e+3300f64;
81+
const NEG_INF3: f32 = -3.40282357e+38_f32;
6982
}

tests/ui/excessive_precision.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//@run-rustfix
22
#![warn(clippy::excessive_precision)]
3-
#![allow(dead_code, unused_variables, clippy::print_literal, clippy::useless_vec)]
3+
#![allow(
4+
dead_code,
5+
overflowing_literals,
6+
unused_variables,
7+
clippy::print_literal,
8+
clippy::useless_vec
9+
)]
410

511
fn main() {
612
// Consts
@@ -66,4 +72,11 @@ fn main() {
6672

6773
// issue #7745
6874
let _ = 1.000_000_000_000_001e-324_f64;
75+
76+
// issue #9910
77+
const INF1: f32 = 1.0e+33f32;
78+
const INF2: f64 = 1.0e+3300f64;
79+
const NEG_INF1: f32 = -1.0e+33f32;
80+
const NEG_INF2: f64 = -1.0e+3300f64;
81+
const NEG_INF3: f32 = -3.40282357e+38_f32;
6982
}

tests/ui/excessive_precision.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,91 @@
11
error: float has excessive precision
2-
--> $DIR/excessive_precision.rs:15:26
2+
--> $DIR/excessive_precision.rs:21:26
33
|
44
LL | const BAD32_1: f32 = 0.123_456_789_f32;
55
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79_f32`
66
|
77
= note: `-D clippy::excessive-precision` implied by `-D warnings`
88

99
error: float has excessive precision
10-
--> $DIR/excessive_precision.rs:16:26
10+
--> $DIR/excessive_precision.rs:22:26
1111
|
1212
LL | const BAD32_2: f32 = 0.123_456_789;
1313
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
1414

1515
error: float has excessive precision
16-
--> $DIR/excessive_precision.rs:17:26
16+
--> $DIR/excessive_precision.rs:23:26
1717
|
1818
LL | const BAD32_3: f32 = 0.100_000_000_000_1;
1919
| ^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
2020

2121
error: float has excessive precision
22-
--> $DIR/excessive_precision.rs:18:29
22+
--> $DIR/excessive_precision.rs:24:29
2323
|
2424
LL | const BAD32_EDGE: f32 = 1.000_000_9;
2525
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001`
2626

2727
error: float has excessive precision
28-
--> $DIR/excessive_precision.rs:22:26
28+
--> $DIR/excessive_precision.rs:28:26
2929
|
3030
LL | const BAD64_3: f64 = 0.100_000_000_000_000_000_1;
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
3232

3333
error: float has excessive precision
34-
--> $DIR/excessive_precision.rs:25:22
34+
--> $DIR/excessive_precision.rs:31:22
3535
|
3636
LL | println!("{:?}", 8.888_888_888_888_888_888_888);
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89`
3838

3939
error: float has excessive precision
40-
--> $DIR/excessive_precision.rs:36:22
40+
--> $DIR/excessive_precision.rs:42:22
4141
|
4242
LL | let bad32: f32 = 1.123_456_789;
4343
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8`
4444

4545
error: float has excessive precision
46-
--> $DIR/excessive_precision.rs:37:26
46+
--> $DIR/excessive_precision.rs:43:26
4747
|
4848
LL | let bad32_suf: f32 = 1.123_456_789_f32;
4949
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
5050

5151
error: float has excessive precision
52-
--> $DIR/excessive_precision.rs:38:21
52+
--> $DIR/excessive_precision.rs:44:21
5353
|
5454
LL | let bad32_inf = 1.123_456_789_f32;
5555
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
5656

5757
error: float has excessive precision
58-
--> $DIR/excessive_precision.rs:48:36
58+
--> $DIR/excessive_precision.rs:54:36
5959
|
6060
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789];
6161
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
6262

6363
error: float has excessive precision
64-
--> $DIR/excessive_precision.rs:49:36
64+
--> $DIR/excessive_precision.rs:55:36
6565
|
6666
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_789];
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_123_456_78`
6868

6969
error: float has excessive precision
70-
--> $DIR/excessive_precision.rs:53:24
70+
--> $DIR/excessive_precision.rs:59:24
7171
|
7272
LL | let bad_e32: f32 = 1.123_456_788_888e-10;
7373
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8e-10`
7474

7575
error: float has excessive precision
76-
--> $DIR/excessive_precision.rs:56:27
76+
--> $DIR/excessive_precision.rs:62:27
7777
|
7878
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
7979
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
8080

8181
error: float has excessive precision
82-
--> $DIR/excessive_precision.rs:65:13
82+
--> $DIR/excessive_precision.rs:71:13
8383
|
8484
LL | let _ = 2.225_073_858_507_201_1e-308_f64;
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `2.225_073_858_507_201e-308_f64`
8686

8787
error: float has excessive precision
88-
--> $DIR/excessive_precision.rs:68:13
88+
--> $DIR/excessive_precision.rs:74:13
8989
|
9090
LL | let _ = 1.000_000_000_000_001e-324_f64;
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0_f64`

tests/ui/lossy_float_literal.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::lossy_float_literal)]
3+
#![allow(overflowing_literals, unused)]
34

45
fn main() {
56
// Lossy whole-number float literals
@@ -32,4 +33,7 @@ fn main() {
3233
let _: f64 = 1e99;
3334
let _: f64 = 1E99;
3435
let _: f32 = 0.1;
36+
37+
const INF1: f32 = 1000000000000000000000000000000000f32;
38+
const NEG_INF1: f32 = -340282357000000000000000000000000000001_f32;
3539
}

tests/ui/lossy_float_literal.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::lossy_float_literal)]
3+
#![allow(overflowing_literals, unused)]
34

45
fn main() {
56
// Lossy whole-number float literals
@@ -32,4 +33,7 @@ fn main() {
3233
let _: f64 = 1e99;
3334
let _: f64 = 1E99;
3435
let _: f32 = 0.1;
36+
37+
const INF1: f32 = 1000000000000000000000000000000000f32;
38+
const NEG_INF1: f32 = -340282357000000000000000000000000000001_f32;
3539
}

tests/ui/lossy_float_literal.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
11
error: literal cannot be represented as the underlying type without loss of precision
2-
--> $DIR/lossy_float_literal.rs:6:18
2+
--> $DIR/lossy_float_literal.rs:7:18
33
|
44
LL | let _: f32 = 16_777_217.0;
55
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
66
|
77
= note: `-D clippy::lossy-float-literal` implied by `-D warnings`
88

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)