about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-06-26 09:35:11 +0200
committerSamuel Tardieu <sam@rfc1149.net>2025-06-26 14:42:52 +0200
commitbb59c39fd2f18398d3d044525a0bf7557f9c4768 (patch)
tree3ae7f2cde73902ccbee9294df52b0ae7036a677f
parent75463814c69b2d9907a3ccd9ba92a5e74767d00c (diff)
downloadrust-bb59c39fd2f18398d3d044525a0bf7557f9c4768.tar.gz
rust-bb59c39fd2f18398d3d044525a0bf7557f9c4768.zip
Don't remove explicit cast to trait object pointer
-rw-r--r--clippy_lints/src/casts/borrow_as_ptr.rs3
-rw-r--r--tests/ui/borrow_as_ptr.fixed6
-rw-r--r--tests/ui/borrow_as_ptr.rs6
3 files changed, 14 insertions, 1 deletions
diff --git a/clippy_lints/src/casts/borrow_as_ptr.rs b/clippy_lints/src/casts/borrow_as_ptr.rs
index ad0a4f8cdf3..e3b125a8d5b 100644
--- a/clippy_lints/src/casts/borrow_as_ptr.rs
+++ b/clippy_lints/src/casts/borrow_as_ptr.rs
@@ -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)
     {
diff --git a/tests/ui/borrow_as_ptr.fixed b/tests/ui/borrow_as_ptr.fixed
index 3ba2eea59f0..3f6e5245b87 100644
--- a/tests/ui/borrow_as_ptr.fixed
+++ b/tests/ui/borrow_as_ptr.fixed
@@ -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;
+}
diff --git a/tests/ui/borrow_as_ptr.rs b/tests/ui/borrow_as_ptr.rs
index 8cdd0512da5..20f4f40e001 100644
--- a/tests/ui/borrow_as_ptr.rs
+++ b/tests/ui/borrow_as_ptr.rs
@@ -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;
+}