rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

add lint for transmute from &T to &mut T of a ADT argument

Open mendelsshop opened this issue 2 months ago • 5 comments

Checks transmutes between the same ADT, making sure type arguments don't go from &T to &mut T.

Like it would catch something like:

let _: Option<&mut i32> = std::mem::transmute(Some(&5i32));

changelog: new lint [mutable_adt_argument_transmute]

fixes rust-lang/rust-clippy#372

mendelsshop avatar Oct 31 '25 02:10 mendelsshop

Edit: I now skip the parts that don't match (like Option<_> and Result<_, _>), but this does require me to match on each type's .kind() to see if they are the same ADT, slice, ..., and if not I use skip_current_subtree().

The way I am doing it now, I take the from_ty and to_ty and just zip the ty.walk() of both of them, which leads to potential false positive like catching:

let _: Result<Option<&mut f32>, &usize> = std::mem::transmute(Option::<Result<&f32, usize>>::Some(Ok(&2f32)));

Because in the order of the walk() &mut f32 and &f32 are at the same position in their respective walk().

mendelsshop avatar Oct 31 '25 14:10 mendelsshop

r? @y21

rustbot has assigned @y21. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar Oct 31 '25 14:10 rustbot

No changes for 1cc591ccd7e895436d9baae8ecec6578daca8ebf

github-actions[bot] avatar Nov 20 '25 20:11 github-actions[bot]

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

rustbot avatar Nov 20 '25 20:11 rustbot

r? clippy

mendelsshop avatar Dec 01 '25 22:12 mendelsshop

r? clippy

mendelsshop avatar Dec 17 '25 19:12 mendelsshop