diff options
| author | gavincrawford <94875769+gavincrawford@users.noreply.github.com> | 2024-11-05 10:27:09 -0700 |
|---|---|---|
| committer | gavincrawford <94875769+gavincrawford@users.noreply.github.com> | 2024-11-11 13:36:24 -0700 |
| commit | 37c30aeb45d33b14dadb6c06c3f71b8e047bd2fe (patch) | |
| tree | 098aaec04bffb23051e8c9e83f048e7a9a35fb53 | |
| parent | 5f443df4044532d95c23aa8d97f2748ca287378f (diff) | |
| download | rust-37c30aeb45d33b14dadb6c06c3f71b8e047bd2fe.tar.gz rust-37c30aeb45d33b14dadb6c06c3f71b8e047bd2fe.zip | |
Check for #[rustc_as_ptr] attribute instead of searching for function names
Co-authored-by: Urgau <3616612+Urgau@users.noreply.github.com>
| -rw-r--r-- | compiler/rustc_lint/src/dangling.rs | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/dangling.rs b/compiler/rustc_lint/src/dangling.rs index a34c3e26778..09fe709e845 100644 --- a/compiler/rustc_lint/src/dangling.rs +++ b/compiler/rustc_lint/src/dangling.rs @@ -133,10 +133,11 @@ impl DanglingPointerSearcher<'_, '_> { fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) { if let ExprKind::MethodCall(method, receiver, _args, _span) = expr.kind - && matches!(method.ident.name, sym::as_ptr | sym::as_mut_ptr) + && let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) + && cx.tcx.has_attr(fn_id, sym::rustc_as_ptr) && is_temporary_rvalue(receiver) && let ty = cx.typeck_results().expr_ty(receiver) - && is_interesting(cx.tcx, ty) + && owns_allocation(cx.tcx, ty) { // FIXME: use `emit_node_lint` when `#[primary_span]` is added. cx.tcx.emit_node_span_lint( @@ -201,14 +202,14 @@ fn is_temporary_rvalue(expr: &Expr<'_>) -> bool { // Array, Vec, String, CString, MaybeUninit, Cell, Box<[_]>, Box<str>, Box<CStr>, // or any of the above in arbitrary many nested Box'es. -fn is_interesting(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool { +fn owns_allocation(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool { if ty.is_array() { true } else if let Some(inner) = ty.boxed_ty() { inner.is_slice() || inner.is_str() || inner.ty_adt_def().is_some_and(|def| tcx.is_lang_item(def.did(), LangItem::CStr)) - || is_interesting(tcx, inner) + || owns_allocation(tcx, inner) } else if let Some(def) = ty.ty_adt_def() { for lang_item in [LangItem::String, LangItem::MaybeUninit] { if tcx.is_lang_item(def.did(), lang_item) { |
