Don't try to suggest `ref mut` for implicit `ref`

This commit is contained in:
ashtneoi 2018-07-10 23:14:12 -07:00
parent 52d6ae854d
commit 6fd1a9fff7
3 changed files with 26 additions and 20 deletions

View File

@ -1841,9 +1841,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
elem: ProjectionElem::Deref, elem: ProjectionElem::Deref,
}) if self.mir.local_decls[*local].is_user_variable.is_some() => { }) if self.mir.local_decls[*local].is_user_variable.is_some() => {
let local_decl = &self.mir.local_decls[*local]; let local_decl = &self.mir.local_decls[*local];
let (err_help_span, suggested_code) = match local_decl.is_user_variable { let suggestion = match local_decl.is_user_variable {
Some(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf)) => { Some(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf)) => {
suggest_ampmut_self(local_decl) Some(suggest_ampmut_self(local_decl))
}, },
Some(ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm { Some(ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm {
@ -1854,9 +1854,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
if let Some(x) = try_suggest_ampmut_rhs( if let Some(x) = try_suggest_ampmut_rhs(
self.tcx, self.mir, *local, self.tcx, self.mir, *local,
) { ) {
x Some(x)
} else { } else {
suggest_ampmut_type(local_decl, opt_ty_info) Some(suggest_ampmut_type(local_decl, opt_ty_info))
} }
}, },
@ -1872,11 +1872,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
None => bug!(), None => bug!(),
}; };
if let Some((err_help_span, suggested_code)) = suggestion {
err.span_suggestion( err.span_suggestion(
err_help_span, err_help_span,
"consider changing this to be a mutable reference", "consider changing this to be a mutable reference",
suggested_code, suggested_code,
); );
}
if let Some(name) = local_decl.name { if let Some(name) = local_decl.name {
err.span_label( err.span_label(
@ -1967,13 +1969,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
fn suggest_ref_mut<'cx, 'gcx, 'tcx>( fn suggest_ref_mut<'cx, 'gcx, 'tcx>(
tcx: TyCtxt<'cx, 'gcx, 'tcx>, tcx: TyCtxt<'cx, 'gcx, 'tcx>,
local_decl: &mir::LocalDecl<'tcx>, local_decl: &mir::LocalDecl<'tcx>,
) -> (Span, String) { ) -> Option<(Span, String)> {
let hi_span = local_decl.source_info.span; let hi_span = local_decl.source_info.span;
let hi_src = tcx.sess.codemap().span_to_snippet(hi_span).unwrap(); let hi_src = tcx.sess.codemap().span_to_snippet(hi_span).unwrap();
assert!(hi_src.starts_with("ref")); if hi_src.starts_with("ref")
assert!(hi_src["ref".len()..].starts_with(Pattern_White_Space)); && hi_src["ref".len()..].starts_with(Pattern_White_Space)
{
let suggestion = format!("ref mut{}", &hi_src["ref".len()..]); let suggestion = format!("ref mut{}", &hi_src["ref".len()..]);
(hi_span, suggestion) Some((hi_span, suggestion))
} else {
None
}
} }
} }

View File

@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:19:5 --> $DIR/enum.rs:19:5
| |
LL | *x += 1; //~ ERROR cannot assign to immutable LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*x` which is behind a `&` reference error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:23:9 --> $DIR/enum.rs:23:9
| |
LL | *x += 1; //~ ERROR cannot assign to immutable LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*x` which is behind a `&` reference error[E0594]: cannot assign to `*x` which is behind a `&` reference
--> $DIR/enum.rs:29:9 --> $DIR/enum.rs:29:9
| |
LL | *x += 1; //~ ERROR cannot assign to immutable LL | *x += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:17:13 --> $DIR/explicit-mut.rs:17:13
| |
LL | *n += 1; //~ ERROR cannot assign to immutable LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*n` which is behind a `&` reference error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:25:13 --> $DIR/explicit-mut.rs:25:13
| |
LL | *n += 1; //~ ERROR cannot assign to immutable LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*n` which is behind a `&` reference error[E0594]: cannot assign to `*n` which is behind a `&` reference
--> $DIR/explicit-mut.rs:33:13 --> $DIR/explicit-mut.rs:33:13
| |
LL | *n += 1; //~ ERROR cannot assign to immutable LL | *n += 1; //~ ERROR cannot assign to immutable
| ^^^^^^^ cannot assign | ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
error: aborting due to 3 previous errors error: aborting due to 3 previous errors