Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: no-ignore-return-values #208

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions config/rulesets/solium-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
"emit": "warning",
"no-constant": "warning",
"no-experimental": "warning",
"no-ignore-return-value": "warning",
"max-len": "warning",
"error-reason": "warning",
"visibility-first": "warning",
Expand Down
3 changes: 2 additions & 1 deletion config/rulesets/solium-recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
"error-reason": "warning",
"constructor": "warning",
"visibility-first": "warning",
"no-ignore-return-value": "warning",

"lbrace": "off",
"mixedcase": "off",
Expand All @@ -39,8 +40,8 @@ module.exports = {
"blank-lines": "off",
"arg-overflow": "off",
"function-order": "off",
"conditionals-whitespace": "off",
"no-experimental": "off",
"conditionals-whitespace": "off",

// Disable deprecated rules
"double-quotes": "off",
Expand Down
7 changes: 7 additions & 0 deletions config/solium.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
"description": "Ensure that experimental features are not used in production"
},

"no-ignore-return-value": {
"enabled": true,
"recommended": true,
"type": "warning",
"description": "Ensure that the return value of a function call is not ignored"
},

"blank-lines": {
"enabled": true,
"recommended": true,
Expand Down
79 changes: 79 additions & 0 deletions lib/rules/no-ignore-return-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @fileoverview Ensure that the return value of a function call is not ignored
* @author Yuichi Nukiyama
*/

"use strict";

module.exports = {

meta: {

docs: {
recommended: true,
type: "warning",
description: "Ensure that the return value of a function call is not ignored"
},

schema: []

},

create: function(context) {

const sourceCode = context.getSourceCode();
const calleeIgnoreReturnParams = [];
const namesOfFunctionWithReturnParams = [];

function inspectFunctionDeclaration(emitted) {
const node = emitted.node;

if (emitted.exit) {
return;
}

if (node.returnParams !== null) {
namesOfFunctionWithReturnParams.push(node.name);
}
}

function inspectCallExpression(emitted) {
const node = emitted.node;

if (emitted.exit) {
return;
}

// check callee without assignment
if (sourceCode.getParent(node).type !== "AssignmentExpression") {
calleeIgnoreReturnParams.push(node);
}
}

function inspectProgram(emitted) {
if (emitted.exit) {
const errorNodes = calleeIgnoreReturnParams.filter(node => {
for (const name of namesOfFunctionWithReturnParams) {
if (name === node.callee.name) {
return true;
}
}

});

errorNodes.forEach(node => {
context.report({
node: node,
message: node.callee.name + "don't use return values."
});
});
}
}

return {
CallExpression: inspectCallExpression,
FunctionDeclaration:inspectFunctionDeclaration,
Program: inspectProgram
};
}
};
2 changes: 1 addition & 1 deletion test/lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe("Checking exported rules object", function() {
// We extend ALL solium core rules and eliminate a few by setting their severity to 0.
// The rest of the rules should all be available.
// The below count will keep changing with every change in the number of core rules that exist in solium.
Object.keys(ruleDescriptions).length.should.equal(29);
Object.keys(ruleDescriptions).length.should.equal(30);

done();
});
Expand Down
79 changes: 79 additions & 0 deletions test/lib/rules/no-ignore-return-value/no-ignore-return-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @fileoverview Tests for no-ignore-return-value rule
* @author Yuichi Nukiyama
*/

"use strict";

const Solium = require("../../../../lib/solium");
const userConfig = {
"rules": {
"no-ignore-return-value": "warning"
}
};


describe("[RULE] no-ignore-return-value: Acceptances", function() {

it("should accept functions which use return value", done => {
let codes = [`
contract Foo {
function a(uint x) private returns (uint) { return 1; }
function b() private { uint c = a(1); }
}
`];

codes.push(`
contract Foo {
uint c;
function a(uint x) private returns (uint) {}
function b() private { c = a(1); }
}
`);

codes.push(`
contract Foo {
function a(uint x) public {}
function b() private { a(1); }
}
`);

codes.forEach(code => {
const errors = Solium.lint(code, userConfig);
errors.should.be.Array();
errors.should.have.size(0);
});

Solium.reset();
done();
});
});

describe("[RULE] no-ignore-return-value: Rejections", function() {

it("should reject functions which don't use return value", done => {
let codes = [`
contract Foo {
function a(uint x) private returns (uint) { return 1; }
function b() private { a(1); }
}
`];

codes.push(`
contract Foo {
uint c;
function a() private returns (uint) {}
function b() private { a(); }
}
`);

codes.forEach(code => {
const errors = Solium.lint(code, userConfig);
errors.should.be.Array();
errors.should.have.size(1);
});

Solium.reset();
done();
});
});