Skip to content

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

Closed
bergwerf opened this issue Mar 27, 2016 · 6 comments
Closed

Analyzer strong mode inconsistent implicit cast warnings #26100

bergwerf opened this issue Mar 27, 2016 · 6 comments
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead.

Comments

@bergwerf
Copy link

The analyzer in strong mode gives me inconsistent implicit cast warnings.
For example, for this snippet I get a warning:

List<String> c;
if (b is List<String>) {
  c = b; // Unsound implicit cast from dynamic to List<String>
}

While this does not give me a warning:

String c;
if (b is String) {
  c = b;
}

(Dart 1.15.0)

@jmesserly
Copy link

Hi, this is intended behavior. Casts to types like List<String> are more likely to fail at runtime in Strong Mode, while these casts were accepted in original Dart. For example:

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.

CC @leafpetersen

@leafpetersen
Copy link
Member

What's the declared type of b? I can't reproduce this on any of the version of 1.15 or 1.16 that I have (using dynamic b as the static declaration of b).

Without the is check, this is working as intended, per John's comment above.

@jmesserly
Copy link

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.)

@bergwerf
Copy link
Author

The idea is that you don't know what b is. I have a class with a constructor that uses a Map<String, dynamic> as input type. All class fields are read from this data (could be JSON data) but to make it more safe I added a check like this:

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).

@bergwerf
Copy link
Author

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 IntList extends List<int> or something). For Maps this is a little more difficult since you often have to deal with key/value lists with many types (union types could offer a solution here).

@jmesserly
Copy link

ah, thanks for the clarification. this is a dupe so I'll consolidate the discussion into that bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

4 participants