-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analyzer strong mode inconsistent implicit cast warnings #26100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, this is intended behavior. Casts to types like var x = <dynamic>[1, 'hello'];
List<String> y = x; // WARNING Here the cast will always fail on the second line, so we provide that information statically. |
What's the declared type of Without the is check, this is working as intended, per John's comment above. |
See also #24565 for further discussion. We're open to changing this behavior in the long term, it was intended to make it easier to find potential runtime cast failures as we upgrade code to Strong Mode. (I think we can dupe it against that bug unless it turns out there's a type-promotion issue.) |
The idea is that you don't know what factory fromData(Map<String, dynamic> data) {
List<int> field;
if(data.containsKey('field') && data['field'] is List<int>) {
field = data['field'] as List<int>;
} else {
throw new ArgumentError.value(data, 'data', "no valid data field for 'field'");
}
return new MyClass(field);
} (I use a factory because the fields in MyClass are final). |
Anyway, as pointed out in #24565, this is intended behavior since generics type parameters are not considered by the analyzer (for this warning at least). I think that especially for List it would be useful if the strong mode only allows explicit types and also uses these for checking (you could even get around this by defining an |
ah, thanks for the clarification. this is a dupe so I'll consolidate the discussion into that bug |
The analyzer in strong mode gives me inconsistent implicit cast warnings.
For example, for this snippet I get a warning:
While this does not give me a warning:
(Dart 1.15.0)
The text was updated successfully, but these errors were encountered: