Skip to content

Commit ef878c3

Browse files
authored
unnecessary_final support for ForEachPartsWithPatterns (dart-archive/linter#4288)
* `unnecessary_final` support for `ForEachPartsWithPattern`s * --
1 parent 82b6067 commit ef878c3

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

lib/src/extensions.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ extension NullableAstNodeExtension on AstNode? {
427427
}
428428
}
429429

430+
extension TokenExtension on Token? {
431+
bool get isFinal => this?.keyword == Keyword.FINAL;
432+
}
433+
430434
extension TokenTypeExtension on TokenType {
431435
TokenType get inverted => switch (this) {
432436
TokenType.LT_EQ => TokenType.GT_EQ,

lib/src/rules/prefer_final_locals.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/ast/token.dart';
76
import 'package:analyzer/dart/ast/visitor.dart';
87
import 'package:analyzer/dart/element/element.dart';
98

109
import '../analyzer.dart';
10+
import '../extensions.dart';
1111

1212
const _desc =
1313
r'Prefer final for variable declarations if they are not reassigned.';
@@ -191,11 +191,3 @@ extension on AstNode {
191191
return false;
192192
}
193193
}
194-
195-
extension on Token? {
196-
bool get isFinal {
197-
var self = this;
198-
if (self == null) return false;
199-
return self.keyword == Keyword.FINAL;
200-
}
201-
}

lib/src/rules/unnecessary_final.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011

1112
const _desc = "Don't use `final` for local variables.";
1213

@@ -122,6 +123,12 @@ class _Visitor extends SimpleAstVisitor<void> {
122123
var errorCode = getErrorCode(loopVariable.type);
123124
rule.reportLintForToken(loopVariable.keyword, errorCode: errorCode);
124125
}
126+
} else if (forLoopParts is ForEachPartsWithPattern) {
127+
var keyword = forLoopParts.keyword;
128+
if (keyword.isFinal) {
129+
rule.reportLintForToken(keyword,
130+
errorCode: UnnecessaryFinal.withoutType);
131+
}
125132
}
126133
}
127134

test/rules/unnecessary_final_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ f() {
129129
]);
130130
}
131131

132+
test_recordPattern_destructured_forEach() async {
133+
await assertDiagnostics(r'''
134+
f() {
135+
for (final (a, b) in [(1, 2)]) { }
136+
}
137+
''', [
138+
lint(13, 5),
139+
]);
140+
}
141+
132142
test_recordPattern_destructured_ok() async {
133143
await assertNoDiagnostics(r'''
134144
f() {

0 commit comments

Comments
 (0)