Skip to content

Commit 2eebc10

Browse files
MichaelRFairhurstcommit-bot@chromium.org
authored andcommitted
Refactor non-bool expression checks
Change-Id: I4da7abe3293c4dbf7218f3a276cf9418b3d14f19 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106000 Commit-Queue: Mike Fairhurst <mfairhurst@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 9bb678f commit 2eebc10

File tree

2 files changed

+28
-50
lines changed

2 files changed

+28
-50
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
3434
import 'package:analyzer/src/generated/resolver.dart';
3535
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
3636
import 'package:analyzer/src/generated/source.dart';
37+
import 'package:meta/meta.dart';
3738

3839
/**
3940
* A visitor used to traverse an AST structure looking for additional errors and
@@ -369,15 +370,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
369370

370371
@override
371372
void visitAssertInitializer(AssertInitializer node) {
372-
_checkForNonBoolExpression(node);
373-
_checkForNullableDereference(node.condition);
373+
_checkForNonBoolExpression(node.condition,
374+
errorCode: StaticTypeWarningCode.NON_BOOL_EXPRESSION);
374375
super.visitAssertInitializer(node);
375376
}
376377

377378
@override
378379
void visitAssertStatement(AssertStatement node) {
379-
_checkForNonBoolExpression(node);
380-
_checkForNullableDereference(node.condition);
380+
_checkForNonBoolExpression(node.condition,
381+
errorCode: StaticTypeWarningCode.NON_BOOL_EXPRESSION);
381382
super.visitAssertStatement(node);
382383
}
383384

@@ -418,8 +419,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
418419
_checkForAssignability(node.rightOperand, _boolType,
419420
StaticTypeWarningCode.NON_BOOL_OPERAND, [lexeme]);
420421
_checkForUseOfVoidResult(node.rightOperand);
421-
_checkForNullableDereference(node.leftOperand);
422-
_checkForNullableDereference(node.rightOperand);
423422
} else if (type == TokenType.EQ_EQ || type == TokenType.BANG_EQ) {
424423
_checkForArgumentTypeNotAssignableForArgument(node.rightOperand,
425424
promoteParameterToNullable: true);
@@ -1168,12 +1167,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
11681167
Expression operand = node.operand;
11691168
if (operatorType == TokenType.BANG) {
11701169
_checkForNonBoolNegationExpression(operand);
1171-
} else if (operatorType.isIncrementOperator) {
1172-
_checkForAssignmentToFinal(operand);
1170+
} else {
1171+
if (operatorType.isIncrementOperator) {
1172+
_checkForAssignmentToFinal(operand);
1173+
}
1174+
_checkForNullableDereference(operand);
1175+
_checkForUseOfVoidResult(operand);
1176+
_checkForIntNotAssignable(operand);
11731177
}
1174-
_checkForIntNotAssignable(operand);
1175-
_checkForNullableDereference(operand);
1176-
_checkForUseOfVoidResult(operand);
11771178
super.visitPrefixExpression(node);
11781179
}
11791180

@@ -4569,57 +4570,34 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
45694570
* See [StaticTypeWarningCode.NON_BOOL_CONDITION].
45704571
*/
45714572
void _checkForNonBoolCondition(Expression condition) {
4572-
DartType conditionType = getStaticType(condition);
4573-
if (!_checkForNullableDereference(condition) &&
4574-
!_checkForUseOfVoidResult(condition) &&
4575-
conditionType != null &&
4576-
!_typeSystem.isAssignableTo(conditionType, _boolType,
4577-
featureSet: _featureSet)) {
4578-
_errorReporter.reportErrorForNode(
4579-
StaticTypeWarningCode.NON_BOOL_CONDITION, condition);
4580-
}
4573+
_checkForNonBoolExpression(condition,
4574+
errorCode: StaticTypeWarningCode.NON_BOOL_CONDITION);
45814575
}
45824576

45834577
/**
4584-
* Verify that the given [assertion] has either a 'bool' or '() -> bool'
4585-
* condition.
4578+
* Verify that the given [expression] is of type 'bool', and report
4579+
* [errorCode] if not, or a nullability error if its improperly nullable.
45864580
*/
4587-
void _checkForNonBoolExpression(Assertion assertion) {
4588-
Expression expression = assertion.condition;
4581+
void _checkForNonBoolExpression(Expression expression,
4582+
{@required ErrorCode errorCode}) {
45894583
DartType type = getStaticType(expression);
4590-
if (type is InterfaceType) {
4591-
if (!_typeSystem.isAssignableTo(type, _boolType,
4592-
featureSet: _featureSet)) {
4593-
if (type.element == _boolType.element) {
4594-
_errorReporter.reportErrorForNode(
4595-
StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, expression);
4596-
} else {
4597-
_errorReporter.reportErrorForNode(
4598-
StaticTypeWarningCode.NON_BOOL_EXPRESSION, expression);
4599-
}
4584+
if (!_checkForUseOfVoidResult(expression) &&
4585+
!_typeSystem.isAssignableTo(type, _boolType, featureSet: _featureSet)) {
4586+
if (type.element == _boolType.element) {
4587+
_errorReporter.reportErrorForNode(
4588+
StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, expression);
4589+
} else {
4590+
_errorReporter.reportErrorForNode(errorCode, expression);
46004591
}
4601-
} else if (type is FunctionType) {
4602-
_errorReporter.reportErrorForNode(
4603-
StaticTypeWarningCode.NON_BOOL_EXPRESSION, expression);
46044592
}
46054593
}
46064594

46074595
/**
46084596
* Checks to ensure that the given [expression] is assignable to bool.
46094597
*/
46104598
void _checkForNonBoolNegationExpression(Expression expression) {
4611-
DartType conditionType = getStaticType(expression);
4612-
if (conditionType != null &&
4613-
!_typeSystem.isAssignableTo(conditionType, _boolType,
4614-
featureSet: _featureSet)) {
4615-
if (conditionType.element == _boolType.element) {
4616-
_errorReporter.reportErrorForNode(
4617-
StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, expression);
4618-
} else {
4619-
_errorReporter.reportErrorForNode(
4620-
StaticTypeWarningCode.NON_BOOL_NEGATION_EXPRESSION, expression);
4621-
}
4622-
}
4599+
_checkForNonBoolExpression(expression,
4600+
errorCode: StaticTypeWarningCode.NON_BOOL_NEGATION_EXPRESSION);
46234601
}
46244602

46254603
/**

pkg/dev_compiler/test/sourcemap/testfiles/next_through_assert.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ void main() {
77
assert(/*bc:1*/ foo());
88
}
99

10-
foo() {
10+
bool foo() {
1111
return true;
1212
}

0 commit comments

Comments
 (0)