Skip to content

Commit

Permalink
field
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyijun committed Nov 7, 2021
1 parent fe2090c commit 9f62d21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/needless_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl LateLintPass<'_> for NeedlessDeref {
if span.from_expansion() {
return;
}
if matches!(deref_expr.kind, ExprKind::Path(..)) {
if matches!(deref_expr.kind, ExprKind::Path(..) | ExprKind::Field(..)) {
if let Some(parent_node) = map.find(parent_hir_id) {
if let rustc_hir::Node::Local(..) = parent_node {
let outer_ty = cx.typeck_results().node_type(parent_hir_id);
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/needless_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ mod should_not_lint2 {
}
}

// similar to should_not_lint2
mod should_not_lint3 {
struct S<'a> {
a: &'a u32,
b: u32,
}

fn main() {
let s = S { a: &1, b: 1 };
let x = &mut &*s.a;
*x = &2;
}
}

// this mod explains why we should not lint `& &* (&T)`
mod false_negative {
fn foo() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL | let b = &mut &*bar(a);
= help: consider using `bar(a)` if you would like to reborrow

error: deref on an immutable reference
--> $DIR/needless_deref.rs:45:23
--> $DIR/needless_deref.rs:59:23
|
LL | let addr_y = &&*x as *const _ as usize; // assert ok
| ^^^
Expand Down

0 comments on commit 9f62d21

Please sign in to comment.