Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

void_checks: Specify individual types which a FutureOr<void> target can accept #2274

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions lib/src/rules/void_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class VoidChecks extends LintRule implements NodeLintRule {
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this, context);
registry.addCompilationUnit(this, visitor);
registry.addMethodInvocation(this, visitor);
registry.addInstanceCreationExpression(this, visitor);
registry.addAssignmentExpression(this, visitor);
Expand All @@ -56,34 +55,38 @@ class _Visitor extends SimpleAstVisitor<void> {
final LinterContext context;
final TypeSystem typeSystem;

InterfaceType _futureDynamicType;

_Visitor(this.rule, this.context) : typeSystem = context.typeSystem;

bool isTypeAcceptableWhenExpectingVoid(DartType type) {
if (type.isVoid) return true;
if (type.isDartCoreNull) return true;
if (type.isDartAsyncFuture &&
type is InterfaceType &&
(type.typeArguments.first.isVoid ||
type.typeArguments.first.isDartCoreNull)) {
isTypeAcceptableWhenExpectingVoid(type.typeArguments.first)) {
return true;
}
return false;
}

bool isTypeAcceptableWhenExpectingFutureOrVoid(DartType type) {
if (type.isDynamic) return true;
if (isTypeAcceptableWhenExpectingVoid(type)) return true;
if (type.isDartAsyncFutureOr &&
type is InterfaceType &&
isTypeAcceptableWhenExpectingFutureOrVoid(type.typeArguments.first)) {
return true;
}

return false;
}

@override
void visitAssignmentExpression(AssignmentExpression node) {
final type = node.writeType;
_check(type, node.rightHandSide?.staticType, node,
checkedNode: node.rightHandSide);
}

@override
void visitCompilationUnit(CompilationUnit node) {
_futureDynamicType = context.typeProvider.futureDynamicType;
}

@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
final args = node.argumentList.arguments;
Expand Down Expand Up @@ -131,11 +134,12 @@ class _Visitor extends SimpleAstVisitor<void> {
checkedNode ??= node;
if (expectedType == null || type == null) {
return;
} else if (expectedType.isVoid &&
!isTypeAcceptableWhenExpectingVoid(type) ||
expectedType.isDartAsyncFutureOr &&
(expectedType as InterfaceType).typeArguments.first.isVoid &&
!typeSystem.isAssignableTo(type, _futureDynamicType)) {
}
if (expectedType.isVoid && !isTypeAcceptableWhenExpectingVoid(type)) {
rule.reportLint(node);
} else if (expectedType.isDartAsyncFutureOr &&
(expectedType as InterfaceType).typeArguments.first.isVoid &&
!isTypeAcceptableWhenExpectingFutureOrVoid(type)) {
rule.reportLint(node);
} else if (checkedNode is FunctionExpression &&
checkedNode.body is! ExpressionFunctionBody &&
Expand Down
11 changes: 11 additions & 0 deletions test/rules/experiments/nnbd/rules/void_checks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `pub run test -N void_checks`

import 'dart:async';

void emptyFunctionExpressionReturningFutureOrVoid(FutureOr<void> Function() f) {
f = () {}; // OK
}
4 changes: 4 additions & 0 deletions test/rules/void_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ missing_parameter_for_argument() {
void foo() {}
foo(0);
}

void emptyFunctionExpressionReturningFutureOrVoid(FutureOr<void> Function() f) {
f = () {}; // OK
}