Skip to content

Commit d21ec9b

Browse files
committed
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
}

src/liballoc/vec_deque.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ impl<'a, T> IntoIterator for &'a mut VecDeque<T> {
23942394
type Item = &'a mut T;
23952395
type IntoIter = IterMut<'a, T>;
23962396

2397-
fn into_iter(mut self) -> IterMut<'a, T> {
2397+
fn into_iter(self) -> IterMut<'a, T> {
23982398
self.iter_mut()
23992399
}
24002400
}
@@ -2558,7 +2558,7 @@ impl<'a, T> Place<T> for PlaceBack<'a, T> {
25582558
impl<'a, T> InPlace<T> for PlaceBack<'a, T> {
25592559
type Owner = &'a mut T;
25602560

2561-
unsafe fn finalize(mut self) -> &'a mut T {
2561+
unsafe fn finalize(self) -> &'a mut T {
25622562
let head = self.vec_deque.head;
25632563
self.vec_deque.head = self.vec_deque.wrap_add(head, 1);
25642564
&mut *(self.vec_deque.ptr().offset(head as isize))
@@ -2605,7 +2605,7 @@ impl<'a, T> Place<T> for PlaceFront<'a, T> {
26052605
impl<'a, T> InPlace<T> for PlaceFront<'a, T> {
26062606
type Owner = &'a mut T;
26072607

2608-
unsafe fn finalize(mut self) -> &'a mut T {
2608+
unsafe fn finalize(self) -> &'a mut T {
26092609
self.vec_deque.tail = self.vec_deque.wrap_sub(self.vec_deque.tail, 1);
26102610
&mut *(self.vec_deque.ptr().offset(self.vec_deque.tail as isize))
26112611
}

src/libcore/ops/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ mod impls {
187187
where F : FnMut<A>
188188
{
189189
type Output = F::Output;
190-
extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
190+
extern "rust-call" fn call_once(self, args: A) -> F::Output {
191191
(*self).call_mut(args)
192192
}
193193
}

src/libcore/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl<'a, T> IntoIterator for &'a mut Option<T> {
872872
type Item = &'a mut T;
873873
type IntoIter = IterMut<'a, T>;
874874

875-
fn into_iter(mut self) -> IterMut<'a, T> {
875+
fn into_iter(self) -> IterMut<'a, T> {
876876
self.iter_mut()
877877
}
878878
}

src/libcore/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
909909
type Item = &'a mut T;
910910
type IntoIter = IterMut<'a, T>;
911911

912-
fn into_iter(mut self) -> IterMut<'a, T> {
912+
fn into_iter(self) -> IterMut<'a, T> {
913913
self.iter_mut()
914914
}
915915
}

src/libcore/tests/slice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,27 @@ fn test_chunks_last() {
105105

106106
#[test]
107107
fn test_chunks_mut_count() {
108-
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
108+
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
109109
let c = v.chunks_mut(3);
110110
assert_eq!(c.count(), 2);
111111

112-
let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
112+
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
113113
let c2 = v2.chunks_mut(2);
114114
assert_eq!(c2.count(), 3);
115115

116-
let mut v3: &mut [i32] = &mut [];
116+
let v3: &mut [i32] = &mut [];
117117
let c3 = v3.chunks_mut(2);
118118
assert_eq!(c3.count(), 0);
119119
}
120120

121121
#[test]
122122
fn test_chunks_mut_nth() {
123-
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
123+
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
124124
let mut c = v.chunks_mut(2);
125125
assert_eq!(c.nth(1).unwrap()[1], 3);
126126
assert_eq!(c.next().unwrap()[0], 4);
127127

128-
let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
128+
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
129129
let mut c2 = v2.chunks_mut(3);
130130
assert_eq!(c2.nth(1).unwrap()[1], 4);
131131
assert_eq!(c2.next(), None);
@@ -194,7 +194,7 @@ fn get_range() {
194194

195195
#[test]
196196
fn get_mut_range() {
197-
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
197+
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
198198
assert_eq!(v.get_mut(..), Some(&mut [0, 1, 2, 3, 4, 5][..]));
199199
assert_eq!(v.get_mut(..2), Some(&mut [0, 1][..]));
200200
assert_eq!(v.get_mut(2..), Some(&mut [2, 3, 4, 5][..]));

src/librustc/infer/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
415415
/// -------- this type is the same as a type argument in the other type, not highlighted
416416
/// ```
417417
fn highlight_outer(&self,
418-
mut value: &mut DiagnosticStyledString,
419-
mut other_value: &mut DiagnosticStyledString,
418+
value: &mut DiagnosticStyledString,
419+
other_value: &mut DiagnosticStyledString,
420420
name: String,
421421
sub: &ty::subst::Substs<'tcx>,
422422
pos: usize,

src/librustc/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ pub fn fully_normalize<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
560560
{
561561
debug!("fully_normalize(value={:?})", value);
562562

563-
let mut selcx = &mut SelectionContext::new(infcx);
563+
let selcx = &mut SelectionContext::new(infcx);
564564
// FIXME (@jroesch) ISSUE 26721
565565
// I'm not sure if this is a bug or not, needs further investigation.
566566
// It appears that by reusing the fulfillment_cx here we incur more

src/librustc/traits/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
494494
never_obligation.predicate = never_obligation.predicate.map_bound(|mut trait_pred| {
495495
// Swap out () with ! so we can check if the trait is impld for !
496496
{
497-
let mut trait_ref = &mut trait_pred.trait_ref;
497+
let trait_ref = &mut trait_pred.trait_ref;
498498
let unit_substs = trait_ref.substs;
499499
let mut never_substs = Vec::with_capacity(unit_substs.len());
500500
never_substs.push(From::from(tcx.types.never));

0 commit comments

Comments
 (0)