rust-lang / rust-lang/rust-clippy
transmute_ptr_to_ptr: exclude instances between reference where itself is not definitely UB?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
cc #6372
Description
transmute_ptr_to_ptr is too noisy for instance of between references even it is pedantic currently IMO.
This issue suggests avoid triggering if following conditions are met, because those do not trigger UBs:
- the call does not extends lifetime, nor change it to unrelated one.
- at least one of following conditions are met
- the
Srcis anenumand it has#[repr(C)]or#[repr($integer)]or#[repr(transparent)], andDstis corresponding type. - the
Srcis anstructand it has#[repr(transparent)], andDstis reference to the inner type. - the
Srcis anstructwhich has single field and#[repr(C)], andDstis reference to the field type.
- the
Purpose
The lint should not be warn on &'a E ~> &'a E::Repr (where E is any field-less enum), &'a Transparent ~> &'a Transparent::Inner (where Transparent is struct with #[repr(transparent)]). The former can be achieved by primitive cast, but latter is not:
#[repr(C)] // also applicable if layout is i*, u*, or transparent
enum ExpReprC {
Zero = 0,
}
#[repr(transparent)] // also applicable if layout is C
struct Transparent(u8);
fn main() {
let m: ExpReprC = ExpReprC::Zero;
let x: &u8 = unsafe { std::mem::transmute(&m) };
let m: Transparent = Transparent(42);
let x: &u8 = unsafe { std::mem::transmute(&m) };
}
Version
rustc 1.73.0-nightly (31395ec38 2023-07-24)
binary: rustc
commit-hash: 31395ec38250b60b380fd3c27e94756aba3557de
commit-date: 2023-07-24
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
Additional Labels
@rustbot label C-question
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two Rust examples and tracing the transmute_ptr_to_ptr lint entry point. Done means the lint avoids warnings for the listed lifetime-preserving enum and transparent or single-field struct reference conversions, while retaining warnings for other cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100