Skip to content

Commit

Permalink
fix make_final to properly update for loop patterns
Browse files Browse the repository at this point in the history
Fixes: #52820

Change-Id: If94b56f034bc8c28087251aada59433578ed7a2f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/312702
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jul 7, 2023
1 parent 201fb68 commit ff8963d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,15 @@ class MakeFinal extends ResolvedCorrectionProducer {
return;
}

if (node is DartPattern) {
var parent = node.parent;
if (parent is ForEachPartsWithPattern) {
await builder.addDartFileEdit(file, (builder) {
var keyword = parent.keyword;
if (keyword.keyword == Keyword.VAR) {
builder.addSimpleReplacement(range.token(keyword), 'final');
}
});
return;
}
var forPattern = node.forEachPartsParent;
if (forPattern is ForEachPartsWithPattern) {
await builder.addDartFileEdit(file, (builder) {
var keyword = forPattern.keyword;
if (keyword.keyword == Keyword.VAR) {
builder.addSimpleReplacement(range.token(keyword), 'final');
}
});
return;
}

if (node is DeclaredVariablePattern) {
Expand Down Expand Up @@ -137,3 +135,10 @@ class MakeFinal extends ResolvedCorrectionProducer {
return null;
}
}

extension on AstNode {
AstNode? get forEachPartsParent {
var parent = this.parent;
return parent is ForEachPartsWithPattern ? parent : parent?.parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,58 @@ f() {
''');
}

Future<void> test_recordPattern_declarationNestedIn_forLoop() async {
await resolveTestCode(r'''
f() {
for (var (a, b) in g(() {
var (c, d) = (0, 1);
return (c, d);
})) {
a++;
b++;
print(a + b);
}
}
List<(int, int)> g((int, int) Function() f) {
return [f()];
}
''');
await assertHasFix(r'''
f() {
for (var (a, b) in g(() {
final (c, d) = (0, 1);
return (c, d);
})) {
a++;
b++;
print(a + b);
}
}
List<(int, int)> g((int, int) Function() f) {
return [f()];
}
''');
}

Future<void> test_recordPattern_forLoop() async {
await resolveTestCode(r'''
f() {
for (var (a) in [(1)]) {
print('$a');
}
}
''');
await assertHasFix(r'''
f() {
for (final (a) in [(1)]) {
print('$a');
}
}
''');
}

Future<void> test_variableDeclarationStatement_type() async {
await resolveTestCode('''
void f() {
Expand Down

0 comments on commit ff8963d

Please sign in to comment.