Skip to content

Commit d21ec9b

Browse files
committedAug 10, 2017
Auto merge of #43582 - ivanbakel:unused_mut_ref, r=arielb1
Fixed mutable vars being marked used when they weren't #### NB : bootstrapping is slow on my machine, even with `keep-stage` - fixes for occurances in the current codebase are <s>in the pipeline</s> done. This PR is being put up for review of the fix of the issue. Fixes #43526, Fixes #30280, Fixes #25049 ### Issue Whenever the compiler detected a mutable deref being used mutably, it marked an associated value as being used mutably as well. In the case of derefencing local variables which were mutable references, this incorrectly marked the reference itself being used mutably, instead of its contents - with the consequence of making the following code emit no warnings ``` fn do_thing<T>(mut arg : &mut T) { ... // don't touch arg - just deref it to access the T } ``` ### Fix Make dereferences not be counted as a mutable use, but only when they're on borrows on local variables. #### Why not on things other than local variables? * Whenever you capture a variable in a closure, it gets turned into a hidden reference - when you use it in the closure, it gets dereferenced. If the closure uses the variable mutably, that is actually a mutable use of the thing being dereffed to, so it has to be counted. * If you deref a mutable `Box` to access the contents mutably, you are using the `Box` mutably - so it has to be counted.
2 parents 2ac5f7d + 8f78d45 commit d21ec9b

File tree

42 files changed

+110
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+110
-70
lines changed
 

‎src/liballoc/btree/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ impl<'a, K: 'a, V: 'a, NodeType>
10371037
Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::KV> {
10381038

10391039
pub fn into_kv_mut(self) -> (&'a mut K, &'a mut V) {
1040-
let (mut keys, mut vals) = self.node.into_slices_mut();
1040+
let (keys, vals) = self.node.into_slices_mut();
10411041
unsafe {
10421042
(keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx))
10431043
}
@@ -1047,7 +1047,7 @@ impl<'a, K: 'a, V: 'a, NodeType>
10471047
impl<'a, K, V, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::KV> {
10481048
pub fn kv_mut(&mut self) -> (&mut K, &mut V) {
10491049
unsafe {
1050-
let (mut keys, mut vals) = self.node.reborrow_mut().into_slices_mut();
1050+
let (keys, vals) = self.node.reborrow_mut().into_slices_mut();
10511051
(keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx))
10521052
}
10531053
}

‎src/liballoc/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> {
17511751
type Item = &'a mut T;
17521752
type IntoIter = slice::IterMut<'a, T>;
17531753

1754-
fn into_iter(mut self) -> slice::IterMut<'a, T> {
1754+
fn into_iter(self) -> slice::IterMut<'a, T> {
17551755
self.iter_mut()
17561756
}
17571757
}

0 commit comments

Comments
 (0)