This repository was archived by the owner on Nov 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +22
-9
lines changed Expand file tree Collapse file tree 4 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -427,6 +427,10 @@ extension NullableAstNodeExtension on AstNode? {
427
427
}
428
428
}
429
429
430
+ extension TokenExtension on Token ? {
431
+ bool get isFinal => this ? .keyword == Keyword .FINAL ;
432
+ }
433
+
430
434
extension TokenTypeExtension on TokenType {
431
435
TokenType get inverted => switch (this ) {
432
436
TokenType .LT_EQ => TokenType .GT_EQ ,
Original file line number Diff line number Diff line change 3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:analyzer/dart/ast/ast.dart' ;
6
- import 'package:analyzer/dart/ast/token.dart' ;
7
6
import 'package:analyzer/dart/ast/visitor.dart' ;
8
7
import 'package:analyzer/dart/element/element.dart' ;
9
8
10
9
import '../analyzer.dart' ;
10
+ import '../extensions.dart' ;
11
11
12
12
const _desc =
13
13
r'Prefer final for variable declarations if they are not reassigned.' ;
@@ -191,11 +191,3 @@ extension on AstNode {
191
191
return false ;
192
192
}
193
193
}
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
- }
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
7
7
import 'package:analyzer/dart/ast/visitor.dart' ;
8
8
9
9
import '../analyzer.dart' ;
10
+ import '../extensions.dart' ;
10
11
11
12
const _desc = "Don't use `final` for local variables." ;
12
13
@@ -122,6 +123,12 @@ class _Visitor extends SimpleAstVisitor<void> {
122
123
var errorCode = getErrorCode (loopVariable.type);
123
124
rule.reportLintForToken (loopVariable.keyword, errorCode: errorCode);
124
125
}
126
+ } else if (forLoopParts is ForEachPartsWithPattern ) {
127
+ var keyword = forLoopParts.keyword;
128
+ if (keyword.isFinal) {
129
+ rule.reportLintForToken (keyword,
130
+ errorCode: UnnecessaryFinal .withoutType);
131
+ }
125
132
}
126
133
}
127
134
Original file line number Diff line number Diff line change @@ -129,6 +129,16 @@ f() {
129
129
]);
130
130
}
131
131
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
+
132
142
test_recordPattern_destructured_ok () async {
133
143
await assertNoDiagnostics (r'''
134
144
f() {
You can’t perform that action at this time.
0 commit comments