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

Commit 02faee6

Browse files
authored
enhanced enum support for hash_and_equals (#3202)
* enhanced enum support for `hash_and_equals` * rename * fmt
1 parent 69c2f6d commit 02faee6

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

lib/src/rules/hash_and_equals.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class HashAndEquals extends LintRule {
6363
NodeLintRegistry registry, LinterContext context) {
6464
var visitor = _Visitor(this);
6565
registry.addClassDeclaration(this, visitor);
66+
registry.addEnumDeclaration(this, visitor);
6667
}
6768
}
6869

@@ -80,9 +81,18 @@ class _Visitor extends SimpleAstVisitor<void> {
8081

8182
@override
8283
void visitClassDeclaration(ClassDeclaration node) {
84+
_check(node.members);
85+
}
86+
87+
@override
88+
void visitEnumDeclaration(EnumDeclaration node) {
89+
_check(node.members);
90+
}
91+
92+
void _check(NodeList<ClassMember> members) {
8393
MethodDeclaration? eq;
8494
ClassMember? hash;
85-
for (var member in node.members) {
95+
for (var member in members) {
8696
if (isEquals(member)) {
8797
eq = member as MethodDeclaration;
8898
} else if (isHashCode(member)) {

test/rules/all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'avoid_unused_constructor_parameters.dart'
1818
import 'conditional_uri_does_not_exist.dart' as conditional_uri_does_not_exist;
1919
import 'deprecated_consistency.dart' as deprecated_consistency;
2020
import 'file_names.dart' as file_names;
21+
import 'hash_and_equals.dart' as hash_and_equals;
2122
import 'literal_only_boolean_expressions.dart'
2223
as literal_only_boolean_expressions;
2324
import 'missing_whitespace_between_adjacent_strings.dart'
@@ -63,6 +64,7 @@ void main() {
6364
conditional_uri_does_not_exist.main();
6465
deprecated_consistency.main();
6566
file_names.main();
67+
hash_and_equals.main();
6668
literal_only_boolean_expressions.main();
6769
missing_whitespace_between_adjacent_strings.main();
6870
non_constant_identifier_names.main();

test/rules/hash_and_equals.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(HashAndEqualsTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class HashAndEqualsTest extends LintRuleTest {
17+
@override
18+
List<String> get experiments => [
19+
EnableString.enhanced_enums,
20+
];
21+
22+
@override
23+
String get lintRule => 'hash_and_equals';
24+
25+
@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/3195')
26+
test_enum_missingHash() async {
27+
await assertDiagnostics(r'''
28+
enum A {
29+
a,b,c;
30+
@override
31+
bool operator ==(Object other) => false;
32+
}
33+
''', [
34+
lint('hash_and_equals', 46, 2), // todo(pq): fix index
35+
]);
36+
}
37+
}

0 commit comments

Comments
 (0)