rust-lang / rust-lang/rust-clippy

Restriction lint for constructing a type with a Drop impl from a raw pointer

Open
#5,954 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

This lint would trigger whenever a function is called that returns an owned type from a raw pointer such as CString::from_raw, String::from_raw_parts, Vec::from_raw_parts, or Box::from_raw. I believe the exact conditions that this lint should trigger are:

  • Does not take &self
  • Takes a *const T or *mut T as any of its arguments
  • Returns a type that is !Copy

But I'd love some feedback on that (especially the third bit, is !Copy the right way to express both a type that impls Drop and a type that has meaningful drop glue?

This would be useful in code bases such as Diesel or rubyfmt, where there is a significant amount of FFI, but it is rare that data owned by Rust is getting passed as a raw pointer. In those code bases, constructing an owned type like String instead of &str is almost always wrong. This is a mistake that's very easy to make, especially for newer Rust programmers, and I would love to require an explicit "no I'm actually sure I own this pointer" in the very few cases where that is actually the case.

I am happy to do the work of implementing this if folks are interested in receiving a PR for this.

Categories (optional)
  • Kind: Restruction lint

The recommended code over the original is that the original will introduce undefined behavior.

Drawbacks

As it is a restriction lint, it's unlikely to be useful to many people.

Example
CString::from_raw(ptr)

Could be written as:

CStr::from_ptr(ptr)

This lint would not be able to make a recommendation in 100% of cases. Special cases would be made for the applicable types in std (Vec, Box, CString, String). This lint would still trigger for Arc and Rc, but I'm not sure if it should recommend & instead or just note that constructing it will result in decreasing the ref count and possibly freeing the value.

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

Start by reviewing the proposed raw-pointer constructors, including CString::from_raw, String::from_raw_parts, Vec::from_raw_parts, and Box::from_raw. Resolve the lint conditions and recommendations, including !Copy versus Drop or drop glue; done means the agreed restriction lint is implemented with coverage for the applicable std types and its behavior is tested.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.