Skip to content

Commit 57d603e

Browse files
committed
tweak spans for ref mut suggestion
1 parent 7a90241 commit 57d603e

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
559559
binding_mode: ty::BindingMode::BindByReference(_),
560560
..
561561
})) => {
562-
let pattern_span = local_decl.source_info.span;
562+
let pattern_span: Span = local_decl.source_info.span;
563563
suggest_ref_mut(self.infcx.tcx, pattern_span)
564-
.map(|replacement| (true, pattern_span, replacement))
564+
.map(|span| (true, span, "mut ".to_owned()))
565565
}
566566

567567
_ => unreachable!(),
@@ -1316,11 +1316,13 @@ fn get_mut_span_in_struct_field<'tcx>(
13161316
}
13171317

13181318
/// If possible, suggest replacing `ref` with `ref mut`.
1319-
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
1320-
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
1321-
if hi_src.starts_with("ref") && hi_src["ref".len()..].starts_with(rustc_lexer::is_whitespace) {
1322-
let replacement = format!("ref mut{}", &hi_src["ref".len()..]);
1323-
Some(replacement)
1319+
fn suggest_ref_mut(tcx: TyCtxt<'_>, span: Span) -> Option<Span> {
1320+
let pattern_str = tcx.sess.source_map().span_to_snippet(span).ok()?;
1321+
if pattern_str.starts_with("ref")
1322+
&& pattern_str["ref".len()..].starts_with(rustc_lexer::is_whitespace)
1323+
{
1324+
let span = span.with_lo(span.lo() + BytePos(4)).shrink_to_lo();
1325+
Some(span)
13241326
} else {
13251327
None
13261328
}

tests/ui/nll/issue-51244.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | *my_ref = 0;
77
help: consider changing this to be a mutable reference
88
|
99
LL | let ref mut my_ref @ _ = 0;
10-
| ~~~~~~~~~~~~~~
10+
| +++
1111

1212
error: aborting due to previous error
1313

tests/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ LL | *_x0 = U;
112112
help: consider changing this to be a mutable reference
113113
|
114114
LL | let (ref mut _x0, _x1, ref _x2, ..) = tup;
115-
| ~~~~~~~~~~~
115+
| +++
116116

117117
error[E0594]: cannot assign to `*_x2`, which is behind a `&` reference
118118
--> $DIR/borrowck-move-ref-pattern.rs:27:5
@@ -123,7 +123,7 @@ LL | *_x2 = U;
123123
help: consider changing this to be a mutable reference
124124
|
125125
LL | let (ref _x0, _x1, ref mut _x2, ..) = tup;
126-
| ~~~~~~~~~~~
126+
| +++
127127

128128
error[E0382]: use of moved value: `tup.1`
129129
--> $DIR/borrowck-move-ref-pattern.rs:28:10

tests/ui/suggestions/suggest-ref-mut.rs

-3
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ impl X {
1212
fn main() {
1313
let ref foo = 16;
1414
//~^ HELP
15-
//~| SUGGESTION ref mut foo
1615
*foo = 32;
1716
//~^ ERROR
1817
if let Some(ref bar) = Some(16) {
1918
//~^ HELP
20-
//~| SUGGESTION ref mut bar
2119
*bar = 32;
2220
//~^ ERROR
2321
}
2422
match 16 {
2523
ref quo => { *quo = 32; },
2624
//~^ ERROR
2725
//~| HELP
28-
//~| SUGGESTION ref mut quo
2926
}
3027
}

tests/ui/suggestions/suggest-ref-mut.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ LL | fn zap(&mut self) {
1010
| ~~~~~~~~~
1111

1212
error[E0594]: cannot assign to `*foo`, which is behind a `&` reference
13-
--> $DIR/suggest-ref-mut.rs:16:5
13+
--> $DIR/suggest-ref-mut.rs:15:5
1414
|
1515
LL | *foo = 32;
1616
| ^^^^^^^^^ `foo` is a `&` reference, so the data it refers to cannot be written
1717
|
1818
help: consider changing this to be a mutable reference
1919
|
2020
LL | let ref mut foo = 16;
21-
| ~~~~~~~~~~~
21+
| +++
2222

2323
error[E0594]: cannot assign to `*bar`, which is behind a `&` reference
24-
--> $DIR/suggest-ref-mut.rs:21:9
24+
--> $DIR/suggest-ref-mut.rs:19:9
2525
|
2626
LL | *bar = 32;
2727
| ^^^^^^^^^ `bar` is a `&` reference, so the data it refers to cannot be written
2828
|
2929
help: consider changing this to be a mutable reference
3030
|
3131
LL | if let Some(ref mut bar) = Some(16) {
32-
| ~~~~~~~~~~~
32+
| +++
3333

3434
error[E0594]: cannot assign to `*quo`, which is behind a `&` reference
35-
--> $DIR/suggest-ref-mut.rs:25:22
35+
--> $DIR/suggest-ref-mut.rs:23:22
3636
|
3737
LL | ref quo => { *quo = 32; },
3838
| ^^^^^^^^^ `quo` is a `&` reference, so the data it refers to cannot be written
3939
|
4040
help: consider changing this to be a mutable reference
4141
|
4242
LL | ref mut quo => { *quo = 32; },
43-
| ~~~~~~~~~~~
43+
| +++
4444

4545
error: aborting due to 4 previous errors
4646

0 commit comments

Comments
 (0)