-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add test that unnecessary null-aware elements are reported
- Loading branch information
Showing
4 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
LanguageFeatures/Control-flow-collections/Null-aware-elements/warnings_A01_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion As we do for other null-aware expressions like `?.` and `...?`, | ||
/// compilers and IDEs should probably warn if the inner expressions in | ||
/// null-aware elements are not potentially nullable since in that case, the `?` | ||
/// has no meaningful effect. | ||
/// | ||
/// @description Check that it is a warning if the inner expressions in | ||
/// null-aware elements are not potentially nullable. | ||
/// @author sgrekhov22@gmail.com | ||
void testSet() { | ||
int e1 = 42; | ||
int? e2 = 2 > 1 ? 2 : null; | ||
int? e3 = 2 > 1 ? 3 : null; | ||
int? e4 = 2 > 1 ? 4 : null; | ||
|
||
if (e4 != null) { | ||
<Object>{ | ||
?e1, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?e2!, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
if (e3 != null) | ||
?e3 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
else | ||
?e3, // Ok | ||
?e4 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} | ||
} | ||
|
||
void testList() { | ||
int e1 = 42; | ||
int? e2 = 2 > 1 ? 2 : null; | ||
int? e3 = 2 > 1 ? 3 : null; | ||
int? e4 = 2 > 1 ? 4 : null; | ||
|
||
if (e4 != null) { | ||
<Object>[ | ||
?e1, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?e2!, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
if (e3 != null) | ||
?e3 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
else | ||
?e3, // Ok | ||
?e4 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
]; | ||
} | ||
} | ||
|
||
void testMapKey() { | ||
int e1 = 42; | ||
int? e2 = 2 > 1 ? 2 : null; | ||
int? e3 = 2 > 1 ? 3 : null; | ||
int? e4 = 2 > 1 ? 4 : null; | ||
|
||
if (e4 != null) { | ||
<Object, int>{ | ||
?e1: 1, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?e2!: 2, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
if (e3 != null) | ||
?e3: 3 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
else | ||
?e3: 3, // Ok | ||
?e4: 4 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} | ||
} | ||
|
||
void testMapValue() { | ||
int e1 = 42; | ||
int? e2 = 2 > 1 ? 2 : null; | ||
int? e3 = 2 > 1 ? 3 : null; | ||
int? e4 = 2 > 1 ? 4 : null; | ||
|
||
if (e4 != null) { | ||
<int, Object>{ | ||
1: ?e1, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
2: ?e2!, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
if (e3 != null) | ||
3: ?e3 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
else | ||
3: ?e3, // Ok | ||
4: ?e4 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} | ||
} | ||
|
||
main() { | ||
testSet(); | ||
testList(); | ||
testMapKey(); | ||
testMapValue(); | ||
} |
122 changes: 122 additions & 0 deletions
122
LanguageFeatures/Control-flow-collections/Null-aware-elements/warnings_A01_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion As we do for other null-aware expressions like `?.` and `...?`, | ||
/// compilers and IDEs should probably warn if the inner expressions in | ||
/// null-aware elements are not potentially nullable since in that case, the `?` | ||
/// has no meaningful effect. | ||
/// | ||
/// @description Check that it is a warning if the inner expressions in | ||
/// null-aware elements are not potentially nullable. Test literals | ||
/// @author sgrekhov22@gmail.com | ||
main() { | ||
<Object>{ | ||
?42, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?3.14, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?true, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?"", | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?''' Triple quoted string ''', | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?r' Raw string ' | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
|
||
<Object>[ | ||
?42, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?3.14, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?true, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?"", | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?''' Triple quoted string ''', | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?r' Raw string ' | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
]; | ||
|
||
<Object, int>{ | ||
?42: 0, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?3.14: 0, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?true: 0, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?"": 0, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?''' Triple quoted string ''': 0, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?r' Raw string ': 0 | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
|
||
<int, Object>{ | ||
1: ?42, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
2: ?3.14, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
3: ?true, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
4: ?"", | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
5: ?''' Triple quoted string ''', | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
6: ?r' Raw string ' | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} |
124 changes: 124 additions & 0 deletions
124
LanguageFeatures/Control-flow-collections/Null-aware-elements/warnings_A01_t03.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion As we do for other null-aware expressions like `?.` and `...?`, | ||
/// compilers and IDEs should probably warn if the inner expressions in | ||
/// null-aware elements are not potentially nullable since in that case, the `?` | ||
/// has no meaningful effect. | ||
/// | ||
/// @description Check that it is a warning if the inner expressions in | ||
/// null-aware elements are not potentially nullable. Test collection literals | ||
/// @author sgrekhov22@gmail.com | ||
main() { | ||
int? e = 2 > 1 ? 1 : null; | ||
|
||
<Iterable>{ | ||
?[], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?[?e], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e: 1}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{2: ?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
|
||
<Iterable>[ | ||
?[], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?[?e], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e: 1}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{2: ?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
]; | ||
|
||
<Iterable, int>{ | ||
?[]: 1, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?[?e]: 2, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e}: 3, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{}: 4, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{?e: 1}: 5, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
?{2: ?e}: 6, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
|
||
<int, Iterable>{ | ||
1: ?[], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
2: ?[?e], | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
3: ?{?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
4: ?{}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
5: ?{?e: 1}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
6: ?{2: ?e}, | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
}; | ||
} |
Oops, something went wrong.