From 7feac4817cb75481f6d1fb227d5e81f298b57f0d Mon Sep 17 00:00:00 2001 From: h1kk4 Date: Wed, 31 May 2023 16:24:54 +0600 Subject: [PATCH 1/3] state variable rule --- ...state-variable-can-be-set-to-immutable.sol | 125 ++++++++++++++++++ ...tate-variable-can-be-set-to-immutable.yaml | 124 +++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 solidity/performance/state-variable-can-be-set-to-immutable.sol create mode 100644 solidity/performance/state-variable-can-be-set-to-immutable.yaml diff --git a/solidity/performance/state-variable-can-be-set-to-immutable.sol b/solidity/performance/state-variable-can-be-set-to-immutable.sol new file mode 100644 index 0000000..b713628 --- /dev/null +++ b/solidity/performance/state-variable-can-be-set-to-immutable.sol @@ -0,0 +1,125 @@ +pragma solidity >=0.7.4; + + + +contract T1{ + //ruleid: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + return true; + } +} + +contract T2{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1 = v1 + 1; + } +} + +contract T3{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1 += 1; + } +} + +contract T4{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1 -= 1; + } +} + +contract T5{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1 /= 1; + } +} + +contract T6{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1 *= 1; + } +} + +contract T7{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1++; + } +} + +contract T8{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + function test() public{ + v1--; + } +} + +contract T9{ + //ok: state-variable-can-be-set-to-immutable + uint v1 = 1; + + constructor(){ + v1--; + } +} + + +contract T10{ + //ok: state-variable-can-be-set-to-immutable + uint public immutable v1 = 1; +} + +contract T11{ + //ok: state-variable-can-be-set-to-immutable + uint immutable v1 = 1; +} + +contract T12{ + //ok: state-variable-can-be-set-to-immutable + uint constant v1 = 1; +} + + + +// semgrep can't catch it correctly now + + +// contract T13{ +// //ok: state-variable-can-be-set-to-immutable +// uint v1 = 1; +// } + +// contract T13Child is T12{ +// function test() public{ +// v1 = 2; +// } +// } + + +//ok: state-variable-can-be-set-to-immutable +contract T14{ + uint v1 = 5; + function kek() { + v1%=10; + } +} \ No newline at end of file diff --git a/solidity/performance/state-variable-can-be-set-to-immutable.yaml b/solidity/performance/state-variable-can-be-set-to-immutable.yaml new file mode 100644 index 0000000..3405523 --- /dev/null +++ b/solidity/performance/state-variable-can-be-set-to-immutable.yaml @@ -0,0 +1,124 @@ +rules: + - id: state-variable-can-be-set-to-immutable + metadata: + references: + - https://twitter.com/0xAsm0d3us/status/1518960501983936512 + category: performance + tags: + - variable + message: Consider making variable $VARIABLE constant or immutable to reduce + waste of gas. + languages: + - solidity + severity: INFO + patterns: + - pattern: | + $TYPE $VARIABLE = $VALUE; + - pattern-inside: | + contract $CONTRACT{ + ... + $TYPE $VARIABLE = $VALUE; + ... + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + $TYPE immutable $VARIABLE = $VALUE; + ... + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + $TYPE constant $VARIABLE = $VALUE; + ... + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE = $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE += $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE -= $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE *= $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE /= $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE %= $NEWVALUE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE++; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + ++$VARIABLE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + --$VARIABLE; + ... + } + } + - pattern-not-inside: | + contract $CONTRACT{ + ... + function $FUNC(...){ + ... + $VARIABLE--; + ... + } + } From 7f3379c286a4a74e4ce602987b2e5a3ca714b2a2 Mon Sep 17 00:00:00 2001 From: h1kk4 Date: Wed, 31 May 2023 16:25:23 +0600 Subject: [PATCH 2/3] add rule --- README.md | 1 + .../performance/state-variable-can-be-set-to-immutable.yaml | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0777274..cebd2ae 100644 --- a/README.md +++ b/README.md @@ -128,3 +128,4 @@ use-prefix-increment-not-postfix | The prefix increment expression is cheaper in use-short-revert-string | Shortening revert strings to fit in 32 bytes will decrease gas costs for deployment and gas costs when the revert condition has been met. non-payable-constructor | Consider making costructor payable to save gas. non-optimal-variables-swap | Consider swapping variables using `($VAR1, $VAR2) = ($VAR2, $VAR1)` to save gas. +state-variable-can-be-set-to-immutable | Consider making variable constant or immutable to save gas. diff --git a/solidity/performance/state-variable-can-be-set-to-immutable.yaml b/solidity/performance/state-variable-can-be-set-to-immutable.yaml index 3405523..7f29874 100644 --- a/solidity/performance/state-variable-can-be-set-to-immutable.yaml +++ b/solidity/performance/state-variable-can-be-set-to-immutable.yaml @@ -6,8 +6,7 @@ rules: category: performance tags: - variable - message: Consider making variable $VARIABLE constant or immutable to reduce - waste of gas. + message: Consider making variable $VARIABLE constant or immutable to save gas. languages: - solidity severity: INFO From 4ae3df91baa29c717ce9f03bf2eb8c4f89d81c0a Mon Sep 17 00:00:00 2001 From: h1kk4 Date: Wed, 31 May 2023 16:27:23 +0600 Subject: [PATCH 3/3] tests fix --- .../performance/state-variable-can-be-set-to-immutable.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solidity/performance/state-variable-can-be-set-to-immutable.sol b/solidity/performance/state-variable-can-be-set-to-immutable.sol index b713628..6e2045d 100644 --- a/solidity/performance/state-variable-can-be-set-to-immutable.sol +++ b/solidity/performance/state-variable-can-be-set-to-immutable.sol @@ -116,8 +116,9 @@ contract T12{ // } -//ok: state-variable-can-be-set-to-immutable + contract T14{ + //ok: state-variable-can-be-set-to-immutable uint v1 = 5; function kek() { v1%=10;