diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-06-06 21:06:49 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-07-03 22:25:36 +0200 |
| commit | a43bea101621d6ecbf2b33f5fcf122819115171d (patch) | |
| tree | e0d41ff62bc1b99e1324046b74ef81be44e2936c | |
| parent | b41fc6784fc723ea52da0e0668fc5205b740f81a (diff) | |
| download | rust-a43bea101621d6ecbf2b33f5fcf122819115171d.tar.gz rust-a43bea101621d6ecbf2b33f5fcf122819115171d.zip | |
Add UI test for `needless_pass_by_ref_mut`
| -rw-r--r-- | tests/ui/needless_pass_by_ref_mut.rs | 105 | ||||
| -rw-r--r-- | tests/ui/needless_pass_by_ref_mut.stderr | 28 |
2 files changed, 133 insertions, 0 deletions
diff --git a/tests/ui/needless_pass_by_ref_mut.rs b/tests/ui/needless_pass_by_ref_mut.rs new file mode 100644 index 00000000000..5e7280995c6 --- /dev/null +++ b/tests/ui/needless_pass_by_ref_mut.rs @@ -0,0 +1,105 @@ +#![allow(unused)] + +use std::ptr::NonNull; + +// Should only warn for `s`. +fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) { + *x += *b + s.len() as u32; +} + +// Should not warn. +fn foo2(s: &mut Vec<u32>) { + s.push(8); +} + +// Should not warn because we return it. +fn foo3(s: &mut Vec<u32>) -> &mut Vec<u32> { + s +} + +// Should not warn because `s` is used as mutable. +fn foo4(s: &mut Vec<u32>) { + Vec::push(s, 4); +} + +// Should not warn. +fn foo5(s: &mut Vec<u32>) { + foo2(s); +} + +// Should warn. +fn foo6(s: &mut Vec<u32>) { + non_mut_ref(s); +} + +fn non_mut_ref(_: &Vec<u32>) {} + +struct Bar; + +impl Bar { + // Should not warn on `&mut self`. + fn bar(&mut self) {} + + // Should warn about `vec` + fn mushroom(&self, vec: &mut Vec<i32>) -> usize { + vec.len() + } + + // Should warn about `vec` (and not `self`). + fn badger(&mut self, vec: &mut Vec<i32>) -> usize { + vec.len() + } +} + +trait Babar { + // Should not warn here since it's a trait method. + fn method(arg: &mut u32); +} + +impl Babar for Bar { + // Should not warn here since it's a trait method. + fn method(a: &mut u32) {} +} + +// Should not warn (checking variable aliasing). +fn alias_check(s: &mut Vec<u32>) { + let mut alias = s; + let mut alias2 = alias; + let mut alias3 = alias2; + alias3.push(0); +} + +// Should not warn (checking variable aliasing). +fn alias_check2(mut s: &mut Vec<u32>) { + let mut alias = &mut s; + alias.push(0); +} + +struct Mut<T> { + ptr: NonNull<T>, +} + +impl<T> Mut<T> { + // Should not warn because `NonNull::from` also accepts `&mut`. + fn new(ptr: &mut T) -> Self { + Mut { + ptr: NonNull::from(ptr), + } + } +} + +// Should not warn. +fn unused(_: &mut u32, _b: &mut u8) {} + +fn main() { + let mut u = 0; + let mut v = vec![0]; + foo(&mut v, &0, &mut u); + foo2(&mut v); + foo3(&mut v); + foo4(&mut v); + foo5(&mut v); + alias_check(&mut v); + alias_check2(&mut v); + println!("{u}"); +} diff --git a/tests/ui/needless_pass_by_ref_mut.stderr b/tests/ui/needless_pass_by_ref_mut.stderr new file mode 100644 index 00000000000..5e9d80bb6c4 --- /dev/null +++ b/tests/ui/needless_pass_by_ref_mut.stderr @@ -0,0 +1,28 @@ +error: this argument is a mutable reference, but not used mutably + --> $DIR/needless_pass_by_ref_mut.rs:6:11 + | +LL | fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) { + | ^^^^^^^^^^^^^ help: consider changing to: `&Vec<u32>` + | + = note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings` + +error: this argument is a mutable reference, but not used mutably + --> $DIR/needless_pass_by_ref_mut.rs:31:12 + | +LL | fn foo6(s: &mut Vec<u32>) { + | ^^^^^^^^^^^^^ help: consider changing to: `&Vec<u32>` + +error: this argument is a mutable reference, but not used mutably + --> $DIR/needless_pass_by_ref_mut.rs:44:29 + | +LL | fn mushroom(&self, vec: &mut Vec<i32>) -> usize { + | ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>` + +error: this argument is a mutable reference, but not used mutably + --> $DIR/needless_pass_by_ref_mut.rs:49:31 + | +LL | fn badger(&mut self, vec: &mut Vec<i32>) -> usize { + | ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>` + +error: aborting due to 4 previous errors + |
