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

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

Issue 2219653005: Disallow uninitialized non-nullable variables (Closed) Base URL: https://github.com/dart-lang/sdk@master
Patch Set: Fix build break 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
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 8f2f12cf4926712a2392ae339ff6f5ce2cbaa234..5f16872c618f6c7446aacf0ff3b2c6d0fc2ef344 100644
--- a/pkg/analyzer/lib/src/task/strong/checker.dart
+++ b/pkg/analyzer/lib/src/task/strong/checker.dart
@@ -341,6 +341,12 @@ class CodeChecker extends RecursiveAstVisitor {
}
@override
+ void visitFieldDeclaration(FieldDeclaration node) {
+ _checkForNonNullNotInitialized(node.fields);
+ super.visitFieldDeclaration(node);
+ }
+
+ @override
void visitFieldFormalParameter(FieldFormalParameter node) {
var element = node.element;
var typeName = node.type;
@@ -602,6 +608,12 @@ class CodeChecker extends RecursiveAstVisitor {
}
@override
+ Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
+ _checkForNonNullNotInitialized(node.variables);
+ return super.visitTopLevelVariableDeclaration(node);
+ }
+
+ @override
void visitSwitchStatement(SwitchStatement node) {
// SwitchStatement defines a boolean conversion to check the result of the
// case value == the switch value, but in dev_compiler we require a boolean
@@ -629,6 +641,12 @@ class CodeChecker extends RecursiveAstVisitor {
}
@override
+ Object visitVariableDeclarationStatement(VariableDeclarationStatement node) {
+ _checkForNonNullNotInitialized(node.variables);
+ return super.visitVariableDeclarationStatement(node);
+ }
+
+ @override
void visitWhileStatement(WhileStatement node) {
checkBoolean(node.condition);
node.visitChildren(this);
@@ -745,6 +763,24 @@ class CodeChecker extends RecursiveAstVisitor {
}
/**
+ *
Jennifer Messerly 2016/08/05 17:39:31 missing doc comment here?
+ */
+ void _checkForNonNullNotInitialized(VariableDeclarationList list) {
Jennifer Messerly 2016/08/05 17:39:31 you might be able to put this logic in visitVariab
stanm 2016/08/05 18:06:52 Awesome! Thanks!
+ NodeList<VariableDeclaration> variables = list.variables;
+ for (VariableDeclaration variable in variables) {
+ if (!list.isConst &&
+ !list.isFinal &&
+ variable.initializer == null &&
+ rules.isNonNullableType(list?.type?.type)) {
+ _recordMessage(
+ variable,
+ StaticTypeWarningCode.NON_NULLABLE_FIELD_NOT_INITIALIZED,
+ [variable.name, list?.type?.type]);
+ }
+ }
+ }
+
+ /**
* Check if the closure [node] is unsafe due to dartbug.com/26947. If so,
* issue a warning.
*
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.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