From c467006fd0d0fd046590c7867fcfef2062d144a3 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 8 Nov 2022 19:45:41 +0900 Subject: [PATCH 1/3] suggest removing unnecessary . to use a floating point literal --- compiler/rustc_hir_typeck/src/demand.rs | 3 +- .../src/fn_ctxt/suggestions.rs | 21 ++++++++ ...ecessary_dot_for_floating_point_literal.rs | 6 +++ ...sary_dot_for_floating_point_literal.stderr | 52 +++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs create mode 100644 src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 7f78f5fb8a7b2..be618eb664c3f 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -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); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index a14759e254c49..fb8669e949cdf 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1204,6 +1204,27 @@ 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 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; + } + false + } + fn is_loop(&self, id: hir::HirId) -> bool { let node = self.tcx.hir().get(id); matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. })) diff --git a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs new file mode 100644 index 0000000000000..b1eda63d56e27 --- /dev/null +++ b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs @@ -0,0 +1,6 @@ +fn main() { + let _: f64 = 0..10; //~ ERROR mismatched types + let _: f64 = 0..; //~ ERROR mismatched types + let _: f64 = ..10; //~ ERROR mismatched types + let _: f64 = std::ops::Range { start: 0, end: 1 }; //~ ERROR mismatched types +} diff --git a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr new file mode 100644 index 0000000000000..2f1d7dc230f4d --- /dev/null +++ b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr @@ -0,0 +1,52 @@ +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 to to use a floating point literal + | +LL - let _: f64 = 0..10; +LL + let _: f64 = 0.10; + | + +error[E0308]: mismatched types + --> $DIR/unnecessary_dot_for_floating_point_literal.rs:3:18 + | +LL | let _: f64 = 0..; + | --- ^^^ expected `f64`, found struct `RangeFrom` + | | + | expected due to this + | + = note: expected type `f64` + found struct `RangeFrom<{integer}>` + +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}>` + +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`. From 004986b79b933a5c9ec160b977f791b154c8d275 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 8 Nov 2022 19:45:48 +0900 Subject: [PATCH 2/3] avoid unnecessary `format!` --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index fb8669e949cdf..5a0af00b6e1fc 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -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, ); From 6018f115aa7365db39a9df1f883123076403a97b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 10 Nov 2022 07:24:22 +0900 Subject: [PATCH 3/3] emit errors when using `RangeFrom` and `RangeTo` --- .../src/fn_ctxt/suggestions.rs | 43 ++++++++++++++----- ...ecessary_dot_for_floating_point_literal.rs | 2 +- ...sary_dot_for_floating_point_literal.stderr | 17 +++++--- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 5a0af00b6e1fc..06e6e4350fcbc 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -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 { diff --git a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs index b1eda63d56e27..c1a944562683a 100644 --- a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs +++ b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.rs @@ -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 } diff --git a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr index 2f1d7dc230f4d..773f1392ae765 100644 --- a/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr +++ b/src/test/ui/suggestions/unnecessary_dot_for_floating_point_literal.stderr @@ -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 @@ -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