Skip to content

Commit 177058c

Browse files
authored
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
2 parents 005be61 + a7261c3 commit 177058c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
319319
.span_to_snippet(lit.span)
320320
.unwrap_or_else(|_| "<numeric literal>".to_owned());
321321

322+
// If this is a floating point literal that ends with '.',
323+
// get rid of it to stop this from becoming a member access.
324+
let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
325+
322326
err.span_suggestion(
323327
lit.span,
324328
&format!(
325329
"you must specify a concrete type for this numeric value, \
326330
like `{}`",
327331
concrete_type
328332
),
329-
format!("{}_{}", snippet, concrete_type),
333+
format!("{snippet}_{concrete_type}"),
330334
Applicability::MaybeIncorrect,
331335
);
332336
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("{}", (3.).recip()); //~ERROR
3+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0689]: can't call method `recip` on ambiguous numeric type `{float}`
2+
--> $DIR/issue-90974.rs:2:25
3+
|
4+
LL | println!("{}", (3.).recip());
5+
| ^^^^^
6+
|
7+
help: you must specify a concrete type for this numeric value, like `f32`
8+
|
9+
LL | println!("{}", (3_f32).recip());
10+
| ~~~~~
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0689`.

0 commit comments

Comments
 (0)