Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

fix: cover more cases in "prefer-immediate-return" rule #760

Merged
merged 2 commits into from
Mar 27, 2022
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## Unreleased

* fix: cover more cases in [`prefer-immediate-return`](https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return) rule
* fix: support index expressions for [`no-magic-number`](https://dartcodemetrics.dev/docs/rules/common/no-magic-number) rule.

## 4.13.0

* feat: add [Checkstyle](https://dartcodemetrics.dev/docs/cli/analyze#checkstyle) format reporter.
* feat: add [prefer-immediate-return](https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return) rule
* feat: add [`prefer-immediate-return`](https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return) rule

## 4.12.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
Iterable<_IssueDetails> get issues => _issues;

@override
void visitBlockFunctionBody(BlockFunctionBody node) {
super.visitBlockFunctionBody(node);
void visitBlock(Block node) {
super.visitBlock(node);

if (node.block.statements.length < 2) {
if (node.statements.length < 2) {
return;
}

final variableDeclarationStatement =
node.block.statements[node.block.statements.length - 2];
final returnStatement = node.block.statements.last;
node.statements[node.statements.length - 2];
final returnStatement = node.statements.last;
if (variableDeclarationStatement is! VariableDeclarationStatement ||
returnStatement is! ReturnStatement) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ int calculateSum(int a, int b) {

return sum; // OK, "sum" variable not immediately preceding return statement
}

void calculateSum(int a, int b) {
try {
final sum = a + b;

return sum; // LINT
} catch (e) {
final sum = 0;

return sum; // LINT
}

return 0;
}

void calculateSomething(bool condition, int a, int b) {
for (var i = 0; i < 10; i++) {
final result = a * b;

return result; // LINT
}
if (condition) {
final result = a + b;

return result; // LINT
}
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,65 @@ void main() {
16,
23,
30,
49,
53,
63,
68,
],
startColumns: [
3,
3,
3,
5,
3,
5,
5,
5,
5,
],
messages: [
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
],
replacementComments: [
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
'Replace with immediate return.',
],
replacements: [
'return a + b;',
'return a + b;',
'return a + b;',
'return width * height;',
'return null;',
'return a + b;',
'return 0;',
'return a * b;',
'return a + b;',
],
locationTexts: [
'return sum;',
'return sum;',
'return sum;',
'return result;',
'return x;',
'return sum;',
'return sum;',
'return result;',
'return result;',
],
);
});
Expand Down