Skip to content

Commit 435dae2

Browse files
committed
only check ExprKind::Path
1 parent fa89c56 commit 435dae2

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clippy_lints/src/needless_deref.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ impl LateLintPass<'_> for NeedlessDeref {
5454
if span.from_expansion() {
5555
return;
5656
}
57-
let parent_node = map.find(parent_hir_id);
58-
if let Some(rustc_hir::Node::Expr(parent_expr)) = parent_node {
59-
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) ||
60-
matches!(parent_expr.kind, ExprKind::AddrOf(..) ) {
61-
return;
57+
if matches!(deref_target.kind, ExprKind::Path(..) ){
58+
let parent_node = map.find(parent_hir_id);
59+
if let Some(rustc_hir::Node::Expr(parent_expr)) = parent_node {
60+
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) ||
61+
matches!(parent_expr.kind, ExprKind::AddrOf(..) ) {
62+
return;
63+
}
6264
}
6365
}
6466
}

tests/ui/needless_deref.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ mod should_lint {
77

88
let s = &String::new();
99
let a: &str = &*s;
10+
11+
let a = &12;
12+
let b = &mut &*bar(a);
13+
}
14+
15+
fn bar(x: &u32) -> &u32 {
16+
x
1017
}
1118
}
1219

tests/ui/needless_deref.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,14 @@ LL | let a: &str = &*s;
1717
= help: consider using `&**s` if you would like to deref
1818
= help: consider using `s` if you would like to reborrow
1919

20-
error: aborting due to 2 previous errors
20+
error: deref on an immutable reference
21+
--> $DIR/needless_deref.rs:12:22
22+
|
23+
LL | let b = &mut &*bar(a);
24+
| ^^^^^^^^
25+
|
26+
= help: consider using `&**bar(a)` if you would like to deref
27+
= help: consider using `bar(a)` if you would like to reborrow
28+
29+
error: aborting due to 3 previous errors
2130

0 commit comments

Comments
 (0)