Skip to content

Commit

Permalink
Elements. Migrate UseResultVerifier.
Browse files Browse the repository at this point in the history
Change-Id: I7e94dac09e52d548db43dd6060a9e8f10bf2a309
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395123
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 14, 2024
1 parent 0015935 commit d064073
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
1 change: 0 additions & 1 deletion pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ lib/src/error/nullable_dereference_verifier.dart
lib/src/error/override_verifier.dart
lib/src/error/required_parameters_verifier.dart
lib/src/error/unused_local_elements_verifier.dart
lib/src/error/use_result_verifier.dart
lib/src/generated/element_resolver.dart
lib/src/generated/element_walker.dart
lib/src/generated/error_detection_helpers.dart
Expand Down
49 changes: 32 additions & 17 deletions pkg/analyzer/lib/src/error/use_result_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/error/codes.g.dart';
Expand All @@ -15,7 +15,7 @@ class UseResultVerifier {
UseResultVerifier(this._errorReporter);

void checkFunctionExpressionInvocation(FunctionExpressionInvocation node) {
var element = node.staticElement;
var element = node.element;
if (element == null) {
return;
}
Expand All @@ -24,7 +24,7 @@ class UseResultVerifier {
}

void checkInstanceCreationExpression(InstanceCreationExpression node) {
var element = node.constructorName.staticElement;
var element = node.constructorName.element;
if (element == null) {
return;
}
Expand All @@ -33,7 +33,7 @@ class UseResultVerifier {
}

void checkMethodInvocation(MethodInvocation node) {
var element = node.methodName.staticElement;
var element = node.methodName.element;
if (element == null) {
return;
}
Expand All @@ -42,7 +42,7 @@ class UseResultVerifier {
}

void checkPropertyAccess(PropertyAccess node) {
var element = node.propertyName.staticElement;
var element = node.propertyName.element;
if (element == null) {
return;
}
Expand All @@ -64,15 +64,15 @@ class UseResultVerifier {
return;
}

var element = node.staticElement;
var element = node.element;
if (element == null) {
return;
}

_check(node, element);
}

void _check(AstNode node, Element element) {
void _check(AstNode node, Element2 element) {
var parent = node.parent;
if (parent is PrefixedIdentifier) {
parent = parent.parent;
Expand Down Expand Up @@ -123,7 +123,7 @@ class UseResultVerifier {
}

String? _getUseResultMessage(ElementAnnotation annotation) {
if (annotation.element is PropertyAccessorElement) {
if (annotation.element2 is GetterElement) {
return null;
}
var constantValue = annotation.computeConstantValue();
Expand All @@ -146,13 +146,13 @@ class UseResultVerifier {
}

var argumentList = node.argumentList as ArgumentListImpl;
var parameters = argumentList.correspondingStaticParameters;
var parameters = argumentList.correspondingStaticParameters2;
if (parameters == null) {
return false;
}

for (var param in parameters) {
var name = param?.name;
var name = param?.name3;
if (unlessParam == name) {
return true;
}
Expand All @@ -174,16 +174,31 @@ class UseResultVerifier {
return node;
}

static ElementAnnotation? _getUseResultMetadata(Element element) {
static ElementAnnotation? _getUseResultMetadata(Element2 element) {
// Implicit getters/setters.
if (element.isSynthetic && element is PropertyAccessorElement) {
var variable = element.variable2;
if (variable == null) {
return null;
if (element.isSynthetic) {
switch (element) {
case GetterElement getter:
if (getter.variable3 case var variable?) {
element = variable;
} else {
return null;
}
case SetterElement setter:
if (setter.variable3 case var variable?) {
element = variable;
} else {
return null;
}
}
element = variable;
}
return element.metadata.firstWhereOrNull((e) => e.isUseResult);

if (element case Annotatable annotatable) {
return annotatable.metadata2.annotations
.firstWhereOrNull((e) => e.isUseResult);
}

return null;
}

static bool _isUsed(AstNode node) {
Expand Down

0 comments on commit d064073

Please sign in to comment.