rust-lang / rust-lang/rust-clippy

[lint request] unnecessary_str_transmute

Open
#3,995 0 comments 3 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 (u8* [const, mut], usize) into a &str/&mut str/* const str/*mut str unnecessarily and vice-versa, instead of building a slice first (e.g. using the from_raw_parts methods), and then using the from_utf8 or from_utf8_unchecked str methods (yeah, really).

We should probably cover also the case where the pointer type is not an u8 since the transmutes will work for any pointer type. People should use the proper methods instead of relying on the unspecified str type 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

The issue provides no file or test entry point, so start by tracing how Rust Clippy defines and tests existing transmute lints. Done means detecting the listed pointer/string transmute forms, including non-u8 pointers and reversed tuple order, and warning even when the code appears inside a macro.

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.