|
| 1 | +#![allow(unused)] |
| 2 | + |
| 3 | +use std::ptr::NonNull; |
| 4 | + |
| 5 | +// Should only warn for `s`. |
| 6 | +fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) { |
| 7 | + *x += *b + s.len() as u32; |
| 8 | +} |
| 9 | + |
| 10 | +// Should not warn. |
| 11 | +fn foo2(s: &mut Vec<u32>) { |
| 12 | + s.push(8); |
| 13 | +} |
| 14 | + |
| 15 | +// Should not warn because we return it. |
| 16 | +fn foo3(s: &mut Vec<u32>) -> &mut Vec<u32> { |
| 17 | + s |
| 18 | +} |
| 19 | + |
| 20 | +// Should not warn because `s` is used as mutable. |
| 21 | +fn foo4(s: &mut Vec<u32>) { |
| 22 | + Vec::push(s, 4); |
| 23 | +} |
| 24 | + |
| 25 | +// Should not warn. |
| 26 | +fn foo5(s: &mut Vec<u32>) { |
| 27 | + foo2(s); |
| 28 | +} |
| 29 | + |
| 30 | +// Should warn. |
| 31 | +fn foo6(s: &mut Vec<u32>) { |
| 32 | + non_mut_ref(s); |
| 33 | +} |
| 34 | + |
| 35 | +fn non_mut_ref(_: &Vec<u32>) {} |
| 36 | + |
| 37 | +struct Bar; |
| 38 | + |
| 39 | +impl Bar { |
| 40 | + // Should not warn on `&mut self`. |
| 41 | + fn bar(&mut self) {} |
| 42 | + |
| 43 | + // Should warn about `vec` |
| 44 | + fn mushroom(&self, vec: &mut Vec<i32>) -> usize { |
| 45 | + vec.len() |
| 46 | + } |
| 47 | + |
| 48 | + // Should warn about `vec` (and not `self`). |
| 49 | + fn badger(&mut self, vec: &mut Vec<i32>) -> usize { |
| 50 | + vec.len() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +trait Babar { |
| 55 | + // Should not warn here since it's a trait method. |
| 56 | + fn method(arg: &mut u32); |
| 57 | +} |
| 58 | + |
| 59 | +impl Babar for Bar { |
| 60 | + // Should not warn here since it's a trait method. |
| 61 | + fn method(a: &mut u32) {} |
| 62 | +} |
| 63 | + |
| 64 | +// Should not warn (checking variable aliasing). |
| 65 | +fn alias_check(s: &mut Vec<u32>) { |
| 66 | + let mut alias = s; |
| 67 | + let mut alias2 = alias; |
| 68 | + let mut alias3 = alias2; |
| 69 | + alias3.push(0); |
| 70 | +} |
| 71 | + |
| 72 | +// Should not warn (checking variable aliasing). |
| 73 | +fn alias_check2(mut s: &mut Vec<u32>) { |
| 74 | + let mut alias = &mut s; |
| 75 | + alias.push(0); |
| 76 | +} |
| 77 | + |
| 78 | +struct Mut<T> { |
| 79 | + ptr: NonNull<T>, |
| 80 | +} |
| 81 | + |
| 82 | +impl<T> Mut<T> { |
| 83 | + // Should not warn because `NonNull::from` also accepts `&mut`. |
| 84 | + fn new(ptr: &mut T) -> Self { |
| 85 | + Mut { |
| 86 | + ptr: NonNull::from(ptr), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Should not warn. |
| 92 | +fn unused(_: &mut u32, _b: &mut u8) {} |
| 93 | + |
| 94 | +fn main() { |
| 95 | + let mut u = 0; |
| 96 | + let mut v = vec![0]; |
| 97 | + foo(&mut v, &0, &mut u); |
| 98 | + foo2(&mut v); |
| 99 | + foo3(&mut v); |
| 100 | + foo4(&mut v); |
| 101 | + foo5(&mut v); |
| 102 | + alias_check(&mut v); |
| 103 | + alias_check2(&mut v); |
| 104 | + println!("{u}"); |
| 105 | +} |
0 commit comments