Skip to content

Commit

Permalink
fix omit_local_variable_types where local type is required for infe…
Browse files Browse the repository at this point in the history
…rence (#3024)

* fix `omit_local_variable_types` where type required for inference

* fmt

* + test
  • Loading branch information
pq committed Oct 12, 2021
1 parent 5ee6393 commit 7e890e5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/src/rules/omit_local_variable_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

import '../analyzer.dart';
Expand Down Expand Up @@ -99,6 +100,20 @@ class _Visitor extends SimpleAstVisitor<void> {
_visitVariableDeclarationList(node.variables);
}

bool _dependsOnDeclaredTypeForInference(Expression? initializer) {
if (initializer is MethodInvocation) {
if (initializer.typeArguments == null) {
var element = initializer.methodName.staticElement;
if (element is FunctionElement) {
if (element.returnType is TypeParameterType) {
return true;
}
}
}
}
return false;
}

void _visitVariableDeclarationList(VariableDeclarationList node) {
var staticType = node.type?.type;
if (staticType == null ||
Expand All @@ -107,7 +122,11 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}
for (var child in node.variables) {
if (child.initializer?.staticType != staticType) {
var initializer = child.initializer;
if (initializer?.staticType != staticType) {
return;
}
if (_dependsOnDeclaredTypeForInference(initializer)) {
return;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'missing_whitespace_between_adjacent_strings.dart'
as missing_whitespace_between_adjacent_strings;
import 'non_constant_identifier_names.dart' as non_constant_identifier_names;
import 'null_closures.dart' as null_closures;
import 'omit_local_variable_types.dart' as omit_local_variable_types;
import 'overridden_fields.dart' as overridden_fields;
import 'prefer_asserts_in_initializer_lists.dart'
as prefer_asserts_in_initializer_lists;
Expand Down Expand Up @@ -43,6 +44,7 @@ void main() {
missing_whitespace_between_adjacent_strings.main();
non_constant_identifier_names.main();
null_closures.main();
omit_local_variable_types.main();
overridden_fields.main();
prefer_asserts_in_initializer_lists.main();
prefer_collection_literals.main();
Expand Down
60 changes: 60 additions & 0 deletions test/rules/omit_local_variable_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2021, 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(OmitLocalVariableTypesTest);
});
}

@reflectiveTest
class OmitLocalVariableTypesTest extends LintRuleTest {
@override
String get lintRule => 'omit_local_variable_types';

/// https://github.com/dart-lang/linter/issues/3016
@failingTest
test_paramIsType() async {
await assertDiagnostics(r'''
T bar<T>(T d) => d;
String f() {
String h = bar('');
return h;
}
''', [
lint('omit_local_variable_types', 42, 26),
]);
}

/// https://github.com/dart-lang/linter/issues/3016
test_typeNeededForInference() async {
await assertNoDiagnostics(r'''
T bar<T>(dynamic d) => d;
String f() {
String h = bar('');
return h;
}
''');
}

/// https://github.com/dart-lang/linter/issues/3016
test_typeParamProvided() async {
await assertDiagnostics(r'''
T bar<T>(dynamic d) => d;
String f() {
String h = bar<String>('');
return h;
}
''', [
lint('omit_local_variable_types', 42, 26),
]);
}
}

0 comments on commit 7e890e5

Please sign in to comment.