Skip to content

Commit 2212d95

Browse files
authored
fix prefer_final_locals false positive in foreach loops (#4287)
1 parent 6646b99 commit 2212d95

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/src/rules/prefer_final_locals.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class _Visitor extends SimpleAstVisitor<void> {
114114
if (function == null) return;
115115

116116
var inCaseClause = node.thisOrAncestorOfType<CaseClause>() != null;
117-
118117
if (inCaseClause) {
119118
if (!isPotentiallyMutated(node, function)) {
120119
rule.reportLint(node);
@@ -184,7 +183,11 @@ class _Visitor extends SimpleAstVisitor<void> {
184183
extension on AstNode {
185184
bool get isDeclaredFinal {
186185
var self = this;
187-
if (self is DeclaredVariablePattern) return self.keyword.isFinal;
186+
if (self is DeclaredVariablePattern) {
187+
if (self.keyword.isFinal) return true;
188+
var pattern = self.thisOrAncestorOfType<ForEachPartsWithPattern>();
189+
if (pattern != null && pattern.keyword.isFinal) return true;
190+
}
188191
return false;
189192
}
190193
}

test/rules/prefer_final_locals_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ f() {
148148
''');
149149
}
150150

151+
/// https://github.com/dart-lang/linter/issues/4286
152+
test_destructured_recordPattern_forLoop_final() async {
153+
await assertNoDiagnostics(r'''
154+
f() {
155+
for (final (a, b) in [(1, 2), (3, 4), (5, 6)]) { }
156+
}
157+
''');
158+
}
159+
151160
test_destructured_recordPattern_mutated() async {
152161
await assertNoDiagnostics(r'''
153162
f() {

0 commit comments

Comments
 (0)