Skip to content

Commit ab75530

Browse files
committed
lint reborrow in Local
1 parent b1fe7c0 commit ab75530

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

clippy_lints/src/borrow_deref_ref.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,13 @@ impl LateLintPass<'_> for BorrowDerefRef {
6868
return;
6969
}
7070
if matches!(deref_expr.kind, ExprKind::Path(..) | ExprKind::Field(..)) {
71-
if let Some(parent_node) = map.find(parent_hir_id) {
72-
if let rustc_hir::Node::Local(..) = parent_node {
73-
let outer_ty = cx.typeck_results().node_type(parent_hir_id);
74-
if outer_ty == ref_ty {
75-
return;
76-
}
71+
if let Some(rustc_hir::Node::Expr(parent_expr)) = map.find(parent_hir_id) {
72+
if matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
73+
return;
7774
}
78-
if let rustc_hir::Node::Expr(parent_expr) = parent_node {
79-
if matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
80-
return;
81-
}
82-
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
83-
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
84-
return;
85-
}
75+
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
76+
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
77+
return;
8678
}
8779
}
8880
}

tests/ui/borrow_deref_ref.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@ fn main() {}
22

33
mod should_lint {
44
fn foo() {
5+
let a = &12;
6+
let b = &*a;
7+
58
let s = &String::new();
6-
let a: &str = &*s;
9+
let x: &str = &*s;
710

8-
let a = &12;
9-
let b = &mut &*bar(a);
11+
let b = &mut &*bar(&12);
1012
}
1113

1214
fn bar(x: &u32) -> &u32 {
1315
x
1416
}
1517
}
1618

17-
mod should_not_lint1 {
18-
fn not_lint_simple_reborrow() {
19-
let a = &12;
20-
let b = &*a;
21-
}
22-
}
23-
2419
// this mod explains why we should not lint `&mut &* (&T)`
25-
mod should_not_lint2 {
20+
mod should_not_lint1 {
2621
fn foo(x: &mut &u32) {
2722
*x = &1;
2823
}
@@ -37,8 +32,8 @@ mod should_not_lint2 {
3732
}
3833
}
3934

40-
// similar to should_not_lint2
41-
mod should_not_lint3 {
35+
// similar to should_not_lint1
36+
mod should_not_lint2 {
4237
struct S<'a> {
4338
a: &'a u32,
4439
b: u32,

tests/ui/borrow_deref_ref.stderr

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
error: deref on an immutable reference
2-
--> $DIR/borrow_deref_ref.rs:6:23
2+
--> $DIR/borrow_deref_ref.rs:6:17
33
|
4-
LL | let a: &str = &*s;
5-
| ^^^
4+
LL | let b = &*a;
5+
| ^^^
66
|
77
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
8+
= help: consider using `a` if you would like to reborrow
9+
10+
error: deref on an immutable reference
11+
--> $DIR/borrow_deref_ref.rs:9:23
12+
|
13+
LL | let x: &str = &*s;
14+
| ^^^
15+
|
816
= help: consider using `&**s` if you would like to deref
917
= help: consider using `s` if you would like to reborrow
1018

1119
error: deref on an immutable reference
12-
--> $DIR/borrow_deref_ref.rs:9:22
20+
--> $DIR/borrow_deref_ref.rs:11:22
1321
|
14-
LL | let b = &mut &*bar(a);
15-
| ^^^^^^^^
22+
LL | let b = &mut &*bar(&12);
23+
| ^^^^^^^^^^
1624
|
17-
= help: consider using `bar(a)` if you would like to reborrow
25+
= help: consider using `bar(&12)` if you would like to reborrow
1826

1927
error: deref on an immutable reference
20-
--> $DIR/borrow_deref_ref.rs:59:23
28+
--> $DIR/borrow_deref_ref.rs:54:23
2129
|
2230
LL | let addr_y = &&*x as *const _ as usize; // assert ok
2331
| ^^^
2432
|
2533
= help: consider using `x` if you would like to reborrow
2634

27-
error: aborting due to 3 previous errors
35+
error: aborting due to 4 previous errors
2836

0 commit comments

Comments
 (0)