forked from dart-lang/linter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dart-lang#1827 from davidmorgan/add-unnecessary-final
Add new lint: unnecessary_final.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, LinterContext context) { | ||
final visitor = _Visitor(this); | ||
registry | ||
..addFormalParameterList(this, visitor) | ||
..addForStatement(this, visitor) | ||
..addVariableDeclaration(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) { | ||
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.reportLint(loopVariable.identifier); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void visitVariableDeclaration(VariableDeclaration node) { | ||
if (node.isFinal) { | ||
rule.reportLint(node.name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
|
||
// test w/ `pub run test -N unnecessary_final` | ||
|
||
void badMethod(final int x) { // LINT | ||
final label = 'Final or var?'; // LINT | ||
print(label); | ||
for (final char in ['v', 'a', 'r']) { // LINT | ||
print(((final String char) => char.length)(char)); // LINT | ||
} | ||
} | ||
|
||
void goodMethod(int x) { | ||
var label = 'Final or var?'; // OK | ||
print(label); | ||
for (var char in ['v', 'a', 'r']) { // OK | ||
print(((String char) => char.length)(char)); // OK | ||
} | ||
} |