blob: c1694a46c088316baa86fd048e6b1d85b6404fb9 [file] [log] [blame]
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import '../analyzer.dart';
const _desc = "Don't use `final` for local variables.";
const _details = r'''
**DON'T** use `final` for local variables.
`var` is shorter, and `final` does not change the meaning of the code.
**BAD:**
```
void badMethod() {
final label = 'Final or var?';
for (final char in ['v', 'a', 'r']) {
print(char);
}
}
```
**GOOD:**
```
void goodMethod() {
var label = 'Final or var?';
for (var char in ['v', 'a', 'r']) {
print(char);
}
}
```
''';
class UnnecessaryFinal extends LintRule implements NodeLintRule {
UnnecessaryFinal()
: super(
name: 'unnecessary_final',
description: _desc,
details: _details,
group: Group.style);
@override
List<String> get incompatibleRules => const ['prefer_final_locals'];
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this);
registry
..addFormalParameterList(this, visitor)
..addForStatement(this, visitor)
..addVariableDeclarationStatement(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
_Visitor(this.rule);
@override
void visitFormalParameterList(FormalParameterList parameterList) {
for (var node in parameterList.parameters) {
if (node.isFinal) {
// TODO(davidmorgan): for consistency this should report on the
// `final` keyword, not the node, but it's hard to get to from
// FormalParameter.
rule.reportLint(node);
}
}
}
@override
void visitForStatement(ForStatement node) {
var forLoopParts = node.forLoopParts;
// If the following `if` test fails, then either the statement is not a
// for-each loop, or it is something like `for(a in b) { ... }`. In the
// second case, notice `a` is not actually declared from within the
// loop. `a` is a variable declared outside the loop.
if (forLoopParts is ForEachPartsWithDeclaration) {
final loopVariable = forLoopParts.loopVariable;
if (loopVariable.isFinal) {
rule.reportLintForToken(loopVariable.keyword);
}
}
}
@override
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
if (node.variables.isFinal) {
rule.reportLintForToken(node.variables.keyword);
}
}
}