solhint-community

non-state-vars-leading-underscore

Category Badge Default Severity Badge warn

Description

Variables that are not in contract state should start with underscore. Conversely, variables that can cause an SLOAD/SSTORE should NOT start with an underscore. This makes it evident which operations cause expensive storage access when hunting for gas optimizations

Options

This rule accepts an array of options:

Index Description Default Value
0 Rule severity. Must be one of “error”, “warn”, “off”. warn

Example Config

{
  "rules": {
    "non-state-vars-leading-underscore": ["warn"]
  }
}

Notes

Examples

👍 Examples of correct code for this rule

mutable variable should NOT start with underscore since they DO cause storage read/writes


      pragma solidity 0.4.4;
        
        
      contract A {
        uint256 public foo;
      }
    

immutable variable should start with underscore since they do not cause storage reads


      pragma solidity 0.4.4;
        
        
      contract A {
        uint256 immutable public _FOO;
      }
    

block variable with leading underscore


      pragma solidity 0.4.4;
        
        
      contract A {
        function foo() public { uint _myVar; }
      }
    

function parameter with leading underscore


      pragma solidity 0.4.4;
        
        
      contract A {
        function foo( uint256 _foo ) public {}
      }
    

👎 Examples of incorrect code for this rule

mutable variable starting with underscore


      pragma solidity 0.4.4;
        
        
      contract A {
        uint256 public _foo;
      }
    

block variable without leading underscore


      pragma solidity 0.4.4;
        
        
      contract A {
        function foo() public { uint myVar; }
      }
    

function parameter without leading underscore


      pragma solidity 0.4.4;
        
        
      contract A {
        function foo( uint256 foo ) public {}
      }
    

Version

This rule was introduced in Solhint 4.0.0-rc01

Resources