diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 4c5a5f9b7..47d5ab8b0 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -2312,13 +2312,17 @@ impl Display for WithDecimalPoint { } } - let mut writer = LookForDecimalPoint { - formatter, - has_decimal_point: false, - }; - tri!(write!(writer, "{}", self.0)); - if !writer.has_decimal_point { - tri!(formatter.write_str(".0")); + if self.0.is_finite() { + let mut writer = LookForDecimalPoint { + formatter, + has_decimal_point: false, + }; + tri!(write!(writer, "{}", self.0)); + if !writer.has_decimal_point { + tri!(formatter.write_str(".0")); + } + } else { + tri!(write!(formatter, "{}", self.0)); } Ok(()) } diff --git a/test_suite/tests/test_de_error.rs b/test_suite/tests/test_de_error.rs index cf4bec86b..75c7e3030 100644 --- a/test_suite/tests/test_de_error.rs +++ b/test_suite/tests/test_de_error.rs @@ -1438,6 +1438,14 @@ fn test_integer_from_float() { ); } +#[test] +fn test_nan_no_decimal_point() { + assert_de_tokens_error::( + &[Token::F32(f32::NAN)], + "invalid type: floating point `NaN`, expected isize", + ); +} + #[test] fn test_unit_struct_from_seq() { assert_de_tokens_error::(