|
| 1 | +// Copyright (c) 2025, 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/token.dart'; |
| 6 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 7 | +import 'package:analyzer/dart/element/nullability_suffix.dart'; |
| 8 | +import 'package:analyzer/dart/element/type.dart'; |
| 9 | +// ignore: implementation_imports |
| 10 | +import 'package:analyzer/src/dart/ast/ast.dart'; |
| 11 | + |
| 12 | +import '../analyzer.dart'; |
| 13 | + |
| 14 | +const _desc = r'No await no async.'; |
| 15 | + |
| 16 | +class UnnecessaryAsync extends LintRule { |
| 17 | + UnnecessaryAsync() |
| 18 | + : super( |
| 19 | + name: LintNames.unnecessary_async, |
| 20 | + description: _desc, |
| 21 | + state: const State.experimental(), |
| 22 | + ); |
| 23 | + |
| 24 | + @override |
| 25 | + LintCode get lintCode => LinterLintCode.unnecessary_async; |
| 26 | + |
| 27 | + @override |
| 28 | + void registerNodeProcessors( |
| 29 | + NodeLintRegistry registry, |
| 30 | + LinterContext context, |
| 31 | + ) { |
| 32 | + var visitor = _Visitor(this); |
| 33 | + registry.addFunctionDeclaration(this, visitor); |
| 34 | + registry.addFunctionExpression(this, visitor); |
| 35 | + registry.addMethodDeclaration(this, visitor); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class _HasAwaitVisitor extends RecursiveAstVisitor<void> { |
| 40 | + bool hasAwait = false; |
| 41 | + bool everyReturnHasValue = true; |
| 42 | + bool returnsOnlyFuture = true; |
| 43 | + |
| 44 | + @override |
| 45 | + void visitAwaitExpression(AwaitExpression node) { |
| 46 | + hasAwait = true; |
| 47 | + super.visitAwaitExpression(node); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + void visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 52 | + _updateWithExpression(node.expression); |
| 53 | + super.visitExpressionFunctionBody(node); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + void visitForElement(ForElement node) { |
| 58 | + hasAwait |= node.awaitKeyword != null; |
| 59 | + super.visitForElement(node); |
| 60 | + } |
| 61 | + |
| 62 | + @override |
| 63 | + void visitForStatement(ForStatement node) { |
| 64 | + hasAwait |= node.awaitKeyword != null; |
| 65 | + super.visitForStatement(node); |
| 66 | + } |
| 67 | + |
| 68 | + @override |
| 69 | + void visitFunctionExpression(FunctionExpression node) { |
| 70 | + // Stop the recursion. |
| 71 | + } |
| 72 | + |
| 73 | + @override |
| 74 | + void visitReturnStatement(ReturnStatement node) { |
| 75 | + var expression = node.expression; |
| 76 | + if (expression != null) { |
| 77 | + _updateWithExpression(expression); |
| 78 | + } else { |
| 79 | + everyReturnHasValue = false; |
| 80 | + } |
| 81 | + |
| 82 | + super.visitReturnStatement(node); |
| 83 | + } |
| 84 | + |
| 85 | + void _updateWithExpression(Expression expression) { |
| 86 | + var type = expression.staticType; |
| 87 | + if (!(type is InterfaceType && |
| 88 | + type.isDartAsyncFutureOrSubtype && |
| 89 | + type.nullabilitySuffix == NullabilitySuffix.none)) { |
| 90 | + returnsOnlyFuture = false; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class _Visitor extends SimpleAstVisitor<void> { |
| 96 | + final LintRule rule; |
| 97 | + |
| 98 | + _Visitor(this.rule); |
| 99 | + |
| 100 | + @override |
| 101 | + void visitFunctionDeclaration(covariant FunctionDeclarationImpl node) { |
| 102 | + var element = node.declaredFragment!.element; |
| 103 | + |
| 104 | + _checkBody( |
| 105 | + body: node.functionExpression.body, |
| 106 | + returnType: element.returnType, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + @override |
| 111 | + void visitFunctionExpression(covariant FunctionExpressionImpl node) { |
| 112 | + // Here we handle only closures. |
| 113 | + if (node.parent is FunctionDeclaration) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + var bodyContext = node.body.bodyContext; |
| 118 | + |
| 119 | + _checkBody( |
| 120 | + body: node.body, |
| 121 | + returnType: bodyContext?.imposedType, |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + void visitMethodDeclaration(covariant MethodDeclarationImpl node) { |
| 127 | + var element = node.declaredFragment!.element; |
| 128 | + |
| 129 | + _checkBody( |
| 130 | + body: node.body, |
| 131 | + returnType: element.returnType, |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + void _checkBody({ |
| 136 | + required FunctionBodyImpl body, |
| 137 | + required DartType? returnType, |
| 138 | + }) { |
| 139 | + var asyncKeyword = body.keyword; |
| 140 | + if (asyncKeyword == null || asyncKeyword.keyword != Keyword.ASYNC) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (body.star != null) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + var bodyContext = body.bodyContext; |
| 149 | + if (bodyContext == null) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + var visitor = _HasAwaitVisitor(); |
| 154 | + body.accept(visitor); |
| 155 | + |
| 156 | + if (visitor.hasAwait) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + // If no imposed return type, then any type is OK. |
| 161 | + if (returnType == null) { |
| 162 | + rule.reportLintForToken(asyncKeyword); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + // We don't have to return anything. |
| 167 | + // So, the generated `Future` is not necessary. |
| 168 | + if (returnType is VoidType) { |
| 169 | + rule.reportLintForToken(asyncKeyword); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + // It is OK to return values into `FutureOr`. |
| 174 | + // So, wrapping values into `Future` is not necessary. |
| 175 | + if (returnType.isDartAsyncFutureOr) { |
| 176 | + rule.reportLintForToken(asyncKeyword); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + // We handle only `Future<T>` below. |
| 181 | + if (!returnType.isDartAsyncFuture) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + // If the body may complete normally, we cannot remove `async`. |
| 186 | + // This would make the body return `null`. |
| 187 | + // And `null` is not the same as `Future.value(null)`. |
| 188 | + if (bodyContext.mayCompleteNormally) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + // If every `return` returns `Future`, we don't need wrapping. |
| 193 | + if (visitor.everyReturnHasValue && visitor.returnsOnlyFuture) { |
| 194 | + rule.reportLintForToken(asyncKeyword); |
| 195 | + return; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +extension on InterfaceType { |
| 201 | + bool get isDartAsyncFutureOrSubtype { |
| 202 | + var typeProvider = element3.library2.typeProvider; |
| 203 | + return asInstanceOf2(typeProvider.futureElement2) != null; |
| 204 | + } |
| 205 | +} |
0 commit comments