Skip to content

Commit fb2751b

Browse files
float_literal_f32_fallback: Don't suggest invalid code
When a float literal ended with a dot, `float_literal_f32_fallback` should not include it in its suggestion.
1 parent 3bf5c6d commit fb2751b

6 files changed

Lines changed: 38 additions & 5 deletions

File tree

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
171171
.inspect(|vid| {
172172
let origin = self.float_var_origin(*vid);
173173
// Show the entire literal in the suggestion to make it clearer.
174-
let literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
174+
let mut literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
175+
// A `.` at the end of the literal is no longer necessary if `f32` is explicitly specified
176+
if let Some(ref mut literal) = literal
177+
&& literal.ends_with('.')
178+
{
179+
literal.pop();
180+
}
175181
self.tcx.emit_node_span_lint(
176182
FLOAT_LITERAL_F32_FALLBACK,
177183
origin.lint_id.unwrap_or(CRATE_HIR_ID),

tests/ui/float/f32-into-f32.next-solver.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ fn main() {
1515
foo(1e5_f32);
1616
//~^ WARN falling back to `f32`
1717
//~| WARN this was previously accepted
18+
foo(0_f32);
19+
//~^ WARN falling back to `f32`
20+
//~| WARN this was previously accepted
1821
foo(4f32); // no warning
1922
let x = -4.0_f32;
2023
//~^ WARN falling back to `f32`

tests/ui/float/f32-into-f32.next-solver.stderr

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ LL | foo(1e5);
2727
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
2828

2929
warning: falling back to `f32` as the trait bound `f32: From<f64>` is not satisfied
30-
--> $DIR/f32-into-f32.rs:19:14
30+
--> $DIR/f32-into-f32.rs:18:9
31+
|
32+
LL | foo(0.);
33+
| ^^ help: explicitly specify the type as `f32`: `0_f32`
34+
|
35+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
36+
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
37+
38+
warning: falling back to `f32` as the trait bound `f32: From<f64>` is not satisfied
39+
--> $DIR/f32-into-f32.rs:22:14
3140
|
3241
LL | let x = -4.0;
3342
| ^^^ help: explicitly specify the type as `f32`: `4.0_f32`
3443
|
3544
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3645
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
3746

38-
warning: 4 warnings emitted
47+
warning: 5 warnings emitted
3948

tests/ui/float/f32-into-f32.old-solver.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ fn main() {
1515
foo(1e5_f32);
1616
//~^ WARN falling back to `f32`
1717
//~| WARN this was previously accepted
18+
foo(0_f32);
19+
//~^ WARN falling back to `f32`
20+
//~| WARN this was previously accepted
1821
foo(4f32); // no warning
1922
let x = -4.0_f32;
2023
//~^ WARN falling back to `f32`

tests/ui/float/f32-into-f32.old-solver.stderr

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ LL | foo(1e5);
2727
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
2828

2929
warning: falling back to `f32` as the trait bound `f32: From<f64>` is not satisfied
30-
--> $DIR/f32-into-f32.rs:19:14
30+
--> $DIR/f32-into-f32.rs:18:9
31+
|
32+
LL | foo(0.);
33+
| ^^ help: explicitly specify the type as `f32`: `0_f32`
34+
|
35+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
36+
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
37+
38+
warning: falling back to `f32` as the trait bound `f32: From<f64>` is not satisfied
39+
--> $DIR/f32-into-f32.rs:22:14
3140
|
3241
LL | let x = -4.0;
3342
| ^^^ help: explicitly specify the type as `f32`: `4.0_f32`
3443
|
3544
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3645
= note: for more information, see issue #154024 <https://github.com/rust-lang/rust/issues/154024>
3746

38-
warning: 4 warnings emitted
47+
warning: 5 warnings emitted
3948

tests/ui/float/f32-into-f32.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ fn main() {
1515
foo(1e5);
1616
//~^ WARN falling back to `f32`
1717
//~| WARN this was previously accepted
18+
foo(0.);
19+
//~^ WARN falling back to `f32`
20+
//~| WARN this was previously accepted
1821
foo(4f32); // no warning
1922
let x = -4.0;
2023
//~^ WARN falling back to `f32`

0 commit comments

Comments
 (0)