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

fix: ignore method invocations in a variable declaration for prefer-moving-to-variable #1144

Merged
merged 2 commits into from
Jan 12, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* feat: exclude `.freezed.dart` files by default.
* fix: handle try and switch statements for [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously)
* chore: restrict `analyzer` version to `>=5.1.0 <5.4.0`.
* fix: ignore method invocations in a variable declaration for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).

## 5.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class _BlockVisitor extends RecursiveAstVisitor<void> {
@override
void visitMethodInvocation(MethodInvocation node) {
if (node.parent is CascadeExpression ||
node.parent is VariableDeclaration ||
(node.staticType?.isVoid ?? false)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void main() {
final var1 = someFunction();
final var2 = someFunction();
}

String someFunction() {
return 'qwe';
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const _cascadeExamplePath =
'prefer_moving_to_variable/examples/cascade_example.dart';
const _genericsExamplePath =
'prefer_moving_to_variable/examples/generics_example.dart';
const _assignmentExamplePath =
'prefer_moving_to_variable/examples/assignment_example.dart';
const _prefixExamplePath =
'prefer_moving_to_variable/examples/prefix_example.dart';

Expand Down Expand Up @@ -217,6 +219,13 @@ void main() {
RuleTestHelper.verifyNoIssues(issues);
});

test('reports no issues for assignments', () async {
final unit = await RuleTestHelper.resolveFromFile(_assignmentExamplePath);
final issues = PreferMovingToVariableRule().check(unit);

RuleTestHelper.verifyNoIssues(issues);
});

test('reports no issues for generics', () async {
final unit = await RuleTestHelper.resolveFromFile(_genericsExamplePath);
final issues = PreferMovingToVariableRule().check(unit);
Expand Down