Skip to content

Clarify and refine “temporary” handling in ref_as_ptr lint #16220

@Abd002

Description

@Abd002

Description

The ref_as_ptr lint currently warns on all reference → pointer casts, including borrows of named locals. There is an internal TODO in the implementation:

// TODO: only block the lint if `cast_expr` is a temporary

I interpret “temporary” to mean values created by expressions without a named local, e.g., literals (&42), function return values (&foo()), struct/tuple literals, etc., whose lifetime ends at statement evaluation — not references to named variables. Under this definition:

let val = 1;
f(&val as *const _);       // should *not* lint
f(&val as *const i32);     // should *not* lint

but

f(&42 as *const _);        // should lint
let _ = &String::new() as *const _;  // should lint

because those expressions borrow temporaries.

The current implementation lints both cases. If the goal of the TODO is to only warn when cast_expr is a true temporary, I want to confirm if this understanding is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions