Skip to content

Commit 74cc3f6

Browse files
eernstgCommit Queue
authored andcommitted
Define "obvious" types to include formal parameters
This CL changes the definition of what it takes for a expression to have an "obvious" type such that formal parameters are also included. For example `(int x) { int y = x; }` will be flagged by the lint `omit_obvious_local_variable_types` because `x` is considered to have an obvious type, so `y` shouldn't have a type annotation. I consider this to be a bug fix. Note that it is assumed that the declaration of the given formal parameter justifies the assumption that it does in fact have an obvious type; if this is not true then the recommended remedy will be to change the declaration of the formal parameter (by adding a type annotation). This is the same kind of treatment which is given to to local variables (where we may add a type annotation, or ensure that it has an initializing expression whose type is obvious). Change-Id: Id38c4d7660eb478293671e2b9785f4108a3313de Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434062 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Erik Ernst <[email protected]>
1 parent 9c86b8b commit 74cc3f6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pkg/linter/lib/src/util/obvious_types.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,13 @@ extension ExpressionExtensions on Expression {
164164
case SimpleIdentifier():
165165
if (self.isQualified) return false;
166166
var declaration = self.element;
167-
if (declaration is! LocalVariableElement) return false;
168-
return self.staticType == declaration.type;
167+
if (declaration is LocalVariableElement) {
168+
return self.staticType == declaration.type;
169+
}
170+
if (declaration is FormalParameterElement) {
171+
return self.staticType == declaration.type;
172+
}
173+
return false;
169174
case InstanceCreationExpression():
170175
var createdType = self.constructorName.type;
171176
if (createdType.typeArguments != null) {

pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,30 @@ var b = 'b';
344344
''');
345345
}
346346

347+
test_parameter() async {
348+
await assertDiagnostics(
349+
r'''
350+
void f(int parameter) {
351+
int i = parameter;
352+
}
353+
''',
354+
[lint(26, 3)],
355+
);
356+
}
357+
358+
test_parameter_of_literal() async {
359+
await assertDiagnostics(
360+
r'''
361+
Function f() {
362+
return (int parameter) {
363+
int i = parameter;
364+
};
365+
}
366+
''',
367+
[lint(46, 3)],
368+
);
369+
}
370+
347371
/// Types are considered an important part of the pattern so we
348372
/// intentionally do not lint on declared variable patterns.
349373
test_pattern_list_destructured() async {

0 commit comments

Comments
 (0)