Skip to content

migrate prefer early return rule with tests#232

Open
Islam-Shaaban-Ibrahim wants to merge 1 commit intoanalysis_server_migrationfrom
migrate/prefer_early_return
Open

migrate prefer early return rule with tests#232
Islam-Shaaban-Ibrahim wants to merge 1 commit intoanalysis_server_migrationfrom
migrate/prefer_early_return

Conversation

@Islam-Shaaban-Ibrahim
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the prefer_early_return lint rule to align with the AnalysisRule API, simplifying the rule definition and its associated visitor. Review feedback identifies a regression where if statements with else blocks are no longer excluded from reporting, potentially leading to undesirable lint triggers. There is also a concern regarding the use of RecursiveAstVisitor with addBlockFunctionBody, which may result in duplicate diagnostics for nested functions.

Comment on lines +32 to 36
bool _shouldReport(IfStatement node) {
final parent = node.parent;

bool _hasThrowExpression(Statement node) {
final visitor = ThrowExpressionVisitor();
node.accept(visitor);
return visitor.nodes.isNotEmpty;
return parent is Block && parent.statements.length == 1;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The _shouldReport logic is missing a check for else statements. The prefer_early_return rule should not trigger on if statements that have an else block, as reversing the condition in those cases doesn't typically lead to an early return and can make the code less readable. The previous implementation correctly excluded these cases.

  bool _shouldReport(IfStatement node) {
    final parent = node.parent;

    return parent is Block &&
        parent.statements.length == 1 &&
        node.elseStatement == null;
  }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Islam-Shaaban-Ibrahim, pay attention to this comment.

Comment on lines +59 to +65
registry.addBlockFunctionBody(
this,
PreferEarlyReturnVisitor(
rule: this,
context: context,
),
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using RecursiveAstVisitor in combination with registry.addBlockFunctionBody will lead to duplicate diagnostics if the code contains nested functions or closures. The visitor will traverse into the nested function's body, and the registry will also trigger the visitor for that same nested body independently.

Consider registering for IfStatement directly using registry.addIfStatement and performing the block check there, or use a non-recursive visitor.

Comment on lines +32 to 36
bool _shouldReport(IfStatement node) {
final parent = node.parent;

bool _hasThrowExpression(Statement node) {
final visitor = ThrowExpressionVisitor();
node.accept(visitor);
return visitor.nodes.isNotEmpty;
return parent is Block && parent.statements.length == 1;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Islam-Shaaban-Ibrahim, pay attention to this comment.

@override
void visitIfStatement(IfStatement node) {
if (_shouldReport(node)) {
context.currentUnit?.diagnosticReporter.atNode(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What is the difference between the current implementation andrule.reportAtNode?

Comment on lines +61 to +64
PreferEarlyReturnVisitor(
rule: this,
context: context,
),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's extract it into a separate variable for better readability.

Comment on lines +89 to +91
);
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please make sure you have migrated all the currently existing tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants