protofire / solhint

Solhint is an open-source project to provide a linting utility for Solidity code.
https://protofire.github.io/solhint/
MIT License
1.04k stars 161 forks source link

False positive with gas-struct-packing #597

Open RyanRHall opened 2 months ago

RyanRHall commented 2 months ago

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

jhweintraub commented 2 months ago

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