Skip to content

Commit 37c30ae

Browse files
gavincrawfordUrgau
andcommitted
Check for #[rustc_as_ptr] attribute instead of searching for function names
Co-authored-by: Urgau <[email protected]>
1 parent 5f443df commit 37c30ae

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

compiler/rustc_lint/src/dangling.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ impl DanglingPointerSearcher<'_, '_> {
133133

134134
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
135135
if let ExprKind::MethodCall(method, receiver, _args, _span) = expr.kind
136-
&& matches!(method.ident.name, sym::as_ptr | sym::as_mut_ptr)
136+
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
137+
&& cx.tcx.has_attr(fn_id, sym::rustc_as_ptr)
137138
&& is_temporary_rvalue(receiver)
138139
&& let ty = cx.typeck_results().expr_ty(receiver)
139-
&& is_interesting(cx.tcx, ty)
140+
&& owns_allocation(cx.tcx, ty)
140141
{
141142
// FIXME: use `emit_node_lint` when `#[primary_span]` is added.
142143
cx.tcx.emit_node_span_lint(
@@ -201,14 +202,14 @@ fn is_temporary_rvalue(expr: &Expr<'_>) -> bool {
201202

202203
// Array, Vec, String, CString, MaybeUninit, Cell, Box<[_]>, Box<str>, Box<CStr>,
203204
// or any of the above in arbitrary many nested Box'es.
204-
fn is_interesting(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool {
205+
fn owns_allocation(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool {
205206
if ty.is_array() {
206207
true
207208
} else if let Some(inner) = ty.boxed_ty() {
208209
inner.is_slice()
209210
|| inner.is_str()
210211
|| inner.ty_adt_def().is_some_and(|def| tcx.is_lang_item(def.did(), LangItem::CStr))
211-
|| is_interesting(tcx, inner)
212+
|| owns_allocation(tcx, inner)
212213
} else if let Some(def) = ty.ty_adt_def() {
213214
for lang_item in [LangItem::String, LangItem::MaybeUninit] {
214215
if tcx.is_lang_item(def.did(), lang_item) {

0 commit comments

Comments
 (0)