Skip to content

Don't remove explicit cast to trait object pointer #15145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clippy_lints/src/casts/borrow_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub(super) fn check<'tcx>(
cast_to: &'tcx Ty<'_>,
msrv: Msrv,
) -> bool {
if matches!(cast_to.kind, TyKind::Ptr(_))
if let TyKind::Ptr(target) = cast_to.kind
&& !matches!(target.ty.kind, TyKind::TraitObject(..))
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
{
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/borrow_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ fn implicit_cast() {
// Do not lint references to temporaries
core::ptr::eq(&0i32, &1i32);
}

fn issue_15141() {
let a = String::new();
// Don't lint cast to dyn trait pointers
let b = &a as *const dyn std::any::Any;
}
6 changes: 6 additions & 0 deletions tests/ui/borrow_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ fn implicit_cast() {
// Do not lint references to temporaries
core::ptr::eq(&0i32, &1i32);
}

fn issue_15141() {
let a = String::new();
// Don't lint cast to dyn trait pointers
let b = &a as *const dyn std::any::Any;
}