Skip to content

Commit

Permalink
Don't report HintCode.CAN_BE_NULL_AFTER_NULL_AWARE when NNBD.
Browse files Browse the repository at this point in the history
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.

R=brianwilkerson@google.com

Change-Id: Ic9bbd0f6116dfca53b8dd9cf05b557919c025d2d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115762
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Sep 5, 2019
1 parent c219a76 commit 592914f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 8 additions & 0 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
/// The [LinterContext] used for possible const calculations.
LinterContext _linterContext;

/// Is `true` if NNBD is enabled for the library being analyzed.
final bool _isNonNullable;

/// Create a new instance of the [BestPracticesVerifier].
///
/// @param errorReporter the error reporter
Expand All @@ -110,6 +113,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
}) : _nullType = typeProvider.nullType,
_futureNullType = typeProvider.futureNullType,
_typeSystem = typeSystem ?? new Dart2TypeSystem(typeProvider),
_isNonNullable = unit.featureSet.isEnabled(Feature.non_nullable),
_inheritanceManager = inheritanceManager,
_invalidAccessVerifier =
new _InvalidAccessVerifier(_errorReporter, _currentLibrary) {
Expand Down Expand Up @@ -918,6 +922,10 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {

/// Produce several null-aware related hints.
void _checkForNullAwareHints(Expression node, Token operator) {
if (_isNonNullable) {
return;
}

if (operator == null || operator.type != TokenType.QUESTION_PERIOD) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,6 @@ m(B? b) {
b.a.x; // 2
}
''', [
// TODO(scheglov) Remove HintCode.CAN_BE_NULL_AFTER_NULL_AWARE
error(HintCode.CAN_BE_NULL_AFTER_NULL_AWARE, 86, 4),
error(StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, 101, 1),
]);
var propertyAccess1 = findNode.propertyAccess('x; // 1');
Expand Down Expand Up @@ -879,8 +877,6 @@ m(C c) {
c.b.a.x; // 2
}
''', [
// TODO(scheglov) Remove HintCode.CAN_BE_NULL_AFTER_NULL_AWARE
error(HintCode.CAN_BE_NULL_AFTER_NULL_AWARE, 131, 6),
error(StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, 148, 3),
]);
var propertyAccess1 = findNode.propertyAccess('x; // 1');
Expand Down

0 comments on commit 592914f

Please sign in to comment.