Skip to content

Commit 2d973c1

Browse files
authored
Merge pull request #2733 from jamessan/nan-decimal
Only format Unexpected::Float with decimal point if it is finite
2 parents 1477028 + 6ca499b commit 2d973c1

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

serde/src/de/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,13 +2312,17 @@ impl Display for WithDecimalPoint {
23122312
}
23132313
}
23142314

2315-
let mut writer = LookForDecimalPoint {
2316-
formatter,
2317-
has_decimal_point: false,
2318-
};
2319-
tri!(write!(writer, "{}", self.0));
2320-
if !writer.has_decimal_point {
2321-
tri!(formatter.write_str(".0"));
2315+
if self.0.is_finite() {
2316+
let mut writer = LookForDecimalPoint {
2317+
formatter,
2318+
has_decimal_point: false,
2319+
};
2320+
tri!(write!(writer, "{}", self.0));
2321+
if !writer.has_decimal_point {
2322+
tri!(formatter.write_str(".0"));
2323+
}
2324+
} else {
2325+
tri!(write!(formatter, "{}", self.0));
23222326
}
23232327
Ok(())
23242328
}

test_suite/tests/test_de_error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,14 @@ fn test_integer_from_float() {
14381438
);
14391439
}
14401440

1441+
#[test]
1442+
fn test_nan_no_decimal_point() {
1443+
assert_de_tokens_error::<isize>(
1444+
&[Token::F32(f32::NAN)],
1445+
"invalid type: floating point `NaN`, expected isize",
1446+
);
1447+
}
1448+
14411449
#[test]
14421450
fn test_unit_struct_from_seq() {
14431451
assert_de_tokens_error::<UnitStruct>(

0 commit comments

Comments
 (0)