crates/lint/docs/could-be-constant.md
Severity: Gas
ID: could-be-constant
Flags state variables that have a compile-time-constant inline initializer and are never written
anywhere — making them eligible to be declared constant.
Reports each non-constant, non-immutable state variable when all of the following hold:
string, bytes) or a
contract type.address(...),
uint160(...), contract/interface casts like IToken(...)), type(T).{min,max,interfaceId},
allowed pure builtin calls (keccak256, sha256, ripemd160, ecrecover, addmod,
mulmod), and references to other constant variables.constant state variables are inlined directly into the deployed bytecode rather than read from
storage, eliminating SLOAD costs on every access. Declaring such variables constant also
expresses intent and prevents future writes.
contract C {
uint256 LIMIT = 100;
bytes32 SALT = keccak256("foundry");
}
contract C {
uint256 constant LIMIT = 100;
bytes32 constant SALT = keccak256("foundry");
}
This lint requires an inline compile-time-constant initializer. Variables without an initializer
(uint256 x;) are not flagged, since converting them to constant requires choosing a value, not
just adding a keyword. This is a Gas-severity lint and is not applied to test or script
files.