rust-lang / rust-lang/rust-clippy

Lint to check for dangling pointers when calling Vec::as_mut_ptr() (or other pointer types) when the vec goes out of scope without having an owner

Open
#8,015 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I'm not entirely sure whether unsafe rust is in-scope for clippy, but if it is, I'd like to see a feature to detect (at least) the more obvious cases of a dangling pointer. Specifically, I had trouble figuring out what was causing my code to have bugs here:

https://github.com/microsoft/windows-rs/issues/1342

This is the offending bit:

let pwfilter = PWSTR(format!("{}\0", filter).encode_utf16().collect::<Vec<u16>>().as_mut_ptr());

This is how it should be written:

let cstr = format!("{}\0", filter).encode_utf16().collect::<Vec<u16>>();
let pwfilter = PWSTR(cstr.as_mut_ptr());

I'm still new to programming in general, especially rust, so I didn't really see this as a problem as I'm also used to the borrow checker working even in unsafe (though, as I've been taught, this isn't a borrow.) I rely on clippy a lot, so I think maybe this is something it could warn about?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by examining how Clippy represents lints for unsafe Rust and whether the Vec::as_mut_ptr() temporary example can be detected, then use the linked windows-rs issue as context. Done means the agreed obvious dangling-pointer cases are detected without false positives and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.