Skip to content

Commit 5353238

Browse files
committed
Don't lint excessive_precision on inf
1 parent 1d0d686 commit 5353238

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ 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+
(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
9595
},
9696
};
9797

98-
if is_whole && !sym_str.contains(|c| c == 'e' || c == 'E') {
98+
if is_whole && !is_inf && !sym_str.contains(|c| c == 'e' || c == 'E') {
9999
// Normalize the literal by stripping the fractional portion
100100
if sym_str.split('.').next().unwrap() != float_str {
101101
// If the type suffix is missing the suggestion would be

tests/ui/excessive_precision.fixed

Lines changed: 13 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,10 @@ 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;
6981
}

tests/ui/excessive_precision.rs

Lines changed: 13 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,10 @@ 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;
6981
}

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`

0 commit comments

Comments
 (0)