Skip to content

Commit 081cc38

Browse files
authored
Rollup merge of rust-lang#104144 - TaKO8Ki:suggest-removing-unnecessary-dot, r=fee1-dead
Suggest removing unnecessary `.` to use a floating point literal Fixes a part of rust-lang#101883
2 parents 02a768b + 6018f11 commit 081cc38

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4343
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
4444
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
4545
|| self.suggest_into(err, expr, expr_ty, expected)
46-
|| self.suggest_option_to_bool(err, expr, expr_ty, expected);
46+
|| self.suggest_option_to_bool(err, expr, expr_ty, expected)
47+
|| self.suggest_floating_point_literal(err, expr, expected);
4748

4849
self.note_type_is_not_clone(err, expected, expr_ty, expr);
4950
self.note_need_for_fn_pointer(err, expected, expr_ty);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374374
let annotation_span = ty.span;
375375
err.span_suggestion(
376376
annotation_span.with_hi(annotation_span.lo()),
377-
format!("alternatively, consider changing the type annotation"),
377+
"alternatively, consider changing the type annotation",
378378
suggest_annotation,
379379
Applicability::MaybeIncorrect,
380380
);
@@ -1204,6 +1204,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12041204
}
12051205
}
12061206

1207+
#[instrument(skip(self, err))]
1208+
pub(crate) fn suggest_floating_point_literal(
1209+
&self,
1210+
err: &mut Diagnostic,
1211+
expr: &hir::Expr<'_>,
1212+
expected_ty: Ty<'tcx>,
1213+
) -> bool {
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,
1246+
}
1247+
}
1248+
12071249
fn is_loop(&self, id: hir::HirId) -> bool {
12081250
let node = self.tcx.hir().get(id);
12091251
matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. }))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let _: f64 = 0..10; //~ ERROR mismatched types
3+
let _: f64 = 1..; //~ ERROR mismatched types
4+
let _: f64 = ..10; //~ ERROR mismatched types
5+
let _: f64 = std::ops::Range { start: 0, end: 1 }; //~ ERROR mismatched types
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:2:18
3+
|
4+
LL | let _: f64 = 0..10;
5+
| --- ^^^^^ expected `f64`, found struct `std::ops::Range`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected type `f64`
10+
found struct `std::ops::Range<{integer}>`
11+
help: remove the unnecessary `.` operator for a floating point literal
12+
|
13+
LL | let _: f64 = 0.10;
14+
| ~
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:3:18
18+
|
19+
LL | let _: f64 = 1..;
20+
| --- ^^^ expected `f64`, found struct `RangeFrom`
21+
| |
22+
| expected due to this
23+
|
24+
= note: expected type `f64`
25+
found struct `RangeFrom<{integer}>`
26+
help: remove the unnecessary `.` operator for a floating point literal
27+
|
28+
LL | let _: f64 = 1.;
29+
| ~
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:4:18
33+
|
34+
LL | let _: f64 = ..10;
35+
| --- ^^^^ expected `f64`, found struct `RangeTo`
36+
| |
37+
| expected due to this
38+
|
39+
= note: expected type `f64`
40+
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+
| ~~
45+
46+
error[E0308]: mismatched types
47+
--> $DIR/unnecessary_dot_for_floating_point_literal.rs:5:18
48+
|
49+
LL | let _: f64 = std::ops::Range { start: 0, end: 1 };
50+
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f64`, found struct `std::ops::Range`
51+
| |
52+
| expected due to this
53+
|
54+
= note: expected type `f64`
55+
found struct `std::ops::Range<{integer}>`
56+
57+
error: aborting due to 4 previous errors
58+
59+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)