Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Validate constructors in ancestor contracts #1385

Merged
merged 5 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions packages/lib/contracts/mocks/Invalid.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ contract WithEmptyConstructor {
constructor() internal { }
}

contract WithModifierInConstructor {
modifier modifies { _; }
constructor() modifies internal { }
}

contract WithAncestorEmptyConstructor is WithEmptyConstructor {
}

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/validations/Constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function hasConstructor(contract: Contract, buildArtifacts: BuildArtifact
function hasNonEmptyConstructorInAST(contractNode: any): boolean {
spalladino marked this conversation as resolved.
Show resolved Hide resolved
return contractNode.nodes
.filter((n: any) => n.nodeType === 'FunctionDefinition' && n.kind === 'constructor')
.filter((n: any) => n.body.statements.length > 0)
.filter((n: any) => n.body.statements.length > 0 || n.modifiers.length > 0)
Copy link
Contributor

@frangio frangio Jan 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be false positives if there is a "modifier" that is a super call to a parent contract with an empty constructor. But that's an edge case that I don't think will happen.

.length > 0;
}
4 changes: 4 additions & 0 deletions packages/lib/test/src/validations/Validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('Validations', function() {
it('does not warn when adding a contract with an ancestor with an empty constructor', async function() {
validate('WithAncestorEmptyConstructor').hasConstructor.should.be.false;
});

it('warns when adding a contract with an empty constructor with modifiers', async function() {
validate('WithModifierInConstructor').hasConstructor.should.be.true;
});
});

it('warns when adding a contract with a selfdestruct call', async function() {
Expand Down