Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

declared variable pattern support for unnecessary_final #4152

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/src/rules/unnecessary_final.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class UnnecessaryFinal extends LintRule {
registry
..addFormalParameterList(this, visitor)
..addForStatement(this, visitor)
..addDeclaredVariablePattern(this, visitor)
..addVariableDeclarationStatement(this, visitor);
}
}
Expand All @@ -80,18 +81,29 @@ class _Visitor extends SimpleAstVisitor<void> {

_Visitor(this.rule);

LintCode getErrorCode(Object? type) =>
type == null ? UnnecessaryFinal.withoutType : UnnecessaryFinal.withType;

@override
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
var keyword = node.keyword;
keyword ??=
node.thisOrAncestorOfType<PatternVariableDeclaration>()?.keyword;
if (keyword == null) return;

var errorCode = getErrorCode(node.matchedValueType);
rule.reportLintForToken(keyword, errorCode: errorCode);
}

@override
void visitFormalParameterList(FormalParameterList parameterList) {
for (var node in parameterList.parameters) {
if (node.isFinal) {
var keyword = _getFinal(node);
if (keyword == null) {
continue;
}
if (keyword == null) continue;

var type = _getType(node);
var errorCode = type == null
? UnnecessaryFinal.withoutType
: UnnecessaryFinal.withType;
var errorCode = getErrorCode(type);
rule.reportLintForToken(keyword, errorCode: errorCode);
}
}
Expand All @@ -106,11 +118,8 @@ class _Visitor extends SimpleAstVisitor<void> {
// loop. `a` is a variable declared outside the loop.
if (forLoopParts is ForEachPartsWithDeclaration) {
var loopVariable = forLoopParts.loopVariable;

if (loopVariable.isFinal) {
var errorCode = loopVariable.type == null
? UnnecessaryFinal.withoutType
: UnnecessaryFinal.withType;
var errorCode = getErrorCode(loopVariable.type);
rule.reportLintForToken(loopVariable.keyword, errorCode: errorCode);
}
}
Expand All @@ -119,9 +128,7 @@ class _Visitor extends SimpleAstVisitor<void> {
@override
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
if (node.variables.isFinal) {
var errorCode = node.variables.type == null
? UnnecessaryFinal.withoutType
: UnnecessaryFinal.withType;
var errorCode = getErrorCode(node.variables.type);
rule.reportLintForToken(node.variables.keyword, errorCode: errorCode);
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import 'unnecessary_brace_in_string_interps_test.dart'
as unnecessary_brace_in_string_interps;
import 'unnecessary_breaks_test.dart' as unnecessary_breaks;
import 'unnecessary_const_test.dart' as unnecessary_const;
import 'unnecessary_final_test.dart' as unnecessary_final;
import 'unnecessary_lambdas_test.dart' as unnecessary_lambdas;
import 'unnecessary_library_directive_test.dart'
as unnecessary_library_directive;
Expand Down Expand Up @@ -194,6 +195,7 @@ void main() {
unnecessary_brace_in_string_interps.main();
unnecessary_breaks.main();
unnecessary_const.main();
unnecessary_final.main();
unnecessary_lambdas.main();
unnecessary_library_directive.main();
unnecessary_null_checks.main();
Expand Down
72 changes: 72 additions & 0 deletions test/rules/unnecessary_final_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2023, 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryFinalTestLanguage300);
});
}

@reflectiveTest
class UnnecessaryFinalTestLanguage300 extends LintRuleTest
with LanguageVersion300Mixin {
@override
String get lintRule => 'unnecessary_final';

test_listPattern_destructured() async {
await assertDiagnostics(r'''
f() {
final [a] = [1];
print('$a');
}
''', [
lint(8, 5),
]);
}

test_mapPattern_destructured() async {
await assertDiagnostics(r'''
f() {
final {'a': a} = {'a': 1};
print('$a');
}
''', [
lint(8, 5),
]);
}

test_objectPattern_switch() async {
await assertDiagnostics(r'''
class A {
int a;
A(this.a);
}

f() {
switch (A(1)) {
case A(a: >0 && final b): print('$b');
}
}
''', [
lint(79, 5),
]);
}

test_recordPattern_switch() async {
await assertDiagnostics(r'''
f() {
switch ((1, 2)) {
case (final a, final b): print('$a$b');
}
}
''', [
lint(36, 5),
lint(45, 5),
]);
}
}