about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-11-19 02:22:59 +0900
committerGitHub <noreply@github.com>2021-11-19 02:22:59 +0900
commitdfbbb3b90092817c4602b80bc48cfce5c8927305 (patch)
tree5f4f35ba5dc246744572f75aa1c638d99e8bfbb1
parent0a2b7d71d96a22126cce57f0dab5890d060f2259 (diff)
parenta7261c32f485872756b8dc1544963a59e86c6449 (diff)
downloadrust-dfbbb3b90092817c4602b80bc48cfce5c8927305.tar.gz
rust-dfbbb3b90092817c4602b80bc48cfce5c8927305.zip
Rollup merge of #90989 - notriddle:notriddle/rustc-suggest-float-ending-in-dot, r=sanxiyn
Avoid suggesting literal formatting that turns into member access

Fixes #90974
-rw-r--r--compiler/rustc_typeck/src/check/method/suggest.rs6
-rw-r--r--src/test/ui/suggestions/issue-90974.rs3
-rw-r--r--src/test/ui/suggestions/issue-90974.stderr14
3 files changed, 22 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs
index 661ced952c7..f96e9063c34 100644
--- a/compiler/rustc_typeck/src/check/method/suggest.rs
+++ b/compiler/rustc_typeck/src/check/method/suggest.rs
@@ -317,6 +317,10 @@ 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!(
@@ -324,7 +328,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                                          like `{}`",
                                         concrete_type
                                     ),
-                                    format!("{}_{}", snippet, concrete_type),
+                                    format!("{snippet}_{concrete_type}"),
                                     Applicability::MaybeIncorrect,
                                 );
                             }
diff --git a/src/test/ui/suggestions/issue-90974.rs b/src/test/ui/suggestions/issue-90974.rs
new file mode 100644
index 00000000000..83590dbf7ac
--- /dev/null
+++ b/src/test/ui/suggestions/issue-90974.rs
@@ -0,0 +1,3 @@
+fn main() {
+    println!("{}", (3.).recip()); //~ERROR
+}
diff --git a/src/test/ui/suggestions/issue-90974.stderr b/src/test/ui/suggestions/issue-90974.stderr
new file mode 100644
index 00000000000..e1fb479a3a7
--- /dev/null
+++ b/src/test/ui/suggestions/issue-90974.stderr
@@ -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`.