diff options
| author | bors <bors@rust-lang.org> | 2017-08-10 08:53:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-08-10 08:53:22 +0000 |
| commit | d21ec9b4efd1da012979b050bc0a0426fe45fcdf (patch) | |
| tree | b33492318dcad1da9a032327304bd1b972d2a947 /src/libcore | |
| parent | 2ac5f7d249e29ee48737359e0e6dd9e59701a568 (diff) | |
| parent | 8f78d453ded152e95d66f113d24287f12c680a15 (diff) | |
| download | rust-d21ec9b4efd1da012979b050bc0a0426fe45fcdf.tar.gz rust-d21ec9b4efd1da012979b050bc0a0426fe45fcdf.zip | |
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.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ops/function.rs | 2 | ||||
| -rw-r--r-- | src/libcore/option.rs | 2 | ||||
| -rw-r--r-- | src/libcore/result.rs | 2 | ||||
| -rw-r--r-- | src/libcore/tests/slice.rs | 12 |
4 files changed, 9 insertions, 9 deletions
diff --git a/src/libcore/ops/function.rs b/src/libcore/ops/function.rs index 62bf69336a3..c5b3fbca1a6 100644 --- a/src/libcore/ops/function.rs +++ b/src/libcore/ops/function.rs @@ -187,7 +187,7 @@ mod impls { where F : FnMut<A> { type Output = F::Output; - extern "rust-call" fn call_once(mut self, args: A) -> F::Output { + extern "rust-call" fn call_once(self, args: A) -> F::Output { (*self).call_mut(args) } } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index ef41b679410..aecf2ee9325 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -872,7 +872,7 @@ impl<'a, T> IntoIterator for &'a mut Option<T> { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; - fn into_iter(mut self) -> IterMut<'a, T> { + fn into_iter(self) -> IterMut<'a, T> { self.iter_mut() } } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 88a93492de9..20cfb02afcc 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -909,7 +909,7 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; - fn into_iter(mut self) -> IterMut<'a, T> { + fn into_iter(self) -> IterMut<'a, T> { self.iter_mut() } } diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index e5d6b53b570..8c31d2e83d3 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -105,27 +105,27 @@ fn test_chunks_last() { #[test] fn test_chunks_mut_count() { - let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; + let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; let c = v.chunks_mut(3); assert_eq!(c.count(), 2); - let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4]; + let v2: &mut [i32] = &mut [0, 1, 2, 3, 4]; let c2 = v2.chunks_mut(2); assert_eq!(c2.count(), 3); - let mut v3: &mut [i32] = &mut []; + let v3: &mut [i32] = &mut []; let c3 = v3.chunks_mut(2); assert_eq!(c3.count(), 0); } #[test] fn test_chunks_mut_nth() { - let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; + let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; let mut c = v.chunks_mut(2); assert_eq!(c.nth(1).unwrap()[1], 3); assert_eq!(c.next().unwrap()[0], 4); - let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4]; + let v2: &mut [i32] = &mut [0, 1, 2, 3, 4]; let mut c2 = v2.chunks_mut(3); assert_eq!(c2.nth(1).unwrap()[1], 4); assert_eq!(c2.next(), None); @@ -194,7 +194,7 @@ fn get_range() { #[test] fn get_mut_range() { - let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; + let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; assert_eq!(v.get_mut(..), Some(&mut [0, 1, 2, 3, 4, 5][..])); assert_eq!(v.get_mut(..2), Some(&mut [0, 1][..])); assert_eq!(v.get_mut(2..), Some(&mut [2, 3, 4, 5][..])); |
