Skip to content

Commit 6bb17f7

Browse files
Add UI test for needless_pass_by_ref_mut
1 parent 946f098 commit 6bb17f7

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

tests/ui/needless_pass_by_ref_mut.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: this argument is a mutable reference, but not used mutably
2+
--> $DIR/needless_pass_by_ref_mut.rs:6: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:31:12
11+
|
12+
LL | fn foo6(s: &mut Vec<u32>) {
13+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<u32>`
14+
15+
error: this argument is a mutable reference, but not used mutably
16+
--> $DIR/needless_pass_by_ref_mut.rs:44:29
17+
|
18+
LL | fn mushroom(&self, vec: &mut Vec<i32>) -> usize {
19+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>`
20+
21+
error: this argument is a mutable reference, but not used mutably
22+
--> $DIR/needless_pass_by_ref_mut.rs:49:31
23+
|
24+
LL | fn badger(&mut self, vec: &mut Vec<i32>) -> usize {
25+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<i32>`
26+
27+
error: aborting due to 4 previous errors
28+

0 commit comments

Comments
 (0)