rust-lang / rust-lang/rust-clippy

Lint suggestion: write to raw pointer with drop glue

Open
#4,294 4 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

One of the subtle aspects of *ptr = val where ptr is a raw pointer is that this will drop the old contents stored behind that pointer. On the one hand that might not be surprising because this is the same for mutable references, but on the other hand raw pointers are often used to point to invalid/uninitialized data, and then this is an easy way to cause a double-drop or to drop some garbage data. I have made this mistake myself and seen it a few times "out there".

So I think it'd be a good idea to lint against *ptr = val when ptr: *mut T and T has drop glue. The lint should recommend to use ptr.write(val) instead, which does not drop. Code that wants to drop should explicitly do ptr.drop_in_place().

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

The issue names no files, tests, or entry points. Start by locating Clippy's raw-pointer assignment lint infrastructure and its existing test patterns; done means assignments through raw pointers to types with drop glue are diagnosed, with suggestions to use write or drop_in_place as appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.