Skip to content

Commit ca00a39

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Support for 'null =' and 'null !=' for nullability.
R=brianwilkerson@google.com, paulberry@google.com Change-Id: I84b186db9888ebc14abbc12acbba7fe4d5809072 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107191 Reviewed-by: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent fa5b04d commit ca00a39

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,22 @@ class _FlowAnalysisVisitor extends GeneralizingAstVisitor<void> {
270270
} else if (operator == TokenType.BANG_EQ) {
271271
left.accept(this);
272272
right.accept(this);
273-
// TODO(scheglov) Support `null != name`
274273
if (right is NullLiteral) {
275274
if (left is SimpleIdentifier) {
276275
var element = left.staticElement;
277276
if (element is VariableElement) {
278277
flow.conditionNotEqNull(node, element);
279278
}
280279
}
280+
} else if (left is NullLiteral) {
281+
if (right is SimpleIdentifier) {
282+
var element = right.staticElement;
283+
if (element is VariableElement) {
284+
flow.conditionNotEqNull(node, element);
285+
}
286+
}
281287
}
282288
} else if (operator == TokenType.EQ_EQ) {
283-
// TODO(scheglov) Support `null == name`
284289
left.accept(this);
285290
right.accept(this);
286291
if (right is NullLiteral) {
@@ -290,6 +295,13 @@ class _FlowAnalysisVisitor extends GeneralizingAstVisitor<void> {
290295
flow.conditionEqNull(node, element);
291296
}
292297
}
298+
} else if (left is NullLiteral) {
299+
if (right is SimpleIdentifier) {
300+
var element = right.staticElement;
301+
if (element is VariableElement) {
302+
flow.conditionEqNull(node, element);
303+
}
304+
}
293305
}
294306
} else if (operator == TokenType.QUESTION_QUESTION) {
295307
left.accept(this);

pkg/analyzer/test/src/dart/resolution/flow_analysis_test.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,18 @@ void f(int a, int b) {
14311431
assertNonNullable('b; // 2', 'a; // 3', 'b; // 4', 'b; // 6');
14321432
}
14331433

1434-
test_if_notNull_thenExit() async {
1434+
test_if_notNull_thenExit_left() async {
1435+
await trackCode(r'''
1436+
void f(int x) {
1437+
if (null != x) return;
1438+
x; // 1
1439+
}
1440+
''');
1441+
assertNullable('x; // 1');
1442+
assertNonNullable();
1443+
}
1444+
1445+
test_if_notNull_thenExit_right() async {
14351446
await trackCode(r'''
14361447
void f(int x) {
14371448
if (x != null) return;
@@ -1442,7 +1453,18 @@ void f(int x) {
14421453
assertNonNullable();
14431454
}
14441455

1445-
test_if_null_thenExit() async {
1456+
test_if_null_thenExit_left() async {
1457+
await trackCode(r'''
1458+
void f(int x) {
1459+
if (null == x) return;
1460+
x; // 1
1461+
}
1462+
''');
1463+
assertNullable();
1464+
assertNonNullable('x; // 1');
1465+
}
1466+
1467+
test_if_null_thenExit_right() async {
14461468
await trackCode(r'''
14471469
void f(int x) {
14481470
if (x == null) return;

0 commit comments

Comments
 (0)