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

Unified Diff: pkg/analyzer/test/src/task/strong/non_null_checker_test.dart

Issue 2219653005: Disallow uninitialized non-nullable variables (Closed) Base URL: https://github.com/dart-lang/sdk@master
Patch Set: Simplify 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/strong/checker.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
diff --git a/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart b/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
index 9b616afb8c33d1cd7378ba41f453f2f63ae1cbc1..e5fa7c662a856384501a5d0512af768d75f9e3b5 100644
--- a/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
+++ b/pkg/analyzer/test/src/task/strong/non_null_checker_test.dart
@@ -48,12 +48,6 @@ main() {
checkFile('int x = null;');
}
- void test_uninitialized_nonnullable() {
- // If `int`s are non-nullable, then this code should throw an error.
- addFile('int x;');
- check(nonnullableTypes: <String>['dart:core,int']);
- }
-
void test_initialize_nonnullable_with_null() {
addFile('int x = /*error:INVALID_ASSIGNMENT*/null;');
check(nonnullableTypes: <String>['dart:core,int']);
@@ -76,6 +70,50 @@ main() {
check(nonnullableTypes: <String>['dart:core,int']);
}
+ void test_uninitialized_nonnullable_local_variable() {
+ // Ideally, we will do flow analysis and throw an error only if a variable
+ // is used before it has been initialized.
+ addFile('main() { int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x; }');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_uninitialized_nonnullable_top_level_variable_declaration() {
+ // If `int`s are non-nullable, then this code should throw an error.
+ addFile('int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x;');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_uninitialized_nonnullable_field_declaration() {
+ addFile('''
+void foo() {}
+
+class A {
+ // Ideally, we should allow x to be init in the constructor, but that requires
+ // too much complication in the checker, so for now we throw a static error at
+ // the declaration site.
+ int /*error:NON_NULLABLE_FIELD_NOT_INITIALIZED*/x;
+
+ A();
+}
+''');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
+ void test_prefer_final_to_non_nullable_error() {
+ addFile('main() { final int /*error:FINAL_NOT_INITIALIZED*/x; }');
+ addFile('final int /*error:FINAL_NOT_INITIALIZED*/x;');
+ addFile('''
+void foo() {}
+
+class A {
+ final int x;
+
+ /*warning:FINAL_NOT_INITIALIZED_CONSTRUCTOR_1*/A();
+}
+''');
+ check(nonnullableTypes: <String>['dart:core,int']);
+ }
+
// Default example from NNBD document.
final String defaultNnbdExample = '''
class Point {
« no previous file with comments | « pkg/analyzer/lib/src/task/strong/checker.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698