diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2022-11-30 11:15:49 -0500 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2022-11-30 11:23:01 -0500 |
| commit | 2d32b403596d6ff152ccce3f1136b695698a3cc8 (patch) | |
| tree | 34a761bc5abb5c145d9b8699db6945fcda1a2cba | |
| parent | 78589ffad2c973f7863fdabc2df6e8ab2b54110f (diff) | |
| download | rust-2d32b403596d6ff152ccce3f1136b695698a3cc8.tar.gz rust-2d32b403596d6ff152ccce3f1136b695698a3cc8.zip | |
Don't lint `explicit_auto_deref` when the initial type is neither a reference, nor a receiver
| -rw-r--r-- | clippy_lints/src/dereference.rs | 17 | ||||
| -rw-r--r-- | tests/ui/explicit_auto_deref.fixed | 4 | ||||
| -rw-r--r-- | tests/ui/explicit_auto_deref.rs | 4 |
3 files changed, 19 insertions, 6 deletions
diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 7de117d5549..57c30661e88 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -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() => @@ -320,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); }, @@ -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 @@ -406,7 +411,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); } diff --git a/tests/ui/explicit_auto_deref.fixed b/tests/ui/explicit_auto_deref.fixed index 59ff5e4040a..475fae5e823 100644 --- a/tests/ui/explicit_auto_deref.fixed +++ b/tests/ui/explicit_auto_deref.fixed @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } diff --git a/tests/ui/explicit_auto_deref.rs b/tests/ui/explicit_auto_deref.rs index bcfb60c3278..c1894258f4d 100644 --- a/tests/ui/explicit_auto_deref.rs +++ b/tests/ui/explicit_auto_deref.rs @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } |
