Skip to content

Commit

Permalink
fix: false positive on for-loop Yul
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Feb 9, 2023
1 parent 021c960 commit 5e268ec
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/rules/best-practises/no-empty-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class NoEmptyBlocksChecker extends BaseChecker {
super(reporter, ruleId, meta)
}

ContractDefinition(node) {
this.isAssemblyFor = false
this._validateContractPartsCount(node)
}

Block(node) {
const isFallbackFunctionBlock = isFallbackFunction(node.parent)
if (isFallbackFunctionBlock) {
Expand All @@ -40,11 +45,19 @@ class NoEmptyBlocksChecker extends BaseChecker {
}

AssemblyBlock(node) {
this._validateChildrenCount(node, 'operations')
if (!this.isAssemblyFor) {
this._validateChildrenCount(node, 'operations')
}
}

ContractDefinition(node) {
this._validateContractPartsCount(node)
AssemblyFor(node) {
this.isAssemblyFor = true
const operationsCount = node.body.operations.length
if (operationsCount === 0) this._error(node)
}

'AssemblyFor:exit'() {
this.isAssemblyFor = false
}

_validateChildrenCount(node, children) {
Expand Down
63 changes: 63 additions & 0 deletions test/rules/best-practises/no-empty-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,69 @@ describe('Linter - no-empty-blocks', () => {
assertNoWarnings(report)
})

it('should not raise error for inline assembly [for] statement with some content', () => {
const code = funcWith(`
assembly {
for { } lt(i, 0x100) { } {
i := add(i, 0x20)
}
}`)

const report = linter.processStr(code, {
rules: { 'no-empty-blocks': 'warn' },
})

assertNoWarnings(report)
})

it('should raise error for inline assembly [for] statement with empty content', () => {
const code = funcWith(`
assembly {
for { } lt(i, 0x100) { } {
}
}`)

const report = linter.processStr(code, {
rules: { 'no-empty-blocks': 'warn' },
})
assertWarnsCount(report, 1)
assertErrorMessage(report, 'empty block')
})

it('should raise error for inline assembly [for nested] statement with empty content', () => {
const code = funcWith(`
assembly {
for { } lt(i, 0x100) { } {
for { } lt(j, 0x100) { } {
}
}
}`)

const report = linter.processStr(code, {
rules: { 'no-empty-blocks': 'warn' },
})
assertWarnsCount(report, 1)
assertErrorMessage(report, 'empty block')
})

it('should not raise error for inline assembly [for nested] statement with some content', () => {
const code = funcWith(`
assembly {
for { } lt(i, 0x100) { } {
i := add(i, 0x20)
for { } lt(i, 0x100) { } {
j := add(j, 0x20)
}
}
}`)

const report = linter.processStr(code, {
rules: { 'no-empty-blocks': 'warn' },
})

assertNoWarnings(report)
})

function label(data) {
const items = data.split('\n')
const lastItemIndex = items.length - 1
Expand Down

0 comments on commit 5e268ec

Please sign in to comment.