Linter: Extend exhaustive_cases to support Classes that are extended

Created on 24 Jun 2020  路  4Comments  路  Source: dart-lang/linter

Describe the rule you'd like to see implemented
I believe currently exhaustive_cases doesn't run if the class extents another class.
I would be nice if somehow it could support when it does. (There might be some complication I am not seeing)

Examples

Following is the way https://github.com/google/built_value.dart works with EnumClasses

class EnumClass {
  final String name;
  const EnumClass(this.name);
  @override
  String toString() => name;
}

class EnumLike extends EnumClass {
  const EnumLike._(String name) : super(name);

  static const a = EnumLike._('A');
  static const b = EnumLike._('B');
  static const c = EnumLike._('C');
}

void bad(EnumLike e) {
  // Enforce exhaustive_cases
  switch(e) {
    case EnumLike.a :
      print('a');
      break;
    case EnumLike.b :
      print('b');
      break;
  }
}
g3 enhancement

All 4 comments

Thanks for the report @pasindud!

I just added a test case for this example and it seems to pass with the current implementation... Is it possible that there is more to your EnumLike that's not included above?

Ah... is EnumLike _subclassed_ anywhere in your codebase?

Your right it works perfectly
Thanks for the help

You're welcome!

Was this page helpful?
0 / 5 - 0 ratings