Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use structured suggestion when casting a reference #57493

Merged
merged 1 commit into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,14 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
fcx.ty_to_string(self.expr_ty),
cast_ty));
if let Ok(snippet) = fcx.sess().source_map().span_to_snippet(self.expr.span) {
err.span_help(self.expr.span,
&format!("did you mean `*{}`?", snippet));
err.span_suggestion_with_applicability(
self.expr.span,
"dereference the expression",
format!("*{}", snippet),
Applicability::MaybeIncorrect,
);
} else {
err.span_help(self.expr.span, "dereference the expression with `*`");
}
err.emit();
}
Expand Down
11 changes: 4 additions & 7 deletions src/test/ui/error-codes/E0606.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ error[E0606]: casting `&u8` as `u8` is invalid
--> $DIR/E0606.rs:2:5
|
LL | &0u8 as u8; //~ ERROR E0606
| ^^^^^^^^^^ cannot cast `&u8` as `u8`
|
help: did you mean `*&0u8`?
--> $DIR/E0606.rs:2:5
|
LL | &0u8 as u8; //~ ERROR E0606
| ^^^^
| ----^^^^^^
| |
| cannot cast `&u8` as `u8`
| help: dereference the expression: `*&0u8`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously not ideal, but at least clippy will lint against it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, seems fine to me.


error: aborting due to previous error

Expand Down
11 changes: 4 additions & 7 deletions src/test/ui/error-festival.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ error[E0606]: casting `&u8` as `u32` is invalid
--> $DIR/error-festival.rs:37:18
|
LL | let y: u32 = x as u32;
| ^^^^^^^^ cannot cast `&u8` as `u32`
|
help: did you mean `*x`?
--> $DIR/error-festival.rs:37:18
|
LL | let y: u32 = x as u32;
| ^
| -^^^^^^^
| |
| cannot cast `&u8` as `u32`
| help: dereference the expression: `*x`

error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
--> $DIR/error-festival.rs:41:5
Expand Down
11 changes: 4 additions & 7 deletions src/test/ui/mismatched_types/cast-rfc0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,10 @@ error[E0606]: casting `&{float}` as `f32` is invalid
--> $DIR/cast-rfc0401.rs:71:30
|
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>(); //~ ERROR is invalid
| ^^^^^^^^ cannot cast `&{float}` as `f32`
|
help: did you mean `*s`?
--> $DIR/cast-rfc0401.rs:71:30
|
LL | vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>(); //~ ERROR is invalid
| ^
| -^^^^^^^
| |
| cannot cast `&{float}` as `f32`
| help: dereference the expression: `*s`

error: aborting due to 34 previous errors

Expand Down