special case removing &
suggestion
This commit is contained in:
parent
ca1178f022
commit
b73cdf1b29
@ -1787,6 +1787,14 @@ impl Expr<'_> {
|
|||||||
expr
|
expr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn peel_borrows(&self) -> &Self {
|
||||||
|
let mut expr = self;
|
||||||
|
while let ExprKind::AddrOf(.., inner) = &expr.kind {
|
||||||
|
expr = inner;
|
||||||
|
}
|
||||||
|
expr
|
||||||
|
}
|
||||||
|
|
||||||
pub fn can_have_side_effects(&self) -> bool {
|
pub fn can_have_side_effects(&self) -> bool {
|
||||||
match self.peel_drop_temps().kind {
|
match self.peel_drop_temps().kind {
|
||||||
ExprKind::Path(_) | ExprKind::Lit(_) => false,
|
ExprKind::Path(_) | ExprKind::Lit(_) => false,
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
use super::FnCtxt;
|
use super::FnCtxt;
|
||||||
|
|
||||||
use crate::type_error_struct;
|
use crate::type_error_struct;
|
||||||
|
use hir::ExprKind;
|
||||||
use rustc_errors::{
|
use rustc_errors::{
|
||||||
struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
|
struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
|
||||||
};
|
};
|
||||||
@ -237,12 +238,23 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
|||||||
fcx,
|
fcx,
|
||||||
);
|
);
|
||||||
|
|
||||||
err.span_suggestion_verbose(
|
if matches!(self.expr.kind, ExprKind::AddrOf(..)) {
|
||||||
self.expr_span.shrink_to_lo(),
|
// get just the borrow part of the expression
|
||||||
"dereference the expression",
|
let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
|
||||||
"*",
|
err.span_suggestion_verbose(
|
||||||
Applicability::MachineApplicable,
|
span,
|
||||||
);
|
"remove the unneeded borrow",
|
||||||
|
"",
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
err.span_suggestion_verbose(
|
||||||
|
self.expr_span.shrink_to_lo(),
|
||||||
|
"dereference the expression",
|
||||||
|
"*",
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
err.emit();
|
err.emit();
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
&0u8 as u8; //~ ERROR E0606
|
let x = &(&0u8 as u8); //~ ERROR E0606
|
||||||
|
x as u8; //~ casting `&u8` as `u8` is invalid [E0606]
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,26 @@
|
|||||||
error[E0606]: casting `&u8` as `u8` is invalid
|
error[E0606]: casting `&u8` as `u8` is invalid
|
||||||
--> $DIR/E0606.rs:2:5
|
--> $DIR/E0606.rs:2:14
|
||||||
|
|
|
|
||||||
LL | &0u8 as u8;
|
LL | let x = &(&0u8 as u8);
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
help: remove the unneeded borrow
|
||||||
|
|
|
||||||
|
LL - let x = &(&0u8 as u8);
|
||||||
|
LL + let x = &(0u8 as u8);
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0606]: casting `&u8` as `u8` is invalid
|
||||||
|
--> $DIR/E0606.rs:3:5
|
||||||
|
|
|
||||||
|
LL | x as u8;
|
||||||
|
| ^^^^^^^
|
||||||
|
|
|
|
||||||
help: dereference the expression
|
help: dereference the expression
|
||||||
|
|
|
|
||||||
LL | *&0u8 as u8;
|
LL | *x as u8;
|
||||||
| +
|
| +
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0606`.
|
For more information about this error, try `rustc --explain E0606`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user