Skip to content

Add new lint to detect dangling pointer return from closure #15154

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PitiBouchon
Copy link

  • Finish some TODO
  • The lint name as_ptr_in_map is not good
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

The goal of this MR is to add a lint to detect dangling pointer from closure that can easily get undetected people think the closure should have took a reference instead of a value (see examples)

changelog: [as_ptr_in_map]: add a new lint to detect dangling pointer returned from closure

@rustbot
Copy link
Collaborator

rustbot commented Jun 27, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 27, 2025
@PitiBouchon PitiBouchon marked this pull request as draft June 27, 2025 00:22
@PitiBouchon
Copy link
Author

PitiBouchon commented Jun 27, 2025

The lint is working using two checks for a closure :

  • return a raw pointer
  • create a raw pointer from an owned parameter

if those two check are valid, the closure is potentially create a dangling pointer and should attract attention

@briansmith
Copy link

This seems very similar to dangling_pointers_from_temporaries. Importantly, not only owned values would result in dangling pointers; borrowed values would result in dangling pointers too, if they are references to temporaries. See rust-lang/rust#133263 (comment) for some of the difficulties with the existing warning's implementation.

@PitiBouchon
Copy link
Author

PitiBouchon commented Jul 1, 2025

This seems very similar to dangling_pointers_from_temporaries. Importantly, not only owned values would result in dangling pointers; borrowed values would result in dangling pointers too, if they are references to temporaries. See rust-lang/rust#133263 (comment) for some of the difficulties with the existing warning's implementation.

What I want to detect is things like :

let v_opt = Some(vec![0]);
let v_ptr = v_opt.map(|v| v.as_ptr());

where it is easy to forget a .as_ref() for example.

I didn't checked rustc lints (only clippy), however the dangling_pointers_from_temporaries lint doesn't seem to detect this (but maybe should in the future if the comment mentioned is resolved).

Also, I don't know how to detect temporaries 🤷

@briansmith
Copy link

I didn't checked rustc lints (only clippy), however the dangling_pointers_from_temporaries lint doesn't seem to detect this (but maybe should in the future if the comment mentioned is resolved).

I think that's rust-lang/rust#139249

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants