rust-lang / rust-lang/rust-clippy

`unused_phantomdata`

Open
#11,273 2 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

What it does

I just wrote this odd code in a test:

use std::marker::PhantomData;

#[repr(transparent)]
struct H<T> {
    h: Box<Option<T>>,
    _m: (),
    __m: PhantomData<T>,
}

I think if the type parameter is used elsewhere, or the lifetime, there should be a lint suggesting to remove it (the PhantomData).

Not sure if there are any cases where this is necessary, though. This section in the docs is a bit worrying: https://doc.rust-lang.org/nightly/std/marker/struct.PhantomData.html#ownership-and-the-drop-check

Advantage
  • Removes an unnecessary PhantomData since it already owns/uses the type parameter/lifetime
Drawbacks

No response

Example
use std::marker::PhantomData;

#[repr(transparent)]
struct H<T> {
    h: Box<Option<T>>,
    _m: (),
    __m: PhantomData<T>,
}

Could be written as:

use std::marker::PhantomData;

#[repr(transparent)]
struct H<T> {
    h: Box<Option<T>>,
    _m: (),
}

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 studying the example and the linked PhantomData ownership and drop-check documentation; done means establishing when PhantomData is unnecessary and defining a lint that suggests its removal without affecting required semantics.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.