Skip to content

Commit

Permalink
emit errors when using RangeFrom and RangeTo
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Nov 11, 2022
1 parent 004986b commit 6018f11
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
43 changes: 32 additions & 11 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,18 +1211,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr: &hir::Expr<'_>,
expected_ty: Ty<'tcx>,
) -> bool {
if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [start, _], _) = expr.kind
&& expected_ty.is_floating_point()
{
err.span_suggestion_verbose(
self.tcx.sess.source_map().next_point(start.span),
"remove the unnecessary `.` operator to to use a floating point literal",
"",
Applicability::MachineApplicable,
);
return true;
if !expected_ty.is_floating_point() {
return false;
}
match expr.kind {
ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [start, end], _) => {
err.span_suggestion_verbose(
start.span.shrink_to_hi().with_hi(end.span.lo()),
"remove the unnecessary `.` operator for a floating point literal",
'.',
Applicability::MaybeIncorrect,
);
true
}
ExprKind::Struct(QPath::LangItem(LangItem::RangeFrom, ..), [start], _) => {
err.span_suggestion_verbose(
expr.span.with_lo(start.span.hi()),
"remove the unnecessary `.` operator for a floating point literal",
'.',
Applicability::MaybeIncorrect,
);
true
}
ExprKind::Struct(QPath::LangItem(LangItem::RangeTo, ..), [end], _) => {
err.span_suggestion_verbose(
expr.span.until(end.span),
"remove the unnecessary `.` operator and add an integer part for a floating point literal",
"0.",
Applicability::MaybeIncorrect,
);
true
}
_ => false,
}
false
}

fn is_loop(&self, id: hir::HirId) -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
let _: f64 = 0..10; //~ ERROR mismatched types
let _: f64 = 0..; //~ ERROR mismatched types
let _: f64 = 1..; //~ ERROR mismatched types
let _: f64 = ..10; //~ ERROR mismatched types
let _: f64 = std::ops::Range { start: 0, end: 1 }; //~ ERROR mismatched types
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ LL | let _: f64 = 0..10;
|
= note: expected type `f64`
found struct `std::ops::Range<{integer}>`
help: remove the unnecessary `.` operator to to use a floating point literal
|
LL - let _: f64 = 0..10;
LL + let _: f64 = 0.10;
help: remove the unnecessary `.` operator for a floating point literal
|
LL | let _: f64 = 0.10;
| ~

error[E0308]: mismatched types
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:3:18
|
LL | let _: f64 = 0..;
LL | let _: f64 = 1..;
| --- ^^^ expected `f64`, found struct `RangeFrom`
| |
| expected due to this
|
= note: expected type `f64`
found struct `RangeFrom<{integer}>`
help: remove the unnecessary `.` operator for a floating point literal
|
LL | let _: f64 = 1.;
| ~

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

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

0 comments on commit 6018f11

Please sign in to comment.