Skip to content

Commit 7e82b39

Browse files
committed
Auto merge of #29738 - sanxiyn:suggest-mut, r=nrc
Fix #16410.
2 parents dbb7854 + 0b3394a commit 7e82b39

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,18 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10151015
self by mutable reference");
10161016
}
10171017
}
1018-
_ => {}
1018+
_ => {
1019+
if let Categorization::Local(local_id) = err.cmt.cat {
1020+
let span = self.tcx.map.span(local_id);
1021+
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
1022+
self.tcx.sess.span_suggestion(
1023+
span,
1024+
&format!("to make the {} mutable, use `mut` as shown:",
1025+
self.cmt_to_string(&err.cmt)),
1026+
format!("mut {}", snippet));
1027+
}
1028+
}
1029+
}
10191030
}
10201031
}
10211032

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[derive(Copy, Clone)]
12+
struct S;
13+
14+
impl S {
15+
fn mutate(&mut self) {
16+
}
17+
}
18+
19+
fn func(arg: S) {
20+
//~^ HELP use `mut` as shown
21+
//~| SUGGESTION fn func(mut arg: S) {
22+
arg.mutate(); //~ ERROR cannot borrow immutable argument
23+
}
24+
25+
fn main() {
26+
let local = S;
27+
//~^ HELP use `mut` as shown
28+
//~| SUGGESTION let mut local = S;
29+
local.mutate(); //~ ERROR cannot borrow immutable local variable
30+
}

0 commit comments

Comments
 (0)