Skip to content

Commit 6018f11

Browse files
committed
emit errors when using RangeFrom and RangeTo
1 parent 004986b commit 6018f11

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -1211,18 +1211,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12111211
expr: &hir::Expr<'_>,
12121212
expected_ty: Ty<'tcx>,
12131213
) -> 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;
1214+
if !expected_ty.is_floating_point() {
1215+
return false;
1216+
}
1217+
match expr.kind {
1218+
ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [start, end], _) => {
1219+
err.span_suggestion_verbose(
1220+
start.span.shrink_to_hi().with_hi(end.span.lo()),
1221+
"remove the unnecessary `.` operator for a floating point literal",
1222+
'.',
1223+
Applicability::MaybeIncorrect,
1224+
);
1225+
true
1226+
}
1227+
ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, ..), [start], _) => {
1228+
err.span_suggestion_verbose(
1229+
expr.span.with_lo(start.span.hi()),
1230+
"remove the unnecessary `.` operator for a floating point literal",
1231+
'.',
1232+
Applicability::MaybeIncorrect,
1233+
);
1234+
true
1235+
}
1236+
ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, ..), [end], _) => {
1237+
err.span_suggestion_verbose(
1238+
expr.span.until(end.span),
1239+
"remove the unnecessary `.` operator and add an integer part for a floating point literal",
1240+
"0.",
1241+
Applicability::MaybeIncorrect,
1242+
);
1243+
true
1244+
}
1245+
_ => false,
12241246
}
1225-
false
12261247
}
12271248

12281249
fn is_loop(&self, id: hir::HirId) -> bool {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
let _: f64 = 0..10; //~ ERROR mismatched types
3-
let _: f64 = 0..; //~ ERROR mismatched types
3+
let _: f64 = 1..; //~ ERROR mismatched types
44
let _: f64 = ..10; //~ ERROR mismatched types
55
let _: f64 = std::ops::Range { start: 0, end: 1 }; //~ ERROR mismatched types
66
}

src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ LL | let _: f64 = 0..10;
88
|
99
= note: expected type `f64`
1010
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;
11+
help: remove the unnecessary `.` operator for a floating point literal
1512
|
13+
LL | let _: f64 = 0.10;
14+
| ~
1615

1716
error[E0308]: mismatched types
1817
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:3:18
1918
|
20-
LL | let _: f64 = 0..;
19+
LL | let _: f64 = 1..;
2120
| --- ^^^ expected `f64`, found struct `RangeFrom`
2221
| |
2322
| expected due to this
2423
|
2524
= note: expected type `f64`
2625
found struct `RangeFrom<{integer}>`
26+
help: remove the unnecessary `.` operator for a floating point literal
27+
|
28+
LL | let _: f64 = 1.;
29+
| ~
2730

2831
error[E0308]: mismatched types
2932
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:4:18
@@ -35,6 +38,10 @@ LL | let _: f64 = ..10;
3538
|
3639
= note: expected type `f64`
3740
found struct `RangeTo<{integer}>`
41+
help: remove the unnecessary `.` operator and add an integer part for a floating point literal
42+
|
43+
LL | let _: f64 = 0.10;
44+
| ~~
3845

3946
error[E0308]: mismatched types
4047
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:5:18

0 commit comments

Comments
 (0)