tweak spans for `ref mut` suggestion

This commit is contained in:
Ezra Shaw 2023-04-20 18:58:43 +12:00 committed by Ezra Shaw
parent 336a6569f5
commit 87a1b3840e
No known key found for this signature in database
GPG Key ID: 5156CF5845150B0D
5 changed files with 18 additions and 19 deletions

View File

@ -559,9 +559,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
binding_mode: ty::BindingMode::BindByReference(_),
..
})) => {
let pattern_span = local_decl.source_info.span;
let pattern_span: Span = local_decl.source_info.span;
suggest_ref_mut(self.infcx.tcx, pattern_span)
.map(|replacement| (true, pattern_span, replacement))
.map(|span| (true, span, "mut ".to_owned()))
}
_ => unreachable!(),
@ -1316,11 +1316,13 @@ fn get_mut_span_in_struct_field<'tcx>(
}
/// If possible, suggest replacing `ref` with `ref mut`.
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
if hi_src.starts_with("ref") && hi_src["ref".len()..].starts_with(rustc_lexer::is_whitespace) {
let replacement = format!("ref mut{}", &hi_src["ref".len()..]);
Some(replacement)
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
if pattern_str.starts_with("ref")
&& pattern_str["ref".len()..].starts_with(rustc_lexer::is_whitespace)
{
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
Some(span)
} else {
None
}

View File

@ -7,7 +7,7 @@ LL | *my_ref = 0;
help: consider changing this to be a mutable reference
|
LL | let ref mut my_ref @ _ = 0;
| ~~~~~~~~~~~~~~
| +++
error: aborting due to previous error

View File

@ -112,7 +112,7 @@ LL | *_x0 = U;
help: consider changing this to be a mutable reference
|
LL | let (ref mut _x0, _x1, ref _x2, ..) = tup;
| ~~~~~~~~~~~
| +++
error[E0594]: cannot assign to `*_x2`, which is behind a `&` reference
--> $DIR/borrowck-move-ref-pattern.rs:27:5
@ -123,7 +123,7 @@ LL | *_x2 = U;
help: consider changing this to be a mutable reference
|
LL | let (ref _x0, _x1, ref mut _x2, ..) = tup;
| ~~~~~~~~~~~
| +++
error[E0382]: use of moved value: `tup.1`
--> $DIR/borrowck-move-ref-pattern.rs:28:10

View File

@ -12,12 +12,10 @@ impl X {
fn main() {
let ref foo = 16;
//~^ HELP
//~| SUGGESTION ref mut foo
*foo = 32;
//~^ ERROR
if let Some(ref bar) = Some(16) {
//~^ HELP
//~| SUGGESTION ref mut bar
*bar = 32;
//~^ ERROR
}
@ -25,6 +23,5 @@ fn main() {
ref quo => { *quo = 32; },
//~^ ERROR
//~| HELP
//~| SUGGESTION ref mut quo
}
}

View File

@ -10,7 +10,7 @@ LL | fn zap(&mut self) {
| ~~~~~~~~~
error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:16:5
--> $DIR/suggest-ref-mut.rs:15:5
|
LL | *foo = 32;
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
@ -18,10 +18,10 @@ LL | *foo = 32;
help: consider changing this to be a mutable reference
|
LL | let ref mut foo = 16;
| ~~~~~~~~~~~
| +++
error[E0594]: cannot assign to `*bar`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:21:9
--> $DIR/suggest-ref-mut.rs:19:9
|
LL | *bar = 32;
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
@ -29,10 +29,10 @@ LL | *bar = 32;
help: consider changing this to be a mutable reference
|
LL | if let Some(ref mut bar) = Some(16) {
| ~~~~~~~~~~~
| +++
error[E0594]: cannot assign to `*quo`, which is behind a `&` reference
--> $DIR/suggest-ref-mut.rs:25:22
--> $DIR/suggest-ref-mut.rs:23:22
|
LL | ref quo => { *quo = 32; },
| ^^^^^^^^^ `quo` is a `&` reference, so the data it refers to cannot be written
@ -40,7 +40,7 @@ LL | ref quo => { *quo = 32; },
help: consider changing this to be a mutable reference
|
LL | ref mut quo => { *quo = 32; },
| ~~~~~~~~~~~
| +++
error: aborting due to 4 previous errors