Skip to content

Commit 8a868ed

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate avoid_implementing_value_types
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: If4b94dcc815a1f83372b91e8c1e67afd2eae996f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389560 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 79c3917 commit 8a868ed

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ lib/src/rules/avoid_empty_else.dart
2222
lib/src/rules/avoid_escaping_inner_quotes.dart
2323
lib/src/rules/avoid_final_parameters.dart
2424
lib/src/rules/avoid_function_literals_in_foreach_calls.dart
25+
lib/src/rules/avoid_implementing_value_types.dart
2526
lib/src/rules/avoid_js_rounded_ints.dart
2627
lib/src/rules/avoid_multiple_declarations_per_line.dart
2728
lib/src/rules/avoid_print.dart
@@ -139,6 +140,7 @@ lib/src/rules/sort_constructors_first.dart
139140
lib/src/rules/sort_unnamed_constructors_first.dart
140141
lib/src/rules/specify_nonobvious_local_variable_types.dart
141142
lib/src/rules/super_goes_last.dart
143+
lib/src/rules/test_types_in_equals.dart
142144
lib/src/rules/throw_in_finally.dart
143145
lib/src/rules/tighten_type_of_initializing_formals.dart
144146
lib/src/rules/type_annotate_public_apis.dart

pkg/linter/lib/src/rules/avoid_implementing_value_types.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:analyzer/dart/element/type.dart';
99

1010
import '../analyzer.dart';
@@ -24,34 +24,39 @@ class AvoidImplementingValueTypes extends LintRule {
2424
@override
2525
void registerNodeProcessors(
2626
NodeLintRegistry registry, LinterContext context) {
27-
var visitor = _Visitor(this);
27+
var visitor = _Visitor(this, context.inheritanceManager);
2828
registry.addClassDeclaration(this, visitor);
2929
}
3030
}
3131

3232
class _Visitor extends SimpleAstVisitor<void> {
33+
static var equalsName = Name(null, '==');
34+
3335
final LintRule rule;
36+
final InheritanceManager3 inheritanceManager;
3437

35-
_Visitor(this.rule);
38+
_Visitor(this.rule, this.inheritanceManager);
3639

3740
@override
3841
void visitClassDeclaration(ClassDeclaration node) {
3942
var implementsClause = node.implementsClause;
4043
if (implementsClause == null) {
4144
return;
4245
}
46+
4347
for (var interface in implementsClause.interfaces) {
4448
var interfaceType = interface.type;
4549
if (interfaceType is InterfaceType &&
46-
_overridesEquals(interfaceType.element)) {
50+
_overridesEquals(interfaceType.element3)) {
4751
rule.reportLint(interface);
4852
}
4953
}
5054
}
5155

52-
static bool _overridesEquals(InterfaceElement element) {
53-
var method = element.lookUpConcreteMethod('==', element.library);
54-
var enclosing = method?.enclosingElement3;
55-
return enclosing is ClassElement && !enclosing.isDartCoreObject;
56+
bool _overridesEquals(InterfaceElement2 element) {
57+
var member =
58+
inheritanceManager.getMember4(element, equalsName, concrete: true);
59+
var definingLibrary = member?.enclosingElement2?.library2;
60+
return definingLibrary != null && !definingLibrary.isDartCore;
5661
}
5762
}

pkg/linter/test/rules/avoid_implementing_value_types_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class C implements A {}
7575
]);
7676
}
7777

78+
test_implementsClassWithoutEqualEqual() async {
79+
await assertNoDiagnostics(r'''
80+
class A {}
81+
class C implements A {}
82+
''');
83+
}
84+
7885
test_mixin() async {
7986
await assertNoDiagnostics(r'''
8087
mixin M {

0 commit comments

Comments
 (0)