Skip to content

Commit c467006

Browse files
committed
suggest removing unnecessary . to use a floating point literal
1 parent c1a859b commit c467006

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4343
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
4444
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
4545
|| self.suggest_into(err, expr, expr_ty, expected)
46-
|| self.suggest_option_to_bool(err, expr, expr_ty, expected);
46+
|| self.suggest_option_to_bool(err, expr, expr_ty, expected)
47+
|| self.suggest_floating_point_literal(err, expr, expected);
4748

4849
self.note_type_is_not_clone(err, expected, expr_ty, expr);
4950
self.note_need_for_fn_pointer(err, expected, expr_ty);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12041204
}
12051205
}
12061206

1207+
#[instrument(skip(self, err))]
1208+
pub(crate) fn suggest_floating_point_literal(
1209+
&self,
1210+
err: &mut Diagnostic,
1211+
expr: &hir::Expr<'_>,
1212+
expected_ty: Ty<'tcx>,
1213+
) -> bool {
1214+
if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [start, _], _) = expr.kind
1215+
&& expected_ty.is_floating_point()
1216+
{
1217+
err.span_suggestion_verbose(
1218+
self.tcx.sess.source_map().next_point(start.span),
1219+
"remove the unnecessary `.` operator to to use a floating point literal",
1220+
"",
1221+
Applicability::MachineApplicable,
1222+
);
1223+
return true;
1224+
}
1225+
false
1226+
}
1227+
12071228
fn is_loop(&self, id: hir::HirId) -> bool {
12081229
let node = self.tcx.hir().get(id);
12091230
matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. }))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let _: f64 = 0..10; //~ ERROR mismatched types
3+
let _: f64 = 0..; //~ ERROR mismatched types
4+
let _: f64 = ..10; //~ ERROR mismatched types
5+
let _: f64 = std::ops::Range { start: 0, end: 1 }; //~ ERROR mismatched types
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:2:18
3+
|
4+
LL | let _: f64 = 0..10;
5+
| --- ^^^^^ expected `f64`, found struct `std::ops::Range`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected type `f64`
10+
found struct `std::ops::Range<{integer}>`
11+
help: remove the unnecessary `.` operator to to use a floating point literal
12+
|
13+
LL - let _: f64 = 0..10;
14+
LL + let _: f64 = 0.10;
15+
|
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:3:18
19+
|
20+
LL | let _: f64 = 0..;
21+
| --- ^^^ expected `f64`, found struct `RangeFrom`
22+
| |
23+
| expected due to this
24+
|
25+
= note: expected type `f64`
26+
found struct `RangeFrom<{integer}>`
27+
28+
error[E0308]: mismatched types
29+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:4:18
30+
|
31+
LL | let _: f64 = ..10;
32+
| --- ^^^^ expected `f64`, found struct `RangeTo`
33+
| |
34+
| expected due to this
35+
|
36+
= note: expected type `f64`
37+
found struct `RangeTo<{integer}>`
38+
39+
error[E0308]: mismatched types
40+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:5:18
41+
|
42+
LL | let _: f64 = std::ops::Range { start: 0, end: 1 };
43+
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found struct `std::ops::Range`
44+
| |
45+
| expected due to this
46+
|
47+
= note: expected type `f64`
48+
found struct `std::ops::Range<{integer}>`
49+
50+
error: aborting due to 4 previous errors
51+
52+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)