Skip to content

Commit bb8a9f2

Browse files
Merge pull request #729 from protofire/feat-no-immutable-before-declaration
Feat: new rule ==> no immutable before declaration
2 parents 2decc66 + c1f0497 commit bb8a9f2

File tree

8 files changed

+668
-18
lines changed

8 files changed

+668
-18
lines changed

conf/rulesets/solhint-all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ module.exports = Object.freeze({
120120
],
121121
'multiple-sends': 'warn',
122122
'no-complex-fallback': 'warn',
123+
'no-immutable-before-declaration': 'warn',
123124
'no-inline-assembly': 'warn',
124125
'not-rely-on-block-hash': 'warn',
125126
'not-rely-on-time': 'warn',

conf/rulesets/solhint-recommended.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ module.exports = Object.freeze({
9797
],
9898
'multiple-sends': 'warn',
9999
'no-complex-fallback': 'warn',
100+
'no-immutable-before-declaration': 'warn',
100101
'no-inline-assembly': 'warn',
101102
'not-rely-on-block-hash': 'warn',
102103
reentrancy: 'warn',

docs/rules.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,25 @@ title: "Rule Index of Solhint"
8080

8181
## Security Rules
8282

83-
| Rule Id | Error | Recommended | Deprecated |
84-
| -------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------ | ---------- |
85-
| [avoid-call-value](./rules/security/avoid-call-value.md) | Avoid to use ".call.value()()". | $~~~~~~~~$✔️ | |
86-
| [avoid-low-level-calls](./rules/security/avoid-low-level-calls.md) | Avoid to use low level calls. | $~~~~~~~~$✔️ | |
87-
| [avoid-sha3](./rules/security/avoid-sha3.md) | Use "keccak256" instead of deprecated "sha3". | $~~~~~~~~$✔️ | |
88-
| [avoid-suicide](./rules/security/avoid-suicide.md) | Use "selfdestruct" instead of deprecated "suicide". | $~~~~~~~~$✔️ | |
89-
| [avoid-throw](./rules/security/avoid-throw.md) | "throw" is deprecated, avoid to use it. | $~~~~~~~~$✔️ | |
90-
| [avoid-tx-origin](./rules/security/avoid-tx-origin.md) | Avoid to use tx.origin. | $~~~~~~~~$✔️ | |
91-
| [check-send-result](./rules/security/check-send-result.md) | Check result of "send" call. | $~~~~~~~~$✔️ | |
92-
| [compiler-version](./rules/security/compiler-version.md) | Compiler version must satisfy a semver requirement at least ^0.8.24. | $~~~~~~~~$✔️ | |
93-
| [func-visibility](./rules/security/func-visibility.md) | Explicitly mark visibility in function. | $~~~~~~~~$✔️ | |
94-
| [multiple-sends](./rules/security/multiple-sends.md) | Avoid multiple calls of "send" method in single transaction. | $~~~~~~~~$✔️ | |
95-
| [no-complex-fallback](./rules/security/no-complex-fallback.md) | Fallback function must be simple. | $~~~~~~~~$✔️ | |
96-
| [no-inline-assembly](./rules/security/no-inline-assembly.md) | Avoid to use inline assembly. It is acceptable only in rare cases. | $~~~~~~~~$✔️ | |
97-
| [not-rely-on-block-hash](./rules/security/not-rely-on-block-hash.md) | Do not rely on "block.blockhash". Miners can influence its value. | $~~~~~~~~$✔️ | |
98-
| [not-rely-on-time](./rules/security/not-rely-on-time.md) | Avoid making time-based decisions in your business logic. | | |
99-
| [reentrancy](./rules/security/reentrancy.md) | Possible reentrancy vulnerabilities. Avoid state changes after transfer. | $~~~~~~~~$✔️ | |
100-
| [state-visibility](./rules/security/state-visibility.md) | Explicitly mark visibility of state. | $~~~~~~~~$✔️ | |
83+
| Rule Id | Error | Recommended | Deprecated |
84+
| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------ | ---------- |
85+
| [avoid-call-value](./rules/security/avoid-call-value.md) | Avoid to use ".call.value()()". | $~~~~~~~~$✔️ | |
86+
| [avoid-low-level-calls](./rules/security/avoid-low-level-calls.md) | Avoid to use low level calls. | $~~~~~~~~$✔️ | |
87+
| [avoid-sha3](./rules/security/avoid-sha3.md) | Use "keccak256" instead of deprecated "sha3". | $~~~~~~~~$✔️ | |
88+
| [avoid-suicide](./rules/security/avoid-suicide.md) | Use "selfdestruct" instead of deprecated "suicide". | $~~~~~~~~$✔️ | |
89+
| [avoid-throw](./rules/security/avoid-throw.md) | "throw" is deprecated, avoid to use it. | $~~~~~~~~$✔️ | |
90+
| [avoid-tx-origin](./rules/security/avoid-tx-origin.md) | Avoid to use tx.origin. | $~~~~~~~~$✔️ | |
91+
| [check-send-result](./rules/security/check-send-result.md) | Check result of "send" call. | $~~~~~~~~$✔️ | |
92+
| [compiler-version](./rules/security/compiler-version.md) | Compiler version must satisfy a semver requirement at least ^0.8.24. | $~~~~~~~~$✔️ | |
93+
| [func-visibility](./rules/security/func-visibility.md) | Explicitly mark visibility in function. | $~~~~~~~~$✔️ | |
94+
| [multiple-sends](./rules/security/multiple-sends.md) | Avoid multiple calls of "send" method in single transaction. | $~~~~~~~~$✔️ | |
95+
| [no-complex-fallback](./rules/security/no-complex-fallback.md) | Fallback function must be simple. | $~~~~~~~~$✔️ | |
96+
| [no-immutable-before-declaration](./rules/security/no-immutable-before-declaration.md) | Immutable variables should not be used in state variable initializers before they are declared. | $~~~~~~~~$✔️ | |
97+
| [no-inline-assembly](./rules/security/no-inline-assembly.md) | Avoid to use inline assembly. It is acceptable only in rare cases. | $~~~~~~~~$✔️ | |
98+
| [not-rely-on-block-hash](./rules/security/not-rely-on-block-hash.md) | Do not rely on "block.blockhash". Miners can influence its value. | $~~~~~~~~$✔️ | |
99+
| [not-rely-on-time](./rules/security/not-rely-on-time.md) | Avoid making time-based decisions in your business logic. | | |
100+
| [reentrancy](./rules/security/reentrancy.md) | Possible reentrancy vulnerabilities. Avoid state changes after transfer. | $~~~~~~~~$✔️ | |
101+
| [state-visibility](./rules/security/state-visibility.md) | Explicitly mark visibility of state. | $~~~~~~~~$✔️ | |
101102
102103

103104
## References
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
warning: "This is a dynamically generated file. Do not edit manually."
3+
layout: "default"
4+
title: "no-immutable-before-declaration | Solhint"
5+
---
6+
7+
# no-immutable-before-declaration
8+
![Recommended Badge](https://img.shields.io/badge/-Recommended-brightgreen)
9+
![Category Badge](https://img.shields.io/badge/-Security%20Rules-informational)
10+
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow)
11+
> The {"extends": "solhint:recommended"} property in a configuration file enables this rule.
12+
13+
14+
## Description
15+
Immutable variables should not be used in state variable initializers before they are declared.
16+
17+
## Options
18+
This rule accepts a string option for rule severity. Must be one of "error", "warn", "off". Defaults to warn.
19+
20+
### Example Config
21+
```json
22+
{
23+
"rules": {
24+
"no-immutable-before-declaration": "warn"
25+
}
26+
}
27+
```
28+
29+
30+
## Examples
31+
### 👍 Examples of **correct** code for this rule
32+
33+
#### Immutable declared before being used in another initializer
34+
35+
```solidity
36+
37+
contract Immutables {
38+
uint256 internal immutable immB = 25;
39+
uint256 public immA = immB + 100; // OK, immB is already declared
40+
}
41+
42+
```
43+
44+
#### Constants can be referenced before declaration
45+
46+
```solidity
47+
48+
contract Immutables {
49+
uint256 public constA = constB + 100; // OK, constants are compile-time
50+
uint256 internal constant constB = 50;
51+
}
52+
53+
```
54+
55+
### 👎 Examples of **incorrect** code for this rule
56+
57+
#### Immutable used before declaration in state variable initializer
58+
59+
```solidity
60+
61+
contract Immutables {
62+
uint256 public immA = immB + 100; // BAD: immB declared later
63+
uint256 internal immutable immB = 25;
64+
}
65+
66+
```
67+
68+
## Version
69+
This rule was introduced in the latest version.
70+
71+
## Resources
72+
- [Rule source](https://github.com/protofire/solhint/blob/master/lib/rules/security/no-immutable-before-declaration.js)
73+
- [Document source](https://github.com/protofire/solhint/blob/master/docs/rules/security/no-immutable-before-declaration.md)
74+
- [Test cases](https://github.com/protofire/solhint/blob/master/test/rules/security/no-immutable-before-declaration.js)

lib/rules/security/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const NotRelyOnBlockHashChecker = require('./not-rely-on-block-hash')
1414
const NotRelyOnTimeChecker = require('./not-rely-on-time')
1515
const ReentrancyChecker = require('./reentrancy')
1616
const StateVisibilityChecker = require('./state-visibility')
17+
const ImmutableB4DeclarationChecker = require('./no-immutable-before-declaration')
1718

1819
module.exports = function security(reporter, config, inputSrc) {
1920
return [
@@ -33,5 +34,6 @@ module.exports = function security(reporter, config, inputSrc) {
3334
new NotRelyOnTimeChecker(reporter),
3435
new ReentrancyChecker(reporter, inputSrc),
3536
new StateVisibilityChecker(reporter),
37+
new ImmutableB4DeclarationChecker(reporter),
3638
]
3739
}

0 commit comments

Comments
 (0)