Skip to content

Commit

Permalink
Rollup merge of rust-lang#104144 - TaKO8Ki:suggest-removing-unnecessa…
Browse files Browse the repository at this point in the history
…ry-dot, r=fee1-dead

Suggest removing unnecessary `.` to use a floating point literal

Fixes a part of rust-lang#101883
  • Loading branch information
Manishearth authored Nov 11, 2022
2 parents 64df701 + 6018f11 commit e944a06
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_option_to_bool(err, expr, expr_ty, expected);
|| self.suggest_option_to_bool(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected);

self.note_type_is_not_clone(err, expected, expr_ty, expr);
self.note_need_for_fn_pointer(err, expected, expr_ty);
Expand Down
44 changes: 43 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let annotation_span = ty.span;
err.span_suggestion(
annotation_span.with_hi(annotation_span.lo()),
format!("alternatively, consider changing the type annotation"),
"alternatively, consider changing the type annotation",
suggest_annotation,
Applicability::MaybeIncorrect,
);
Expand Down Expand Up @@ -1204,6 +1204,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

#[instrument(skip(self, err))]
pub(crate) fn suggest_floating_point_literal(
&self,
err: &mut Diagnostic,
expr: &hir::Expr<'_>,
expected_ty: Ty<'tcx>,
) -> bool {
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,
}
}

fn is_loop(&self, id: hir::HirId) -> bool {
let node = self.tcx.hir().get(id);
matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. }))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let _: f64 = 0..10; //~ 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
@@ -0,0 +1,59 @@
error[E0308]: mismatched types
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:2:18
|
LL | let _: f64 = 0..10;
| --- ^^^^^ expected `f64`, found struct `std::ops::Range`
| |
| expected due to this
|
= note: expected type `f64`
found struct `std::ops::Range<{integer}>`
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 = 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
|
LL | let _: f64 = ..10;
| --- ^^^^ expected `f64`, found struct `RangeTo`
| |
| expected due to this
|
= 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
|
LL | let _: f64 = std::ops::Range { start: 0, end: 1 };
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found struct `std::ops::Range`
| |
| expected due to this
|
= note: expected type `f64`
found struct `std::ops::Range<{integer}>`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit e944a06

Please sign in to comment.