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

Commit 8b16cbb

Browse files
Roman Petrovdkrutskikh
andauthored
fix: cover more cases in "prefer-immediate-return" rule (#760)
Co-authored-by: Dmitry Krutskikh <dmitry.krutskikh@gmail.com>
1 parent 76705e5 commit 8b16cbb

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## Unreleased
44

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

78
## 4.13.0
89

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

1213
## 4.12.0
1314

lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_immediate_return/visitor.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
66
Iterable<_IssueDetails> get issues => _issues;
77

88
@override
9-
void visitBlockFunctionBody(BlockFunctionBody node) {
10-
super.visitBlockFunctionBody(node);
9+
void visitBlock(Block node) {
10+
super.visitBlock(node);
1111

12-
if (node.block.statements.length < 2) {
12+
if (node.statements.length < 2) {
1313
return;
1414
}
1515

1616
final variableDeclarationStatement =
17-
node.block.statements[node.block.statements.length - 2];
18-
final returnStatement = node.block.statements.last;
17+
node.statements[node.statements.length - 2];
18+
final returnStatement = node.statements.last;
1919
if (variableDeclarationStatement is! VariableDeclarationStatement ||
2020
returnStatement is! ReturnStatement) {
2121
return;

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_immediate_return/examples/example.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,31 @@ int calculateSum(int a, int b) {
4141

4242
return sum; // OK, "sum" variable not immediately preceding return statement
4343
}
44+
45+
void calculateSum(int a, int b) {
46+
try {
47+
final sum = a + b;
48+
49+
return sum; // LINT
50+
} catch (e) {
51+
final sum = 0;
52+
53+
return sum; // LINT
54+
}
55+
56+
return 0;
57+
}
58+
59+
void calculateSomething(bool condition, int a, int b) {
60+
for (var i = 0; i < 10; i++) {
61+
final result = a * b;
62+
63+
return result; // LINT
64+
}
65+
if (condition) {
66+
final result = a + b;
67+
68+
return result; // LINT
69+
}
70+
return 0;
71+
}

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_immediate_return/prefer_immediate_return_rule_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,65 @@ void main() {
3333
16,
3434
23,
3535
30,
36+
49,
37+
53,
38+
63,
39+
68,
3640
],
3741
startColumns: [
3842
3,
3943
3,
4044
3,
4145
5,
4246
3,
47+
5,
48+
5,
49+
5,
50+
5,
4351
],
4452
messages: [
4553
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
4654
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
4755
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
4856
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
4957
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
58+
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
59+
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
60+
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
61+
'Prefer returning the result immediately instead of declaring an intermediate variable right before the return statement.',
5062
],
5163
replacementComments: [
5264
'Replace with immediate return.',
5365
'Replace with immediate return.',
5466
'Replace with immediate return.',
5567
'Replace with immediate return.',
5668
'Replace with immediate return.',
69+
'Replace with immediate return.',
70+
'Replace with immediate return.',
71+
'Replace with immediate return.',
72+
'Replace with immediate return.',
5773
],
5874
replacements: [
5975
'return a + b;',
6076
'return a + b;',
6177
'return a + b;',
6278
'return width * height;',
6379
'return null;',
80+
'return a + b;',
81+
'return 0;',
82+
'return a * b;',
83+
'return a + b;',
6484
],
6585
locationTexts: [
6686
'return sum;',
6787
'return sum;',
6888
'return sum;',
6989
'return result;',
7090
'return x;',
91+
'return sum;',
92+
'return sum;',
93+
'return result;',
94+
'return result;',
7195
],
7296
);
7397
});

0 commit comments

Comments
 (0)