Migrate avoid unnecessary return variable rule and its tests#237
Migrate avoid unnecessary return variable rule and its tests#237daria-trusca-solid wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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.
| class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> { | |
| class AvoidUnnecessaryReturnVariableVisitor extends SimpleAstVisitor<void> { |
| rule.reportAtNode(node); | ||
|
|
||
| super.visitReturnStatement(node); |
There was a problem hiding this comment.
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.
| rule.reportAtNode(node); | |
| super.visitReturnStatement(node); | |
| rule.reportAtNode(node); |
Migrated the
avoid_unnecessary_return_variablerule and its corresponding tests to comply with theanalyzerpackage.Changes
avoid_unnecessary_return_variableto use the currentanalyzersyntax.