Skip to content

Commit c334e54

Browse files
committed
not lint on reborrow
1 parent ee22f0e commit c334e54

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

clippy_lints/src/needless_deref.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ impl LateLintPass<'_> for NeedlessDeref {
6868
return;
6969
}
7070
if matches!(deref_expr.kind, ExprKind::Path(..)) {
71-
let parent_node = map.find(parent_hir_id);
72-
if let Some(rustc_hir::Node::Expr(parent_expr)) = parent_node {
73-
if matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
74-
return;
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+
}
7577
}
76-
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
77-
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
78-
return;
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+
}
7986
}
8087
}
8188
}

tests/ui/needless_deref.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ fn main() {}
22

33
mod should_lint {
44
fn foo() {
5-
let a = &12;
6-
let b = &*a;
7-
85
let s = &String::new();
96
let a: &str = &*s;
107

@@ -17,8 +14,15 @@ mod should_lint {
1714
}
1815
}
1916

20-
// this mod explains why we should not lint `&mut &* (&T)`
2117
mod should_not_lint1 {
18+
fn not_lint_simple_reborrow() {
19+
let a = &12;
20+
let b = &*a;
21+
}
22+
}
23+
24+
// this mod explains why we should not lint `&mut &* (&T)`
25+
mod should_not_lint2 {
2226
fn foo(x: &mut &u32) {
2327
*x = &1;
2428
}

tests/ui/needless_deref.stderr

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
error: deref on an immutable reference
2-
--> $DIR/needless_deref.rs:6:17
3-
|
4-
LL | let b = &*a;
5-
| ^^^
6-
|
7-
= note: `-D clippy::needless-deref` 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/needless_deref.rs:9:23
2+
--> $DIR/needless_deref.rs:6:23
123
|
134
LL | let a: &str = &*s;
145
| ^^^
156
|
7+
= note: `-D clippy::needless-deref` implied by `-D warnings`
168
= help: consider using `&**s` if you would like to deref
179
= help: consider using `s` if you would like to reborrow
1810

1911
error: deref on an immutable reference
20-
--> $DIR/needless_deref.rs:12:22
12+
--> $DIR/needless_deref.rs:9:22
2113
|
2214
LL | let b = &mut &*bar(a);
2315
| ^^^^^^^^
2416
|
2517
= help: consider using `bar(a)` if you would like to reborrow
2618

2719
error: deref on an immutable reference
28-
--> $DIR/needless_deref.rs:41:23
20+
--> $DIR/needless_deref.rs:45:23
2921
|
3022
LL | let addr_y = &&*x as *const _ as usize; // assert ok
3123
| ^^^
3224
|
3325
= help: consider using `x` if you would like to reborrow
3426

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

0 commit comments

Comments
 (0)