Skip to content

Commit 29a8386

Browse files
committed
use structured suggestion when casting a reference
1 parent 6ecad33 commit 29a8386

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

src/librustc_typeck/check/cast.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,14 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
213213
fcx.ty_to_string(self.expr_ty),
214214
cast_ty));
215215
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr.span) {
216-
err.span_help(self.expr.span,
217-
&format!("did you mean `*{}`?", snippet));
216+
err.span_suggestion_with_applicability(
217+
self.expr.span,
218+
"dereference the expression",
219+
format!("*{}", snippet),
220+
Applicability::MaybeIncorrect,
221+
);
222+
} else {
223+
err.span_help(self.expr.span, "dereference the expression with `*`");
218224
}
219225
err.emit();
220226
}

src/test/ui/error-codes/E0606.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ error[E0606]: casting `&u8` as `u8` is invalid
22
--> $DIR/E0606.rs:2:5
33
|
44
LL | &0u8 as u8; //~ ERROR E0606
5-
| ^^^^^^^^^^ cannot cast `&u8` as `u8`
6-
|
7-
help: did you mean `*&0u8`?
8-
--> $DIR/E0606.rs:2:5
9-
|
10-
LL | &0u8 as u8; //~ ERROR E0606
11-
| ^^^^
5+
| ----^^^^^^
6+
| |
7+
| cannot cast `&u8` as `u8`
8+
| help: dereference the expression: `*&0u8`
129

1310
error: aborting due to previous error
1411

src/test/ui/error-festival.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ error[E0606]: casting `&u8` as `u32` is invalid
6060
--> $DIR/error-festival.rs:37:18
6161
|
6262
LL | let y: u32 = x as u32;
63-
| ^^^^^^^^ cannot cast `&u8` as `u32`
64-
|
65-
help: did you mean `*x`?
66-
--> $DIR/error-festival.rs:37:18
67-
|
68-
LL | let y: u32 = x as u32;
69-
| ^
63+
| -^^^^^^^
64+
| |
65+
| cannot cast `&u8` as `u32`
66+
| help: dereference the expression: `*x`
7067

7168
error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
7269
--> $DIR/error-festival.rs:41:5

src/test/ui/mismatched_types/cast-rfc0401.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,10 @@ error[E0606]: casting `&{float}` as `f32` is invalid
240240
--> $DIR/cast-rfc0401.rs:71:30
241241
|
242242
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>(); //~ ERROR is invalid
243-
| ^^^^^^^^ cannot cast `&{float}` as `f32`
244-
|
245-
help: did you mean `*s`?
246-
--> $DIR/cast-rfc0401.rs:71:30
247-
|
248-
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>(); //~ ERROR is invalid
249-
| ^
243+
| -^^^^^^^
244+
| |
245+
| cannot cast `&{float}` as `f32`
246+
| help: dereference the expression: `*s`
250247

251248
error: aborting due to 34 previous errors
252249

0 commit comments

Comments
 (0)