marktoda / forge-gas-snapshot

Library for checked-in, targeted gas snapshots with forge
MIT License
98 stars 11 forks source link

state mutability #4

Closed NoahZinsmeister closed 1 year ago

NoahZinsmeister commented 1 year ago

running forge install in a repo with forge-gas-snapshot as a dependency results in a compiler warning on this line: https://github.com/marktoda/forge-gas-snapshot/blob/74afc91f5e52bd402811c66168c56a011c21def9/src/GasSnapshot.sol#L117

warning[2018]: Warning: Function state mutability can be restricted to view
   --> lib/forge-gas-snapshot/src/GasSnapshot.sol:108:5:
    |
108 |     function _readSnapshot(string memory name) private returns (uint256 res) {
    |     ^ (Relevant source part starts here and spans across multiple lines).
marktoda commented 1 year ago

ah thanks, fixed in 63a504e03a4300b297b603afff432396e97710e8

bd21 commented 10 months ago

Got this error but don't understand why - both readLine and _getSnapFile are views. Had to remove the view modifier _checkSnapshot and _readSnapshot.

➜ cctp-money-bridge-contracts (audit-pass-3) ✗ forge test --use 0.8.22 --evm-version paris
[⠊] Compiling...
[⠰] Compiling 3 files with 0.8.22
[⠔] Solc 0.8.22 finished in 335.68ms
Error: 
Compiler run failed:
Error (8961): Function cannot be declared as view because this expression (potentially) modifies the state.
   --> lib/forge-gas-snapshot/src/GasSnapshot.sol:118:34:
    |
118 |         string memory oldValue = vm.readLine(_getSnapFile(name));
    |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bd21 commented 10 months ago

@marktoda

marktoda commented 10 months ago

interesting, could be that you're not using the right Test mixin from foundry, for ex it works on latest in v4 with https://github.com/Uniswap/v4-core/blob/83557113a0425eb3d81570c30e7a5ce550037149/test/PoolManager.t.sol#L4

marktoda commented 10 months ago

or check in your forge-std Script.sol to see how it defines readLine - in my local instances it is function readLine(string calldata path) external view returns (string memory line);