Skip to content

Commit 7d25108

Browse files
Merge branch 'develop' into fix/reentrancy-rule-path
2 parents 77e0692 + 95ebbd1 commit 7d25108

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/rules/gas-consumption/gas-custom-errors.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,25 @@ class GasCustomErrorsChecker extends BaseChecker {
8989
FunctionCall(node) {
9090
let errorStr = ''
9191
if (this.isVersionGreater(node)) {
92-
// added second part of conditional to be able to use require with Custom Errors
93-
if (node.expression.name === 'require' && node.arguments[1].type !== 'FunctionCall') {
94-
errorStr = 'require'
92+
if (node.expression.name === 'require') {
93+
// If no second argument or second argument is a string, flag it
94+
if (node.arguments.length < 2) {
95+
// No second argument
96+
errorStr = 'require'
97+
} else {
98+
const secondArg = node.arguments[1]
99+
if (secondArg.type === 'StringLiteral') {
100+
// e.g. require(cond, "Error message");
101+
errorStr = 'require'
102+
} else if (secondArg.type !== 'FunctionCall') {
103+
// e.g. require(cond, 42) or require(cond, someVar)
104+
// Probably not a custom error, so still flag
105+
errorStr = 'require'
106+
}
107+
// else if secondArg.type === 'FunctionCall':
108+
// e.g. require(cond, MyCustomError())
109+
// We skip, because it’s presumably a custom error
110+
}
95111
} else if (
96112
node.expression.name === 'revert' &&
97113
(node.arguments.length === 0 || node.arguments[0].type === 'StringLiteral')

test/rules/gas-consumption/gas-custom-errors.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@ function replaceSolidityVersion(code, newVersion) {
1717
}
1818

1919
describe('Linter - gas-custom-errors', () => {
20+
it('should raise error for require with a non-function-call second argument', () => {
21+
// second arg is a numeric literal instead of a function call
22+
const code = `
23+
pragma solidity 0.8.5;
24+
contract A {
25+
function test() external {
26+
require(msg.sender != address(0), 123);
27+
}
28+
}
29+
`
30+
const report = linter.processStr(code, {
31+
rules: { 'gas-custom-errors': 'error' },
32+
})
33+
assertErrorCount(report, 1)
34+
assertErrorMessage(report, 'Use Custom Errors instead of require statements')
35+
})
36+
37+
it('should raise error for require with no second argument', () => {
38+
const code = `
39+
pragma solidity 0.8.5;
40+
contract A {
41+
function test() external {
42+
require(msg.sender != address(0));
43+
}
44+
}
45+
`
46+
const report = linter.processStr(code, {
47+
rules: { 'gas-custom-errors': 'error' },
48+
})
49+
assertErrorCount(report, 1)
50+
assertErrorMessage(report, 'Use Custom Errors instead of require statements')
51+
})
52+
2053
it('should NOT raise error for require with comparison and custom error()', () => {
2154
let code = funcWith(`require(a > b, CustomErrorEmitted());`)
2255
code = replaceSolidityVersion(code, '^0.8.4')

0 commit comments

Comments
 (0)