diff --git a/test/rules/all.dart b/test/rules/all.dart index a0c50b0aa..d6f11f5ff 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -124,6 +124,7 @@ import 'sort_constructors_first_test.dart' as sort_constructors_first; import 'sort_pub_dependencies_test.dart' as sort_pub_dependencies; import 'sort_unnamed_constructors_first_test.dart' as sort_unnamed_constructors_first; +import 'test_types_in_equals_test.dart' as test_types_in_equals; import 'throw_in_finally_test.dart' as throw_in_finally; import 'tighten_type_of_initializing_formals_test.dart' as tighten_type_of_initializing_formals; @@ -272,6 +273,7 @@ void main() { sort_constructors_first.main(); sort_pub_dependencies.main(); sort_unnamed_constructors_first.main(); + test_types_in_equals.main(); throw_in_finally.main(); tighten_type_of_initializing_formals.main(); type_init_formals.main(); diff --git a/test/rules/test_types_in_equals_test.dart b/test/rules/test_types_in_equals_test.dart new file mode 100644 index 000000000..72c862551 --- /dev/null +++ b/test/rules/test_types_in_equals_test.dart @@ -0,0 +1,51 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../rule_test_support.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(TestTypesInEqualsTest); + }); +} + +@reflectiveTest +class TestTypesInEqualsTest extends LintRuleTest { + @override + String get lintRule => 'test_types_in_equals'; + + test_usesIs() async { + await assertNoDiagnostics(r''' +class C { + int? x; + + @override + bool operator ==(Object other) { + if (identical(this, other)) { + return true; + } + return other is C && this.x == other.x; + } +} +'''); + } + + test_doesNotUseIs() async { + await assertDiagnostics(r''' +class C { + int? x; + + @override + bool operator ==(Object other) { + C otherC = other as C; + return otherC.x == x; + } +} +''', [ + lint(83, 10), + ]); + } +} diff --git a/test_data/rules/test_types_in_equals.dart b/test_data/rules/test_types_in_equals.dart deleted file mode 100644 index b34bc0b31..000000000 --- a/test_data/rules/test_types_in_equals.dart +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -// test w/ `dart test -N test_types_in_equals` - -class Field {} - -class Good { - final Field someField; - - Good(this.someField); - - @override - bool operator ==(Object other) { - if (identical(this, other)) { - return true; - } - return other is Good && this.someField == other.someField; - } - - @override - int get hashCode { - return someField.hashCode; - } -} - -class Bad { - final Field someField; - - Bad(this.someField); - - @override - bool operator ==(Object other) { - Bad otherBad = other as Bad; // LINT - return otherBad != null && otherBad.someField == someField; - } - - @override - int get hashCode { - return someField.hashCode; - } -}