Misleading dartanalyzer hint. #20144
Labels
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
P1
A high priority bug; for example, a single project is unusable or has many test failures
Milestone
The following program produces a hint that 'replaceAll' is not defined on class 'Map'.
$ cat main.dart
Map foo() => null;
main() {
var x = foo();
if (x != null) x = '$x';
if (x != null) {
x.replaceAll('x', 'y');
}
}
$ dartanalyzer main.dart
Analyzing [main.dart]...
[hint] The method 'replaceAll' is not defined for the class 'Map' (/.../main.dart, line 8, col 7)
No issues found
This hint is misleading, since
- the static type of [x] is dynamic
- the concrete type at runtime at the point of 'x.replaceAll()' is 'String' (which has a 'replaceAll' method).
This hint seems to be not coming from the actual static type checker, but rather from the type inferrer.
I'm not sure how it's implemented, but if dartanalyzer infers a type for [x], the inferred type should be the union of the types of all assignments to [x] -- which would in this case be Union[Map,String] and that type can clearly have a 'replaceAll' method.
Example where we use this is:
dart/pkg/oauth2/lib/src/handle_access_token_response.dart:
var contentType = response.headers['content-type'];
if (contentType != null) contentType = new MediaType.parse(contentType);
validate(contentType != null &&
(contentType.mimeType == "application/json" ||
contentType.mimeType == "text/javascript"),
'content-type was "$contentType", expected "application/json"');
Here the hint is that [mimeType] is not a member of 'String'.
The text was updated successfully, but these errors were encountered: