Skip to content

Commit fac1b21

Browse files
Add UI test for needless_pass_by_ref_mut
1 parent ac10c03 commit fac1b21

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

tests/ui/needless_pass_by_ref_mut.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: this argument is a mutable reference, but not used mutably
2+
--> $DIR/needless_pass_by_ref_mut.rs:4:11
3+
|
4+
LL | fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) {
5+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<u32>`
6+
|
7+
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
8+
9+
error: this argument is a mutable reference, but not used mutably
10+
--> $DIR/needless_pass_by_ref_mut.rs:30:29
11+
|
12+
LL | fn mushroom(&self, vec: &mut Vec<i32>) -> usize {
13+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>`
14+
15+
error: this argument is a mutable reference, but not used mutably
16+
--> $DIR/needless_pass_by_ref_mut.rs:35:31
17+
|
18+
LL | fn badger(&mut self, vec: &mut Vec<i32>) -> usize {
19+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)