Skip to content

Commit 33ff8fc

Browse files
committed
restructure some code and add first test
1 parent 3f1d055 commit 33ff8fc

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,15 +1191,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
11911191
if let ty::BindByValue(bind_value_mut) = self.local_binding_mode(node_id) {
11921192
let (local_node_ty, is_implicit_self) = self.local_ty(node_id);
11931193
if let Some(local_node_ty) = local_node_ty {
1194-
if let hir::Ty_::TyPtr(ref ty_ptr_mutability) = local_node_ty.node {
1195-
if ty_ptr_mutability.mutbl == bind_value_mut {
1196-
if let Ok(snippet) =self.tcx.sess.codemap().span_to_snippet(let_span) {
1194+
match (local_node_ty.node, self.tcx.sess.codemap().span_to_snippet(let_span)) {
1195+
(hir::Ty_::TyPtr(ref ty_ptr_mutability), Ok(snippet)) if ty_ptr_mutability.mutbl == bind_value_mut => {
11971196
db.span_suggestion(
11981197
error_span,
11991198
"consider removing the `&mut`, as it is an immutable binding to a mutable reference",
12001199
snippet);
1201-
}
1202-
}
1200+
},
1201+
_ => {}
12031202
}
12041203
} else if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
12051204
if is_implicit_self && snippet != "self" {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct User {
2+
name: String,
3+
}
4+
5+
fn main() {
6+
let mut user1 = User{
7+
name: String::from("Kurtis"),
8+
};
9+
mutator_part1(&mut user1);
10+
}
11+
12+
fn mutator_part1(user: &mut User) {
13+
mutator_part2(&mut user);
14+
}
15+
16+
fn mutator_part2(user: &mut User) {
17+
user.name = String::from("Steve");
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0596]: cannot borrow immutable argument `user` as mutable
2+
--> unnecessary_mut_reference.rs:13:24
3+
|
4+
12 | fn mutator_part1(user: &mut User) {
5+
13 | mutator_part2(&mut user);
6+
| ^^^^ cannot borrow mutably
7+
| --------- consider removing the `&mut`, as it is an immutable binding to a mutable reference
8+
9+
error: aborting due to previous error

0 commit comments

Comments
 (0)