Rollup merge of #100609 - chenyukang:fix-100527, r=davidtwco

Extend invalid floating point literal suffix suggestion

Fixes #100527
This commit is contained in:
Dylan DPC 2022-08-16 18:16:12 +05:30 committed by GitHub
commit cf5c7645ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 16 deletions

View File

@ -2193,7 +2193,7 @@ fn check_field(
E0610,
"`{expr_t}` is a primitive type and therefore doesn't have fields",
);
let is_valid_suffix = |field: String| {
let is_valid_suffix = |field: &str| {
if field == "f32" || field == "f64" {
return true;
}
@ -2218,20 +2218,39 @@ fn check_field(
let suffix = chars.collect::<String>();
suffix.is_empty() || suffix == "f32" || suffix == "f64"
};
let maybe_partial_suffix = |field: &str| -> Option<&str> {
let first_chars = ['f', 'l'];
if field.len() >= 1
&& field.to_lowercase().starts_with(first_chars)
&& field[1..].chars().all(|c| c.is_ascii_digit())
{
if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
} else {
None
}
};
if let ty::Infer(ty::IntVar(_)) = expr_t.kind()
&& let ExprKind::Lit(Spanned {
node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
..
}) = base.kind
&& !base.span.from_expansion()
&& is_valid_suffix(field_name)
{
err.span_suggestion_verbose(
field.span.shrink_to_lo(),
"If the number is meant to be a floating point number, consider adding a `0` after the period",
'0',
Applicability::MaybeIncorrect,
);
if is_valid_suffix(&field_name) {
err.span_suggestion_verbose(
field.span.shrink_to_lo(),
"if intended to be a floating point literal, consider adding a `0` after the period",
'0',
Applicability::MaybeIncorrect,
);
} else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
err.span_suggestion_verbose(
field.span,
format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
format!("0{correct_suffix}"),
Applicability::MaybeIncorrect,
);
}
}
err.emit();
}

View File

@ -3,4 +3,8 @@ fn main() {
let x = 0;
let _ = x.foo; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = x.bar; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 0.f; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 2.l; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 12.F; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 34.L; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
}

View File

@ -10,6 +10,50 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | let _ = x.bar;
| ^^^
error: aborting due to 2 previous errors
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:6:15
|
LL | let _ = 0.f;
| ^
|
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
|
LL | let _ = 0.0f32;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:7:15
|
LL | let _ = 2.l;
| ^
|
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
|
LL | let _ = 2.0f64;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:8:16
|
LL | let _ = 12.F;
| ^
|
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
|
LL | let _ = 12.0f32;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:9:16
|
LL | let _ = 34.L;
| ^
|
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
|
LL | let _ = 34.0f64;
| ~~~~
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0610`.

View File

@ -4,7 +4,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.e1;
| ^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0e1;
| +
@ -15,7 +15,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.E1;
| ^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0E1;
| +
@ -26,7 +26,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.f32;
| ^^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0f32;
| +
@ -37,7 +37,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.f64;
| ^^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0f64;
| +
@ -48,7 +48,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.e+12;
| ^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0e+12;
| +
@ -59,7 +59,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.e-12;
| ^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0e-12;
| +
@ -70,7 +70,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 2.e1f32;
| ^^^^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
help: if intended to be a floating point literal, consider adding a `0` after the period
|
LL | 2.0e1f32;
| +