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

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

Issue 2200263004: Disallow assigning null to non-nullable variables (Closed) Base URL: https://github.com/dart-lang/sdk@master
Patch Set: Address lgtm comment Created 4 years, 4 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 | « pkg/analyzer/lib/src/task/dart.dart ('k') | 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 21281719674209128269d53a0165ed8d8cab6fd2..c074b7938c7aec46eabdae9302f15523b0dd0659 100644
--- a/pkg/analyzer/lib/src/task/strong/checker.dart
+++ b/pkg/analyzer/lib/src/task/strong/checker.dart
@@ -163,6 +163,7 @@ class CodeChecker extends RecursiveAstVisitor {
checkAssignment(expr.expression, type);
} else {
_checkDowncast(expr, type);
+ _checkNonNullAssignment(expr, type);
}
}
@@ -613,11 +614,10 @@ class CodeChecker extends RecursiveAstVisitor {
// typing rules may have inferred a more precise type for the variable
// based on the initializer.
} else {
- var dartType = getType(type);
for (VariableDeclaration variable in node.variables) {
var initializer = variable.initializer;
if (initializer != null) {
- checkAssignment(initializer, dartType);
+ checkAssignment(initializer, type.type);
}
}
}
@@ -839,6 +839,14 @@ class CodeChecker extends RecursiveAstVisitor {
[declElement.name]);
}
+ void _checkNonNullAssignment(Expression expression, DartType type) {
+ var exprType = expression.staticType;
+ if (rules.isNonNullableType(type) && !rules.isNonNullableType(exprType)) {
+ _recordMessage(expression, StaticTypeWarningCode.INVALID_ASSIGNMENT,
+ [exprType, type]);
+ }
+ }
+
void _checkReturnOrYield(Expression expression, AstNode node,
{bool yieldStar: false}) {
FunctionBody body = node.getAncestor((n) => n is FunctionBody);
« no previous file with comments | « pkg/analyzer/lib/src/task/dart.dart ('k') | 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