Skip to content

Commit 5863d2e

Browse files
committed
Don't remove explicit cast to trait object pointer
1 parent 7546381 commit 5863d2e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub(super) fn check<'tcx>(
1818
cast_to: &'tcx Ty<'_>,
1919
msrv: Msrv,
2020
) -> bool {
21-
if matches!(cast_to.kind, TyKind::Ptr(_))
21+
if let TyKind::Ptr(target) = cast_to.kind
22+
&& !matches!(target.ty.kind, TyKind::TraitObject(..))
2223
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
2324
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
2425
{

tests/ui/borrow_as_ptr.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ fn implicit_cast() {
4747
// Do not lint references to temporaries
4848
core::ptr::eq(&0i32, &1i32);
4949
}
50+
51+
fn issue_15141() {
52+
let a = String::new();
53+
let b = &a as *const dyn std::any::Any;
54+
}

tests/ui/borrow_as_ptr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ fn implicit_cast() {
4747
// Do not lint references to temporaries
4848
core::ptr::eq(&0i32, &1i32);
4949
}
50+
51+
fn issue_15141() {
52+
let a = String::new();
53+
let b = &a as *const dyn std::any::Any;
54+
}

0 commit comments

Comments
 (0)