Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Unified Diff: pkg/analyzer/lib/src/task/strong/checker.dart

Issue 2302573003: Check for non-null assignment when checking casts (Closed) Base URL: https://github.com/dart-lang/sdk@master
Patch Set: Add tests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/task/strong/non_null_checker_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/task/strong/checker.dart
diff --git a/pkg/analyzer/lib/src/task/strong/checker.dart b/pkg/analyzer/lib/src/task/strong/checker.dart
index d0b55fed93a80bdca1f0ba935f98e22dae82274d..a2d003eeb795cec098d82777dba8c9e59a8befb6 100644
--- a/pkg/analyzer/lib/src/task/strong/checker.dart
+++ b/pkg/analyzer/lib/src/task/strong/checker.dart
@@ -196,9 +196,7 @@ class CodeChecker extends RecursiveAstVisitor {
if (expr is ParenthesizedExpression) {
checkAssignment(expr.expression, type);
} else {
- if (_checkNonNullAssignment(expr, type)) {
- _checkDowncast(expr, type);
- }
+ _checkDowncast(expr, type);
}
}
@@ -743,6 +741,8 @@ class CodeChecker extends RecursiveAstVisitor {
from = _getDefiniteType(expr);
}
+ if (!_checkNonNullAssignment(expr, to, from)) return;
+
// We can use anything as void.
if (to.isVoid) return;
@@ -877,11 +877,10 @@ class CodeChecker extends RecursiveAstVisitor {
/// Checks if the assignment is valid with respect to non-nullable types.
/// Returns `false` if a nullable expression is assigned to a variable of
/// non-nullable type and `true` otherwise.
- bool _checkNonNullAssignment(Expression expression, DartType type) {
- var exprType = expression.staticType;
- if (rules.isNonNullableType(type) && rules.isNullableType(exprType)) {
+ bool _checkNonNullAssignment(Expression expression, DartType to, DartType from) {
+ if (rules.isNonNullableType(to) && rules.isNullableType(from)) {
_recordMessage(expression, StaticTypeWarningCode.INVALID_ASSIGNMENT,
- [exprType, type]);
+ [from, to]);
return false;
}
return true;
« no previous file with comments | « no previous file | pkg/analyzer/test/src/task/strong/non_null_checker_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698