|
| 1 | +// Copyright (c) 2021, 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:analyzer/dart/ast/ast.dart'; |
| 6 | +import 'package:analyzer/dart/element/element.dart'; |
| 7 | +import 'package:analyzer/error/listener.dart'; |
| 8 | +import 'package:analyzer/src/dart/error/hint_codes.dart'; |
| 9 | +import 'package:collection/collection.dart'; |
| 10 | + |
| 11 | +class UseResultVerifier { |
| 12 | + final ErrorReporter _errorReporter; |
| 13 | + |
| 14 | + UseResultVerifier(this._errorReporter); |
| 15 | + |
| 16 | + void checkMethodInvocation(MethodInvocation node) { |
| 17 | + var element = node.methodName.staticElement; |
| 18 | + if (element == null) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + _check(node, element); |
| 23 | + } |
| 24 | + |
| 25 | + void checkPropertyAccess(PropertyAccess node) { |
| 26 | + var element = node.propertyName.staticElement; |
| 27 | + if (element == null) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + _check(node, element); |
| 32 | + } |
| 33 | + |
| 34 | + void checkSimpleIdentifier(SimpleIdentifier node) { |
| 35 | + if (node.inDeclarationContext()) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + var parent = node.parent; |
| 40 | + // Covered by checkPropertyAccess and checkMethodInvocation respectively. |
| 41 | + if (parent is PropertyAccess || parent is MethodInvocation) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var element = node.staticElement; |
| 46 | + if (element == null) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + _check(node, element); |
| 51 | + } |
| 52 | + |
| 53 | + void _check(AstNode node, Element element) { |
| 54 | + var annotation = _getUseResultMetadata(element); |
| 55 | + if (annotation == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (_isUsed(node)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + var displayName = element.displayName; |
| 64 | + |
| 65 | + var message = _getUseResultMessage(annotation); |
| 66 | + if (message == null || message.isEmpty) { |
| 67 | + _errorReporter |
| 68 | + .reportErrorForNode(HintCode.UNUSED_RESULT, node, [displayName]); |
| 69 | + } else { |
| 70 | + _errorReporter.reportErrorForNode( |
| 71 | + HintCode.UNUSED_RESULT_WITH_MESSAGE, node, [displayName, message]); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static String? _getUseResultMessage(ElementAnnotation annotation) { |
| 76 | + if (annotation.element is PropertyAccessorElement) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + var constantValue = annotation.computeConstantValue(); |
| 80 | + return constantValue?.getField('message')?.toStringValue(); |
| 81 | + } |
| 82 | + |
| 83 | + static ElementAnnotation? _getUseResultMetadata(Element element) { |
| 84 | + // Implicit getters/setters. |
| 85 | + if (element.isSynthetic && element is PropertyAccessorElement) { |
| 86 | + element = element.variable; |
| 87 | + } |
| 88 | + return element.metadata.firstWhereOrNull((e) => e.isUseResult); |
| 89 | + } |
| 90 | + |
| 91 | + static bool _isUsed(AstNode node) { |
| 92 | + var parent = node.parent; |
| 93 | + if (parent == null) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (parent is ParenthesizedExpression || parent is ConditionalExpression) { |
| 98 | + return _isUsed(parent); |
| 99 | + } |
| 100 | + |
| 101 | + return parent is ArgumentList || |
| 102 | + parent is VariableDeclaration || |
| 103 | + parent is MethodInvocation || |
| 104 | + parent is PropertyAccess || |
| 105 | + parent is ExpressionFunctionBody || |
| 106 | + parent is ReturnStatement || |
| 107 | + parent is FunctionExpressionInvocation || |
| 108 | + parent is ListLiteral || |
| 109 | + parent is SetOrMapLiteral || |
| 110 | + parent is MapLiteralEntry; |
| 111 | + } |
| 112 | +} |
0 commit comments