Clippy proposal: raw byte manipulation on non-repr(C) types


#1

I was reading this post on optimizing struct field ordering, and it mentioned a case in which this optimization broke Servo because Servo was relying on the layout of types that were not marked as #[repr(C)].

I was thinking that:

  1. This is probably an easy mistake to make. I’ve been working on some unsafe code recently, and only noticed a little while in that I’d forgotten to mark a type as #[repr(C)].
  2. It might be possible to design a Clippy lint that would detect code that appeared to rely on the order and offset of struct fields. This would certainly be a very fuzzy lint - it’d probably have tons of false positives and false negatives - but it might be made good enough to still be useful.

Thoughts?


#2

I wonder if repr© should warn as well because isn’t padding due to member alignment target specific? If so, only repr(packed) should be relied upon.


#3

Oh, interesting point; I think you might be right.