Skip to content

Commit 15247a0

Browse files
committed
feat: explicit return types check
1 parent daccb28 commit 15247a0

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

pretest.sh

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ source_directory=$(pwd) # Current working directory
55
package_name=$(node -pe "require('./package.json').name" | sed 's/^.*\///') # Get the package name without the scope from package.json
66
destination_directory="$source_directory/node_modules/$package_name"
77

8+
rm -rf "$destination_directory"
9+
810
# Create the destination directory if it doesn't exist
911
mkdir -p "$destination_directory"
1012

rules/explicitReturnTypesSafeCheck.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class ExplicitReturnTypesSafeCheck {
2222
}
2323
// if there are no named returns, return
2424
if (typeNames.length === 0) {
25-
// TODO: create an error that we have no named returns?
2625
return;
2726
}
2827

@@ -63,7 +62,7 @@ class ExplicitReturnTypesSafeCheck {
6362
ctx,
6463
this.ruleId,
6564
`Return statements must be written and must explicitly return something; consider "return ${returnExprGen(
66-
namedReturns || typeNames
65+
namedReturns.length === 0 ? typeNames : namedReturns
6766
)};"?`
6867
);
6968
return;
@@ -73,7 +72,7 @@ class ExplicitReturnTypesSafeCheck {
7372
ctx,
7473
this.ruleId,
7574
`Return statements must explicitly return something; consider "return ${returnExprGen(
76-
namedReturns || typeNames
75+
namedReturns.length === 0 ? typeNames : namedReturns
7776
)};"?`
7877
);
7978
return;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pragma solidity ^0.8.13;
2+
3+
contract TestNoReturn {
4+
function test() internal pure returns (uint) {
5+
uint256 result = 0;
6+
}
7+
}

test/explicitReturnTypesSafeCheck.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ describe("ExplicitReturnTypesSafeCheck", () => {
2929
`Return statements must be written and must explicitly return something; consider "return uint256;"?`
3030
);
3131
});
32+
33+
it("should return an error if no return is detected", () => {
34+
const explicitTypeMissingContract = fs.readFileSync(
35+
"test/contracts/ExplicitTypeMissing.sol",
36+
"utf8"
37+
);
38+
const result = linter.processStr(explicitTypeMissingContract, settings);
39+
assert.equal(
40+
result.reports[0].message,
41+
`Return statements must explicitly return something; consider "returns (uint256)"?`
42+
);
43+
});
3244
});

0 commit comments

Comments
 (0)