protofire/solhint

False positive with gas-struct-packing

RyanRHall opened this issue · 1 comments

I think there is a false positive with the gas-struct-packing rule when using contract types. Here is the MRE:

pragma solidity 0.8.24;

contract Bar {}

contract Foo {
  // this triggers gas-struct-packing
  struct MyStruct1 {
    uint96 a; // ────────╮ 1 slot full
    Bar bar; // ─────────╯
    address myAddr; // ──  20/32 bytes used in 2nd slot
  }

  // this is fine
  struct MyStruct2 {
    uint96 a; // ────────╮ 1 slot full
    address myAddr; // ──╯
    Bar bar; // ─────────  20/32 bytes used in 2nd slot
  }
}

MyStruct1 and MyStruct2 are both "tightly packed", but for some reason the linter doesn't like the first struct. I tested this against solhint v5.0.3

I found another false positive also when using multiple addresses, since you can't pack 2 addresses into the same slot

struct ExampleStruct {
  address addr1; // 1 slot
  address addr2; // 1 slot
  address addr3; // 1 slot
}
./ExampleContract.sol
  39:3  warning  GC: For [ ExampleStruct ] struct, packing seems inefficient. Try rearranging to achieve 32bytes slots  gas-struct-packing