Skip to content

Commit 4ee1c39

Browse files
Add UI test for needless_pass_by_ref_mut
1 parent c0b9c74 commit 4ee1c39

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

tests/ui/needless_pass_by_ref_mut.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
30+
trait Babar {
31+
// Should not warn here since it's a trait method.
32+
fn method(arg: &mut u32);
33+
}
34+
35+
impl Babar for Bar {
36+
// Should not warn here since it's a trait method.
37+
fn method(a: &mut u32) {}
38+
}
39+
40+
fn main() {
41+
let mut u = 0;
42+
let mut v = vec![0];
43+
foo(&mut v, &0, &mut u);
44+
foo2(&mut v);
45+
foo3(&mut v);
46+
println!("{u}");
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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: aborting due to previous error
10+

0 commit comments

Comments
 (0)