about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEzra Shaw <ezrasure@outlook.com>2023-04-20 18:58:43 +1200
committerEzra Shaw <ezrashawdev@gmail.com>2023-05-05 22:40:05 +1200
commit87a1b3840ecfebdcd22313ed37f0609732d8cf83 (patch)
treefa3adc2fb1a740f58b751d3ea3d81103593466f8
parent336a6569f5477d36151500bbd7c57e3bba357cec (diff)
downloadrust-87a1b3840ecfebdcd22313ed37f0609732d8cf83.tar.gz
rust-87a1b3840ecfebdcd22313ed37f0609732d8cf83.zip
tweak spans for `ref mut` suggestion
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs16
-rw-r--r--tests/ui/nll/issue-51244.stderr2
-rw-r--r--tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr4
-rw-r--r--tests/ui/suggestions/suggest-ref-mut.rs3
-rw-r--r--tests/ui/suggestions/suggest-ref-mut.stderr12
5 files changed, 18 insertions, 19 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
index e842afced81..0a89eb07245 100644
--- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
@@ -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
     }
diff --git a/tests/ui/nll/issue-51244.stderr b/tests/ui/nll/issue-51244.stderr
index 03d8acc8188..8ccb5809e39 100644
--- a/tests/ui/nll/issue-51244.stderr
+++ b/tests/ui/nll/issue-51244.stderr
@@ -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
 
diff --git a/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr b/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr
index c7c7c074f7c..a033cc0655e 100644
--- a/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr
+++ b/tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr
@@ -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
diff --git a/tests/ui/suggestions/suggest-ref-mut.rs b/tests/ui/suggestions/suggest-ref-mut.rs
index d04113ffccc..b40439b8e37 100644
--- a/tests/ui/suggestions/suggest-ref-mut.rs
+++ b/tests/ui/suggestions/suggest-ref-mut.rs
@@ -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
     }
 }
diff --git a/tests/ui/suggestions/suggest-ref-mut.stderr b/tests/ui/suggestions/suggest-ref-mut.stderr
index 7973759bf5e..cc00022ab8e 100644
--- a/tests/ui/suggestions/suggest-ref-mut.stderr
+++ b/tests/ui/suggestions/suggest-ref-mut.stderr
@@ -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