rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.4k stars 1.54k forks source link

[lint idea] dangerous transmutes #546

Open durka opened 8 years ago

durka commented 8 years ago

Along the lines of useless_transmute, it seems plausible to warn on transmute::<X, Y>(x) where X and Y aren't both primitive types or marked #[repr(C)], since as @ubsan keeps reminding us this is likely to cause UB.

strega-nil commented 8 years ago

@durka heh :)

The only valid transmutes of a repr(Rust) struct that I know of are in std::repr, so it should(?) be fairly simple to special case those.

Ryman commented 8 years ago

I would love to see something like this, it's very easy to forget about and have seen it come up a number of times.

durka commented 8 years ago

@ubsan I expect running clippy on libstd would trigger more lints than that :)

oli-obk commented 8 years ago

also casting a #[repr(C)] struct with padding to anything where the padding becomes accessible is UB

durka commented 8 years ago

Does clippy have access to ask rustc whether a struct has any padding, or would the logic have to be duplicated?

On Thu, May 12, 2016 at 11:15 AM, Oliver Schneider <notifications@github.com

wrote:

also casting a #[repr(C)] struct with padding to anything where the padding becomes accessible is UB

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/Manishearth/rust-clippy/issues/546#issuecomment-218789297

oli-obk commented 8 years ago

clippy can ask rustc, at least miri can do it (since a few weeks ago), so clippy should be able to do it, too.