Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Fix unrelated_type_equality_checks when comparing enums #4282

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/src/util/dart_type_utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion test_data/rules/unrelated_type_equality_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {}
Expand All @@ -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 }