|
| 1 | +#![allow(unused)] |
| 2 | + |
| 3 | +// Should only warn for `s`. |
| 4 | +fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) { |
| 5 | + *x += *b + s.len() as u32; |
| 6 | +} |
| 7 | + |
| 8 | +// Should not warn. |
| 9 | +fn foo2(s: &mut Vec<u32>) { |
| 10 | + s.push(8); |
| 11 | +} |
| 12 | + |
| 13 | +// Should not warn because we return it. |
| 14 | +fn foo3(s: &mut Vec<u32>) -> &mut Vec<u32> { |
| 15 | + s |
| 16 | +} |
| 17 | + |
| 18 | +// Should not warn because `s` is used as mutable. |
| 19 | +fn foo4(s: &mut Vec<u32>) { |
| 20 | + Vec::push(s, 4); |
| 21 | +} |
| 22 | + |
| 23 | +struct Bar; |
| 24 | + |
| 25 | +impl Bar { |
| 26 | + // Should not warn on `&mut self`. |
| 27 | + fn bar(&mut self) {} |
| 28 | + |
| 29 | + // Should warn about `vec` |
| 30 | + fn mushroom(&self, vec: &mut Vec<i32>) -> usize { |
| 31 | + vec.len() |
| 32 | + } |
| 33 | + |
| 34 | + // Should warn about `vec` (and not `self`). |
| 35 | + fn badger(&mut self, vec: &mut Vec<i32>) -> usize { |
| 36 | + vec.len() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +trait Babar { |
| 41 | + // Should not warn here since it's a trait method. |
| 42 | + fn method(arg: &mut u32); |
| 43 | +} |
| 44 | + |
| 45 | +impl Babar for Bar { |
| 46 | + // Should not warn here since it's a trait method. |
| 47 | + fn method(a: &mut u32) {} |
| 48 | +} |
| 49 | + |
| 50 | +// Should not warn (checking variable aliasing). |
| 51 | +fn alias_check(s: &mut Vec<u32>) { |
| 52 | + let mut alias = s; |
| 53 | + let mut alias2 = alias; |
| 54 | + let mut alias3 = alias2; |
| 55 | + alias3.push(0); |
| 56 | +} |
| 57 | + |
| 58 | +// Should not warn (checking variable aliasing). |
| 59 | +fn alias_check2(mut s: &mut Vec<u32>) { |
| 60 | + let mut alias = &mut s; |
| 61 | + alias.push(0); |
| 62 | +} |
| 63 | + |
| 64 | +fn main() { |
| 65 | + let mut u = 0; |
| 66 | + let mut v = vec![0]; |
| 67 | + foo(&mut v, &0, &mut u); |
| 68 | + foo2(&mut v); |
| 69 | + foo3(&mut v); |
| 70 | + alias_check(&mut v); |
| 71 | + alias_check2(&mut v); |
| 72 | + println!("{u}"); |
| 73 | +} |
0 commit comments