Skip to content

Commit

Permalink
Rollup merge of #90989 - notriddle:notriddle/rustc-suggest-float-endi…
Browse files Browse the repository at this point in the history
…ng-in-dot, r=sanxiyn

Avoid suggesting literal formatting that turns into member access

Fixes #90974
  • Loading branch information
JohnTitor authored Nov 18, 2021
2 parents b8ef644 + a7261c3 commit a1bd7c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.span_to_snippet(lit.span)
.unwrap_or_else(|_| "<numeric literal>".to_owned());

// If this is a floating point literal that ends with '.',
// get rid of it to stop this from becoming a member access.
let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);

err.span_suggestion(
lit.span,
&format!(
"you must specify a concrete type for this numeric value, \
like `{}`",
concrete_type
),
format!("{}_{}", snippet, concrete_type),
format!("{snippet}_{concrete_type}"),
Applicability::MaybeIncorrect,
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/suggestions/issue-90974.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("{}", (3.).recip()); //~ERROR
}
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/issue-90974.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
--> $DIR/issue-90974.rs:2:25
|
LL | println!("{}", (3.).recip());
| ^^^^^
|
help: you must specify a concrete type for this numeric value, like `f32`
|
LL | println!("{}", (3_f32).recip());
| ~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0689`.

0 comments on commit a1bd7c9

Please sign in to comment.