Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plus-equal-cost-more-for-state-variables #51

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ 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.
inefficient-state-variable-increment | <x> += <y> costs more gas than <x> = <x> + <y> for state variables.

## Best Practices Rules

Expand Down
52 changes: 52 additions & 0 deletions solidity/performance/inefficient-state-variable-increment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.2;

contract A {
uint256[10] public a;
uint256 public b;
function one() public {
a[0] = 4;
b = 5;
// ruleid: inefficient-state-variable-increment
a[0] += b;
}
}


contract B {
uint256 public a;
uint256 public b;
function one() public {
a = 4;
b = 5;
// ok: inefficient-state-variable-increment
a = a + b;
}
}

contract C {
uint256 public a = 4;
uint256 public b;
function one() public {
b = 5;
// ruleid: inefficient-state-variable-increment
a += b;
}
}

contract D {
function one() public {
a = 4;
b = 5;
// ok: inefficient-state-variable-increment
a += b;
}
}

contract E {
mapping (address => uint) public a;
function one() public {
// ok: inefficient-state-variable-increment
a[msg.sender] += 4;
}
}
37 changes: 37 additions & 0 deletions solidity/performance/inefficient-state-variable-increment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
rules:
-
id: inefficient-state-variable-increment
message: |
<x> += <y> costs more gas than <x> = <x> + <y> for state variables.
metadata:
references:
- https://gist.github.com/IllIllI000/cbbfb267425b898e5be734d4008d4fe8
category: performance
tags:
- plus-equal
patterns:
- pattern-either:
- pattern: |
$X += $Y
- pattern: |
$X[...] += $Y
- pattern-either:
- pattern-inside: |
contract $C {
...
$TYPE $X;
...
}
- pattern-inside: |
contract $C {
...
$TYPE $X = ...;
...
}
- metavariable-regex:
metavariable: $TYPE
regex: uint
languages:
- solidity
severity: INFO

Loading