Skip to content

Commit 592914f

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Don't report HintCode.CAN_BE_NULL_AFTER_NULL_AWARE when NNBD.
With NNBD we do shorting, so `foo?.bar.baz` is OK, `baz` will never be requested from `null`, unless `foo.bar` is nullable, in which case it is UNCHECKED_USE_OF_NULLABLE_VALUE. [email protected] Change-Id: Ic9bbd0f6116dfca53b8dd9cf05b557919c025d2d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115762 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c219a76 commit 592914f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
9393
/// The [LinterContext] used for possible const calculations.
9494
LinterContext _linterContext;
9595

96+
/// Is `true` if NNBD is enabled for the library being analyzed.
97+
final bool _isNonNullable;
98+
9699
/// Create a new instance of the [BestPracticesVerifier].
97100
///
98101
/// @param errorReporter the error reporter
@@ -110,6 +113,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
110113
}) : _nullType = typeProvider.nullType,
111114
_futureNullType = typeProvider.futureNullType,
112115
_typeSystem = typeSystem ?? new Dart2TypeSystem(typeProvider),
116+
_isNonNullable = unit.featureSet.isEnabled(Feature.non_nullable),
113117
_inheritanceManager = inheritanceManager,
114118
_invalidAccessVerifier =
115119
new _InvalidAccessVerifier(_errorReporter, _currentLibrary) {
@@ -918,6 +922,10 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
918922

919923
/// Produce several null-aware related hints.
920924
void _checkForNullAwareHints(Expression node, Token operator) {
925+
if (_isNonNullable) {
926+
return;
927+
}
928+
921929
if (operator == null || operator.type != TokenType.QUESTION_PERIOD) {
922930
return;
923931
}

pkg/analyzer/test/src/diagnostics/unchecked_use_of_nullable_value_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,6 @@ m(B? b) {
805805
b.a.x; // 2
806806
}
807807
''', [
808-
// TODO(scheglov) Remove HintCode.CAN_BE_NULL_AFTER_NULL_AWARE
809-
error(HintCode.CAN_BE_NULL_AFTER_NULL_AWARE, 86, 4),
810808
error(StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, 101, 1),
811809
]);
812810
var propertyAccess1 = findNode.propertyAccess('x; // 1');
@@ -879,8 +877,6 @@ m(C c) {
879877
c.b.a.x; // 2
880878
}
881879
''', [
882-
// TODO(scheglov) Remove HintCode.CAN_BE_NULL_AFTER_NULL_AWARE
883-
error(HintCode.CAN_BE_NULL_AFTER_NULL_AWARE, 131, 6),
884880
error(StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, 148, 3),
885881
]);
886882
var propertyAccess1 = findNode.propertyAccess('x; // 1');

0 commit comments

Comments
 (0)