Skip to content

Commit fef8276

Browse files
committed
borrowck: do not suggest to change "&mut self" to "&mut mut self"
Matching the snippet string might not be the cleanest, but matching the AST node instead seems to end in a lot of nested `if let`s... Fixes rust-lang#31424.
1 parent a4d2424 commit fef8276

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/librustc_borrowck/borrowck/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,13 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
970970
if let Categorization::Local(local_id) = err.cmt.cat {
971971
let span = self.tcx.map.span(local_id);
972972
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
973-
db.span_suggestion(
974-
span,
975-
&format!("to make the {} mutable, use `mut` as shown:",
976-
self.cmt_to_string(&err.cmt)),
977-
format!("mut {}", snippet));
973+
if snippet != "self" {
974+
db.span_suggestion(
975+
span,
976+
&format!("to make the {} mutable, use `mut` as shown:",
977+
self.cmt_to_string(&err.cmt)),
978+
format!("mut {}", snippet));
979+
}
978980
}
979981
}
980982
}

src/test/compile-fail/issue-31424.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 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+
// forbid-output: &mut mut self
12+
13+
struct Struct;
14+
15+
impl Struct {
16+
fn foo(&mut self) {
17+
(&mut self).bar();
18+
//~^ ERROR cannot borrow immutable argument `self` as mutable
19+
// ... and no SUGGESTION that suggests `&mut mut self`
20+
}
21+
22+
// In this case we could keep the suggestion, but to distinguish the
23+
// two cases is pretty hard. It's an obscure case anyway.
24+
fn bar(self: &mut Self) {
25+
(&mut self).bar();
26+
//~^ ERROR cannot borrow immutable argument `self` as mutable
27+
}
28+
}
29+
30+
fn main () {}

0 commit comments

Comments
 (0)