Fix invalid float literal suggestions when recovering an integer
Only suggest adding a zero to integers with a preceding dot when the change will result in a valid floating point literal. For example, `.0x0` should not be turned into `0.0x0`.
This commit is contained in:
parent
481725984b
commit
80fcd7c40e
3 changed files with 60 additions and 1 deletions
|
@ -1854,7 +1854,16 @@ impl<'a> Parser<'a> {
|
|||
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
|
||||
next_token.kind
|
||||
{
|
||||
if self.token.span.hi() == next_token.span.lo() {
|
||||
// If this integer looks like a float, then recover as such.
|
||||
//
|
||||
// We will never encounter the exponent part of a floating
|
||||
// point literal here, since there's no use of the exponent
|
||||
// syntax that also constitutes a valid integer, so we need
|
||||
// not check for that.
|
||||
if suffix.map_or(true, |s| s == sym::f32 || s == sym::f64)
|
||||
&& symbol.as_str().chars().all(|c| c.is_numeric() || c == '_')
|
||||
&& self.token.span.hi() == next_token.span.lo()
|
||||
{
|
||||
let s = String::from("0.") + symbol.as_str();
|
||||
let kind = TokenKind::lit(token::Float, Symbol::intern(&s), suffix);
|
||||
return Some(Token::new(kind, self.token.span.to(next_token.span)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue