Skip to content

Commit d7d098a

Browse files
committed
Auto merge of rust-lang#9997 - Jarcho:issue_9901, r=llogiq
Don't lint `explicit_auto_deref` when the initial type is neither a reference, nor a receiver fixes rust-lang#9901 fixes rust-lang#9777 changelog: `explicit_auto_deref`: Don't lint when the initial value is neither a reference, nor a receiver
2 parents 87963f0 + 2d32b40 commit d7d098a

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Diff for: clippy_lints/src/dereference.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,24 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
289289
let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv);
290290
match kind {
291291
RefOp::Deref => {
292+
let sub_ty = typeck.expr_ty(sub_expr);
292293
if let Position::FieldAccess {
293294
name,
294295
of_union: false,
295296
} = position
296-
&& !ty_contains_field(typeck.expr_ty(sub_expr), name)
297+
&& !ty_contains_field(sub_ty, name)
297298
{
298299
self.state = Some((
299300
State::ExplicitDerefField { name },
300301
StateData { span: expr.span, hir_id: expr.hir_id, position },
301302
));
302-
} else if position.is_deref_stable() {
303+
} else if position.is_deref_stable() && sub_ty.is_ref() {
303304
self.state = Some((
304305
State::ExplicitDeref { mutability: None },
305306
StateData { span: expr.span, hir_id: expr.hir_id, position },
306307
));
307308
}
308-
}
309+
},
309310
RefOp::Method(target_mut)
310311
if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
311312
&& position.lint_explicit_deref() =>
@@ -320,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
320321
StateData {
321322
span: expr.span,
322323
hir_id: expr.hir_id,
323-
position
324+
position,
324325
},
325326
));
326327
},
@@ -394,7 +395,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
394395
msg,
395396
snip_expr,
396397
}),
397-
StateData { span: expr.span, hir_id: expr.hir_id, position },
398+
StateData {
399+
span: expr.span,
400+
hir_id: expr.hir_id,
401+
position,
402+
},
398403
));
399404
} else if position.is_deref_stable()
400405
// Auto-deref doesn't combine with other adjustments
@@ -406,7 +411,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
406411
StateData {
407412
span: expr.span,
408413
hir_id: expr.hir_id,
409-
position
414+
position,
410415
},
411416
));
412417
}

Diff for: tests/ui/explicit_auto_deref.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,8 @@ fn main() {
277277
unimplemented!()
278278
}
279279
let _: String = takes_assoc(&*String::new());
280+
281+
// Issue #9901
282+
fn takes_ref(_: &i32) {}
283+
takes_ref(*Box::new(&0i32));
280284
}

Diff for: tests/ui/explicit_auto_deref.rs

+4
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,8 @@ fn main() {
277277
unimplemented!()
278278
}
279279
let _: String = takes_assoc(&*String::new());
280+
281+
// Issue #9901
282+
fn takes_ref(_: &i32) {}
283+
takes_ref(*Box::new(&0i32));
280284
}

0 commit comments

Comments
 (0)