Skip to content

Commit 79e8869

Browse files
eernstgCommit Queue
authored and
Commit Queue
committed
With ...obvious... lints, treat dynamic like other types
The lints `omit_obvious_local_variable_types` and `specify_nonobvious_local_variable_types` have previously treated the type `dynamic` in a special way. This CL changes these lints such that they treat `dynamic` as the type annotation of a local variable just like any other type used in the same situation. This means that `dynamic d = 1;` will no longer be linted by `specify_nonobvious_local_variable_types` (note that the type of `1` is currently considered non-obvious), but `dynamic d = 1 as dynamic;` will now be linted by `omit_obvious_local_variable_types`. Change-Id: Ib04b38c2aad8512839e90c29e4021487ad853e7e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381500 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Erik Ernst <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 045d26b commit 79e8869

4 files changed

+30
-6
lines changed

pkg/linter/lib/src/rules/omit_obvious_local_variable_types.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ class _Visitor extends SimpleAstVisitor<void> {
136136

137137
void _visitVariableDeclarationList(VariableDeclarationList node) {
138138
var staticType = node.type?.type;
139-
if (staticType == null ||
140-
staticType is DynamicType ||
141-
staticType.isDartCoreNull) {
139+
if (staticType == null || staticType.isDartCoreNull) {
142140
return;
143141
}
144142
for (var child in node.variables) {

pkg/linter/lib/src/rules/specify_nonobvious_local_variable_types.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ class _Visitor extends SimpleAstVisitor<void> {
184184

185185
void _visitVariableDeclarationList(VariableDeclarationList node) {
186186
var staticType = node.type?.type;
187-
if (staticType != null &&
188-
staticType is! DynamicType &&
189-
!staticType.isDartCoreNull) {
187+
if (staticType != null && !staticType.isDartCoreNull) {
190188
return;
191189
}
192190
bool aDeclaredTypeIsNeeded = false;

pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ num n = 1;
2929
]);
3030
}
3131

32+
test_as_dynamic() async {
33+
await assertDiagnostics(r'''
34+
f() {
35+
dynamic i = n as dynamic;
36+
}
37+
38+
num n = 1;
39+
''', [
40+
lint(8, 7),
41+
]);
42+
}
43+
3244
test_cascade() async {
3345
await assertDiagnostics(r'''
3446
f() {

pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ class SpecifyNonObviousLocalVariableTypesTest extends LintRuleTest {
1717
@override
1818
String get lintRule => 'specify_nonobvious_local_variable_types';
1919

20+
test_as() async {
21+
await assertNoDiagnostics(r'''
22+
f() {
23+
var d = 1 as num;
24+
}
25+
''');
26+
}
27+
28+
test_as_dynamic() async {
29+
await assertNoDiagnostics(r'''
30+
f() {
31+
var d = 1 as dynamic;
32+
}
33+
''');
34+
}
35+
2036
test_cascade() async {
2137
await assertNoDiagnostics(r'''
2238
f() {

0 commit comments

Comments
 (0)