Skip to content

Commit b63e957

Browse files
committed
Add run-rustfix for toplevel_ref_arg lint
1 parent 0fcb49e commit b63e957

5 files changed

+54
-20
lines changed

tests/ui/toplevel_ref_arg.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-rustfix
2+
3+
#![allow(unused)]
4+
5+
fn main() {
6+
// Closures should not warn
7+
let y = |ref x| println!("{:?}", x);
8+
y(1u8);
9+
10+
let x = &1;
11+
12+
let y: &(&_, u8) = &(&1, 2);
13+
14+
let z = &(1 + 2);
15+
16+
let z = &mut (1 + 2);
17+
18+
let (ref x, _) = (1, 2); // ok, not top level
19+
println!("The answer is {}.", x);
20+
21+
// Make sure that allowing the lint works
22+
#[allow(clippy::toplevel_ref_arg)]
23+
let ref mut x = 1_234_543;
24+
}

tests/ui/toplevel_ref_arg.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
#![warn(clippy::all)]
2-
#![allow(unused)]
1+
// run-rustfix
32

4-
fn the_answer(ref mut x: u8) {
5-
*x = 42;
6-
}
3+
#![allow(unused)]
74

85
fn main() {
9-
let mut x = 0;
10-
the_answer(x);
116
// Closures should not warn
127
let y = |ref x| println!("{:?}", x);
138
y(1u8);

tests/ui/toplevel_ref_arg.stderr

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
error: `ref` directly on a function argument is ignored. Consider using a reference type instead.
2-
--> $DIR/toplevel_ref_arg.rs:4:15
3-
|
4-
LL | fn the_answer(ref mut x: u8) {
5-
| ^^^^^^^^^
6-
|
7-
= note: `-D clippy::toplevel-ref-arg` implied by `-D warnings`
8-
91
error: `ref` on an entire `let` pattern is discouraged, take a reference with `&` instead
10-
--> $DIR/toplevel_ref_arg.rs:15:9
2+
--> $DIR/toplevel_ref_arg.rs:10:9
113
|
124
LL | let ref x = 1;
135
| ----^^^^^----- help: try: `let x = &1;`
6+
|
7+
= note: `-D clippy::toplevel-ref-arg` implied by `-D warnings`
148

159
error: `ref` on an entire `let` pattern is discouraged, take a reference with `&` instead
16-
--> $DIR/toplevel_ref_arg.rs:17:9
10+
--> $DIR/toplevel_ref_arg.rs:12:9
1711
|
1812
LL | let ref y: (&_, u8) = (&1, 2);
1913
| ----^^^^^--------------------- help: try: `let y: &(&_, u8) = &(&1, 2);`
2014

2115
error: `ref` on an entire `let` pattern is discouraged, take a reference with `&` instead
22-
--> $DIR/toplevel_ref_arg.rs:19:9
16+
--> $DIR/toplevel_ref_arg.rs:14:9
2317
|
2418
LL | let ref z = 1 + 2;
2519
| ----^^^^^--------- help: try: `let z = &(1 + 2);`
2620

2721
error: `ref` on an entire `let` pattern is discouraged, take a reference with `&` instead
28-
--> $DIR/toplevel_ref_arg.rs:21:9
22+
--> $DIR/toplevel_ref_arg.rs:16:9
2923
|
3024
LL | let ref mut z = 1 + 2;
3125
| ----^^^^^^^^^--------- help: try: `let z = &mut (1 + 2);`
3226

33-
error: aborting due to 5 previous errors
27+
error: aborting due to 4 previous errors
3428

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::toplevel_ref_arg)]
2+
#![allow(unused)]
3+
4+
fn the_answer(ref mut x: u8) {
5+
*x = 42;
6+
}
7+
8+
fn main() {
9+
let mut x = 0;
10+
the_answer(x);
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `ref` directly on a function argument is ignored. Consider using a reference type instead.
2+
--> $DIR/toplevel_ref_arg_non_rustfix.rs:4:15
3+
|
4+
LL | fn the_answer(ref mut x: u8) {
5+
| ^^^^^^^^^
6+
|
7+
= note: `-D clippy::toplevel-ref-arg` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)