Skip to content

Commit

Permalink
Auto merge of #9997 - Jarcho:issue_9901, r=llogiq
Browse files Browse the repository at this point in the history
Don't lint `explicit_auto_deref` when the initial type is neither a reference, nor a receiver

fixes #9901
fixes #9777
changelog: `explicit_auto_deref`: Don't lint when the initial value is neither a reference, nor a receiver
  • Loading branch information
bors committed Nov 30, 2022
2 parents 87963f0 + 2d32b40 commit d7d098a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,24 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv);
match kind {
RefOp::Deref => {
let sub_ty = typeck.expr_ty(sub_expr);
if let Position::FieldAccess {
name,
of_union: false,
} = position
&& !ty_contains_field(typeck.expr_ty(sub_expr), name)
&& !ty_contains_field(sub_ty, name)
{
self.state = Some((
State::ExplicitDerefField { name },
StateData { span: expr.span, hir_id: expr.hir_id, position },
));
} else if position.is_deref_stable() {
} else if position.is_deref_stable() && sub_ty.is_ref() {
self.state = Some((
State::ExplicitDeref { mutability: None },
StateData { span: expr.span, hir_id: expr.hir_id, position },
));
}
}
},
RefOp::Method(target_mut)
if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
&& position.lint_explicit_deref() =>
Expand All @@ -320,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
StateData {
span: expr.span,
hir_id: expr.hir_id,
position
position,
},
));
},
Expand Down Expand Up @@ -394,7 +395,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
msg,
snip_expr,
}),
StateData { span: expr.span, hir_id: expr.hir_id, position },
StateData {
span: expr.span,
hir_id: expr.hir_id,
position,
},
));
} else if position.is_deref_stable()
// Auto-deref doesn't combine with other adjustments
Expand All @@ -406,7 +411,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
StateData {
span: expr.span,
hir_id: expr.hir_id,
position
position,
},
));
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/explicit_auto_deref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,8 @@ fn main() {
unimplemented!()
}
let _: String = takes_assoc(&*String::new());

// Issue #9901
fn takes_ref(_: &i32) {}
takes_ref(*Box::new(&0i32));
}
4 changes: 4 additions & 0 deletions tests/ui/explicit_auto_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,8 @@ fn main() {
unimplemented!()
}
let _: String = takes_assoc(&*String::new());

// Issue #9901
fn takes_ref(_: &i32) {}
takes_ref(*Box::new(&0i32));
}

0 comments on commit d7d098a

Please sign in to comment.