suggest removing unnecessary . to use a floating point literal
This commit is contained in:
parent
c1a859b25a
commit
c467006fd0
4 changed files with 81 additions and 1 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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(..), .. }))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue