Skip to content

Commit 0f4883e

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate leak_detector
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I884c138719f046f641bf601f8be4e46a8348fe47 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394387 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 5cd0517 commit 0f4883e

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

pkg/linter/lib/src/util/leak_detector_visitor.dart

Lines changed: 29 additions & 25 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
import 'package:meta/meta.dart';
1010

@@ -24,7 +24,7 @@ _VisitVariableDeclaration _buildVariableReporter(
2424
return;
2525
}
2626

27-
var variableElement = variable.declaredElement;
27+
var variableElement = variable.declaredFragment?.element;
2828
if (variableElement == null) {
2929
return;
3030
}
@@ -56,39 +56,35 @@ bool _hasMatch(Map<DartTypePredicate, String> predicates, DartType type,
5656
predicates.keys.any((p) => predicates[p] == methodName && p(type));
5757

5858
bool _isElementEqualToVariable(
59-
Element? propertyElement, VariableElement? variableElement) =>
59+
Element2? propertyElement, VariableElement2? variableElement) =>
6060
propertyElement == variableElement ||
61-
propertyElement is PropertyAccessorElement &&
62-
propertyElement.variable2 == variableElement;
61+
propertyElement.matches(variableElement);
6362

6463
bool _isInvocationThroughCascadeExpression(
65-
MethodInvocation invocation, VariableElement variableElement) {
64+
MethodInvocation invocation, VariableElement2 variableElement) {
6665
if (invocation.realTarget is! SimpleIdentifier) {
6766
return false;
6867
}
6968

7069
var identifier = invocation.realTarget;
7170
if (identifier is SimpleIdentifier) {
72-
var element = identifier.staticElement;
73-
if (element is PropertyAccessorElement) {
74-
return element.variable2 == variableElement;
75-
}
71+
return identifier.element.matches(variableElement);
7672
}
7773
return false;
7874
}
7975

8076
bool _isPostfixExpressionOperandEqualToVariable(
81-
AstNode? n, VariableElement variableElement) {
77+
AstNode? n, VariableElement2 variableElement) {
8278
if (n is PostfixExpression) {
8379
var operand = n.operand;
8480
return operand is SimpleIdentifier &&
85-
_isElementEqualToVariable(operand.staticElement, variableElement);
81+
_isElementEqualToVariable(operand.element, variableElement);
8682
}
8783
return false;
8884
}
8985

9086
bool _isPropertyAccessThroughThis(
91-
Expression? n, VariableElement variableElement) {
87+
Expression? n, VariableElement2 variableElement) {
9288
if (n is! PropertyAccess) {
9389
return false;
9490
}
@@ -98,14 +94,14 @@ bool _isPropertyAccessThroughThis(
9894
return false;
9995
}
10096

101-
var propertyElement = n.propertyName.staticElement;
97+
var propertyElement = n.propertyName.element;
10298
return _isElementEqualToVariable(propertyElement, variableElement);
10399
}
104100

105101
bool _isSimpleIdentifierElementEqualToVariable(
106-
AstNode? n, VariableElement variableElement) =>
102+
AstNode? n, VariableElement2 variableElement) =>
107103
n is SimpleIdentifier &&
108-
_isElementEqualToVariable(n.staticElement, variableElement);
104+
_isElementEqualToVariable(n.element, variableElement);
109105

110106
typedef DartTypePredicate = bool Function(DartType type);
111107

@@ -164,7 +160,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
164160

165161
/// The element of the variable under consideration; stored here as a non-
166162
/// `null` value.
167-
final VariableElement variableElement;
163+
final VariableElement2 variableElement;
168164

169165
/// The predicates that determine whether a method call or method tear-off is
170166
/// a valid use.
@@ -190,7 +186,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
190186
// Being assigned another reference.
191187
if (node.rightHandSide is SimpleIdentifier) {
192188
if (_isElementEqualToVariable(
193-
node.writeElement, variable.declaredElement)) {
189+
node.writeElement2, variable.declaredFragment?.element)) {
194190
containsValidUse = true;
195191
return;
196192
}
@@ -207,7 +203,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
207203

208204
@override
209205
void visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
210-
if (node.fieldName.staticElement == variableElement) {
206+
if (node.fieldName.element == variableElement) {
211207
containsValidUse = true;
212208
return;
213209
}
@@ -217,9 +213,9 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
217213
@override
218214
void visitFieldFormalParameter(FieldFormalParameter node) {
219215
if (variableType == _VariableType.field) {
220-
var staticElement = node.declaredElement;
221-
if (staticElement is FieldFormalParameterElement &&
222-
staticElement.field == variableElement) {
216+
var staticElement = node.declaredFragment?.element;
217+
if (staticElement is FieldFormalParameterElement2 &&
218+
staticElement.field2 == variableElement) {
223219
containsValidUse = true;
224220
return;
225221
}
@@ -247,7 +243,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
247243

248244
if (node.argumentList.arguments
249245
.whereType<SimpleIdentifier>()
250-
.map((e) => e.staticElement)
246+
.map((e) => e.element)
251247
.contains(variableElement)) {
252248
// If any function is invoked with our variable, we suppress lints. This
253249
// is because it is not so uncommon to invoke the target method there. We
@@ -264,7 +260,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
264260

265261
@override
266262
void visitPrefixedIdentifier(PrefixedIdentifier node) {
267-
if (node.prefix.staticElement == variableElement &&
263+
if (node.prefix.element == variableElement &&
268264
_hasMatch(
269265
predicates, variableElement.type, node.identifier.token.lexeme)) {
270266
containsValidUse = true;
@@ -278,7 +274,7 @@ class _ValidUseVisitor extends RecursiveAstVisitor<void> {
278274
if (variableType == _VariableType.local) {
279275
var expression = node.expression;
280276
if (expression is SimpleIdentifier &&
281-
expression.staticElement == variableElement) {
277+
expression.element == variableElement) {
282278
containsValidUse = true;
283279
return;
284280
}
@@ -292,3 +288,11 @@ enum _VariableType {
292288
field,
293289
local;
294290
}
291+
292+
extension on Element2? {
293+
bool matches(VariableElement2? variable) => switch (this) {
294+
GetterElement(:var variable3) => variable3 == variable,
295+
SetterElement(:var variable3) => variable3 == variable,
296+
_ => false,
297+
};
298+
}

0 commit comments

Comments
 (0)