Skip to content

Commit 3da9bbf

Browse files
committed
split into borrow_deref_ref.rs and borrow_deref_ref_unfixable.rs
1 parent 8430fa2 commit 3da9bbf

6 files changed

+92
-22
lines changed

tests/compile-test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ fn compile_test() {
346346

347347
const RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS: &[&str] = &[
348348
"assign_ops2.rs",
349+
"borrow_deref_ref_unfixable.rs",
349350
"cast_size_32bit.rs",
350351
"char_lit_as_u8.rs",
351352
"cmp_owned/without_suggestion.rs",

tests/ui/borrow_deref_ref.fixed

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// run-rustfix
2+
3+
#![allow(dead_code, unused_variables)]
4+
5+
fn main() {}
6+
7+
mod should_lint {
8+
fn one_suggestion() {
9+
let a = &12;
10+
let b = a;
11+
12+
let b = &mut bar(&12);
13+
}
14+
15+
fn bar(x: &u32) -> &u32 {
16+
x
17+
}
18+
}
19+
20+
// this mod explains why we should not lint `&mut &* (&T)`
21+
mod should_not_lint1 {
22+
fn foo(x: &mut &u32) {
23+
*x = &1;
24+
}
25+
26+
fn main() {
27+
let mut x = &0;
28+
foo(&mut &*x); // should not lint
29+
assert_eq!(*x, 0);
30+
31+
foo(&mut x);
32+
assert_eq!(*x, 1);
33+
}
34+
}
35+
36+
// similar to should_not_lint1
37+
mod should_not_lint2 {
38+
struct S<'a> {
39+
a: &'a u32,
40+
b: u32,
41+
}
42+
43+
fn main() {
44+
let s = S { a: &1, b: 1 };
45+
let x = &mut &*s.a;
46+
*x = &2;
47+
}
48+
}
49+
50+
// this mod explains why we should not lint `& &* (&T)`
51+
mod false_negative {
52+
fn foo() {
53+
let x = &12;
54+
let addr_x = &x as *const _ as usize;
55+
let addr_y = &x as *const _ as usize; // assert ok
56+
// let addr_y = &x as *const _ as usize; // assert fail
57+
assert_ne!(addr_x, addr_y);
58+
}
59+
}

tests/ui/borrow_deref_ref.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
fn main() {}
66

77
mod should_lint {
8-
fn foo() {
8+
fn one_suggestion() {
99
let a = &12;
1010
let b = &*a;
1111

12-
let s = &String::new();
13-
let x: &str = &*s;
14-
1512
let b = &mut &*bar(&12);
1613
}
1714

tests/ui/borrow_deref_ref.stderr

+3-18
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,16 @@ LL | let b = &*a;
77
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
88

99
error: deref on an immutable reference
10-
--> $DIR/borrow_deref_ref.rs:13:23
11-
|
12-
LL | let x: &str = &*s;
13-
| ^^^
14-
|
15-
help: if you would like to reborrow, try removing `&*`
16-
|
17-
LL | let x: &str = s;
18-
| ~
19-
help: if you would like to deref, try using `&**`
20-
|
21-
LL | let x: &str = &**s;
22-
| ~~~~
23-
24-
error: deref on an immutable reference
25-
--> $DIR/borrow_deref_ref.rs:15:22
10+
--> $DIR/borrow_deref_ref.rs:12:22
2611
|
2712
LL | let b = &mut &*bar(&12);
2813
| ^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `bar(&12)`
2914

3015
error: deref on an immutable reference
31-
--> $DIR/borrow_deref_ref.rs:58:23
16+
--> $DIR/borrow_deref_ref.rs:55:23
3217
|
3318
LL | let addr_y = &&*x as *const _ as usize; // assert ok
3419
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`
3520

36-
error: aborting due to 4 previous errors
21+
error: aborting due to 3 previous errors
3722

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![allow(dead_code, unused_variables)]
2+
3+
fn main() {}
4+
5+
mod should_lint {
6+
fn two_suggestions() {
7+
let s = &String::new();
8+
let x: &str = &*s;
9+
}
10+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: deref on an immutable reference
2+
--> $DIR/borrow_deref_ref_unfixable.rs:8:23
3+
|
4+
LL | let x: &str = &*s;
5+
| ^^^
6+
|
7+
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
8+
help: if you would like to reborrow, try removing `&*`
9+
|
10+
LL | let x: &str = s;
11+
| ~
12+
help: if you would like to deref, try using `&**`
13+
|
14+
LL | let x: &str = &**s;
15+
| ~~~~
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)