Skip to content

Migrate avoid unnecessary return variable rule and its tests#237

Open
daria-trusca-solid wants to merge 5 commits into
analysis_server_migrationfrom
migrate_avoid_unnecessary_return_variable
Open

Migrate avoid unnecessary return variable rule and its tests#237
daria-trusca-solid wants to merge 5 commits into
analysis_server_migrationfrom
migrate_avoid_unnecessary_return_variable

Conversation

@daria-trusca-solid
Copy link
Copy Markdown
Collaborator

Migrated the avoid_unnecessary_return_variable rule and its corresponding tests to comply with the analyzer package.

Changes

  • Rule Refactoring: Updated the implementation of avoid_unnecessary_return_variable to use the current analyzer syntax.
  • Tests: Updated and verified all associated test cases to ensure the rule triggers correctly and no regressions are introduced.

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 avoid_unnecessary_return_variable lint rule to extend AnalysisRule and extracts the return variable usage logic into a dedicated ReturnVariableUsageVisitor. Additionally, the test suite has been migrated to a reflective test structure. The review feedback suggests extending SimpleAstVisitor instead of RecursiveAstVisitor in AvoidUnnecessaryReturnVariableVisitor to avoid redundant AST traversal, which also allows for the removal of a redundant super.visitReturnStatement(node) call.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

/// This visitor is searches all uses of a single local variable,
/// including variable declaration.
/// Visitor for [AvoidUnnecessaryReturnVariableRule].
class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> {
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

Since this visitor is registered via registry.addReturnStatement to process specific nodes, it does not need to recursively traverse the entire AST. Changing the base class to SimpleAstVisitor avoids redundant traversal and improves performance.

Suggested change
class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> {
class AvoidUnnecessaryReturnVariableVisitor extends SimpleAstVisitor<void> {

Comment on lines +55 to +57
rule.reportAtNode(node);

super.visitReturnStatement(node);
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

Calling super.visitReturnStatement(node) is redundant when using SimpleAstVisitor and registered via registry.addReturnStatement. Furthermore, it was applied inconsistently as it was skipped in several early return paths. Removing it simplifies the code and ensures consistent behavior.

Suggested change
rule.reportAtNode(node);
super.visitReturnStatement(node);
rule.reportAtNode(node);

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.

1 participant