diff --git a/lib/src/util/dart_type_utilities.dart b/lib/src/util/dart_type_utilities.dart index c6987bbb1..15f10738b 100644 --- a/lib/src/util/dart_type_utilities.dart +++ b/lib/src/util/dart_type_utilities.dart @@ -288,8 +288,16 @@ extension on TypeSystem { // Otherwise, they might be related. return false; } else { + var sameSupertypes = leftElement.supertype == rightElement.supertype; + + // Unrelated Enums have the same supertype, but they are not the same element, so + // they are unrelated. + if (sameSupertypes && leftElement is EnumElement) { + return true; + } + return (leftElement.supertype?.isDartCoreObject ?? false) || - leftElement.supertype != rightElement.supertype; + !sameSupertypes; } } } diff --git a/test_data/rules/unrelated_type_equality_checks.dart b/test_data/rules/unrelated_type_equality_checks.dart index 012475b5c..c0dfdc516 100644 --- a/test_data/rules/unrelated_type_equality_checks.dart +++ b/test_data/rules/unrelated_type_equality_checks.dart @@ -167,7 +167,7 @@ void function23() { } void function30() { - ClassWMethod c = ClassWMethod(); + ClassWMethod c = ClassWMethod(); if (c.determinant == 0.0) print('someFunction30'); // LINT if (c.determinant == double) print('someFunction30'); // LINT if (c.determinant == c.determinspider) print('someFunction30'); // OK @@ -178,6 +178,24 @@ void function30() { if (c.determinant == callable2) print('someFunction30'); // OK } +void function31() { + var x = EnumOpKind1.delete; + if (x == EnumOpKind1.delete) print('delete'); // OK + if (x == EnumOpKind1.update) print('update'); // OK + if (x == EnumOpKind2.delete) print('delete'); // LINT + if (x == 'delete') print('delete'); // LINT +} + +void function32() { + var x = EnumImplements.a; + if (x == EnumImplements.a) print('a'); // OK + if (x == EnumImplements.b) print('b'); // OK + if (x == EnumMixin.b) print('b'); // LINT + + var y = EnumMixin.a; + if (y == EnumImplements.a) print('a'); // LINT +} + class ClassBase {} class DerivedClass1 extends ClassBase {} @@ -203,3 +221,11 @@ class ClassWCall { } class SubClassWCall extends ClassWCall {} + +enum EnumOpKind1 { insert, update, delete } + +enum EnumOpKind2 { upsert, delete } + +enum EnumImplements implements ClassBase { a, b, c } + +enum EnumMixin with Mixin { a, b, c }