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

Commit 82e7147

Browse files
Fix unrelated_type_equality_checks when comparing enums (#4282)
* Fix unrelated_type_equality_checks when comparing enums * format * Fix Mixin * add test case * remove true && * add more test cases * format * remove mixin logic
1 parent d8ccf81 commit 82e7147

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/src/util/dart_type_utilities.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,16 @@ extension on TypeSystem {
288288
// Otherwise, they might be related.
289289
return false;
290290
} else {
291+
var sameSupertypes = leftElement.supertype == rightElement.supertype;
292+
293+
// Unrelated Enums have the same supertype, but they are not the same element, so
294+
// they are unrelated.
295+
if (sameSupertypes && leftElement is EnumElement) {
296+
return true;
297+
}
298+
291299
return (leftElement.supertype?.isDartCoreObject ?? false) ||
292-
leftElement.supertype != rightElement.supertype;
300+
!sameSupertypes;
293301
}
294302
}
295303
}

test_data/rules/unrelated_type_equality_checks.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void function23() {
167167
}
168168

169169
void function30() {
170-
ClassWMethod c = ClassWMethod();
170+
ClassWMethod c = ClassWMethod();
171171
if (c.determinant == 0.0) print('someFunction30'); // LINT
172172
if (c.determinant == double) print('someFunction30'); // LINT
173173
if (c.determinant == c.determinspider) print('someFunction30'); // OK
@@ -178,6 +178,24 @@ void function30() {
178178
if (c.determinant == callable2) print('someFunction30'); // OK
179179
}
180180

181+
void function31() {
182+
var x = EnumOpKind1.delete;
183+
if (x == EnumOpKind1.delete) print('delete'); // OK
184+
if (x == EnumOpKind1.update) print('update'); // OK
185+
if (x == EnumOpKind2.delete) print('delete'); // LINT
186+
if (x == 'delete') print('delete'); // LINT
187+
}
188+
189+
void function32() {
190+
var x = EnumImplements.a;
191+
if (x == EnumImplements.a) print('a'); // OK
192+
if (x == EnumImplements.b) print('b'); // OK
193+
if (x == EnumMixin.b) print('b'); // LINT
194+
195+
var y = EnumMixin.a;
196+
if (y == EnumImplements.a) print('a'); // LINT
197+
}
198+
181199
class ClassBase {}
182200

183201
class DerivedClass1 extends ClassBase {}
@@ -203,3 +221,11 @@ class ClassWCall {
203221
}
204222

205223
class SubClassWCall extends ClassWCall {}
224+
225+
enum EnumOpKind1 { insert, update, delete }
226+
227+
enum EnumOpKind2 { upsert, delete }
228+
229+
enum EnumImplements implements ClassBase { a, b, c }
230+
231+
enum EnumMixin with Mixin { a, b, c }

0 commit comments

Comments
 (0)