Skip to content

Commit d413caa

Browse files
committed
Auto merge of #4567 - phansch:toplevel_ref_arg, r=flip1995
Add run-rustfix for toplevel_ref_arg lint cc #3630
2 parents 4d566b6 + df83732 commit d413caa

5 files changed

+56
-20
lines changed

tests/ui/toplevel_ref_arg.fixed

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

tests/ui/toplevel_ref_arg.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#![warn(clippy::all)]
2-
#![allow(unused)]
1+
// run-rustfix
32

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

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

tests/ui/toplevel_ref_arg.stderr

+7-13
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:11: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:13: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:15: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:17: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

+11
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+
}
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)