rust-lang / rust-lang/rust-clippy

[lint request] unnecessary_slice_transmute

Open
#3,994 1 comment 2 reactions 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

Some people transmute a (T* [const, mut], usize) into a &[T]/&mut [T]/* const[T]/*mut [T] unnecessarily and vice-versa, instead of using the from_raw_parts methods and or (slice.as_ptr(), slice.len()) (yeah, really).

We should probably cover also the case where the pointer type and the element type of the slice differ. A pointer is a pointer, so the transmute won't care, but they should just be casting the stuff around and using the proper methods instead of relying on the unspecified slice layout.

We should detect this and tell people to use the proper methods instead. We probably want to detect the incorrect (usize, T* [const, mut]) cases as well, and tell people to also use the correct methods instead.

If people are doing this inside a macro... we should warn anyways.

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 source files, tests, or entry points are named in the issue. Start by reading Clippy's existing transmute lint implementations and the Rust APIs for slice construction and pointer/length conversion; done means the requested slice transmute patterns, including reversed tuple order, differing element types, and macro usage, are detected with appropriate guidance.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.