Skip to content

Commit 085c4b4

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 40482. Fix for Function?.call() reporting unchecked nullable.
Bug: #40482 Change-Id: Ifb65cd8016dca18c634cd216759278b7518d41ef Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135185 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 20e20a0 commit 085c4b4

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ class MethodInvocationResolver {
423423
InterfaceType receiverType, SimpleIdentifier nameNode, String name) {
424424
if (_isCoreFunction(receiverType) &&
425425
name == FunctionElement.CALL_METHOD_NAME) {
426-
_resolver.nullableDereferenceVerifier.expression(receiver);
426+
_resolver.nullableDereferenceVerifier.expression(
427+
receiver,
428+
type: receiverType,
429+
);
427430
_setDynamicResolution(node);
428431
return;
429432
}

pkg/analyzer/lib/src/error/nullable_dereference_verifier.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ class NullableDereferenceVerifier {
2121
}) : _typeSystem = typeSystem,
2222
_errorReporter = errorReporter;
2323

24-
bool expression(Expression expression) {
24+
bool expression(Expression expression, {DartType type}) {
2525
if (!_typeSystem.isNonNullableByDefault) {
2626
return false;
2727
}
2828

29-
return _check(expression, expression.staticType);
29+
type ??= expression.staticType;
30+
return _check(expression, type);
3031
}
3132

3233
void report(AstNode errorNode, DartType receiverType) {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,40 @@ main() {
17251725
);
17261726
}
17271727

1728+
test_hasReceiver_interfaceQ_Function_call_checked() async {
1729+
await assertNoErrorsInCode(r'''
1730+
void main(Function? foo) {
1731+
foo?.call();
1732+
}
1733+
''');
1734+
1735+
assertMethodInvocation2(
1736+
findNode.methodInvocation('foo?.call()'),
1737+
element: null,
1738+
typeArgumentTypes: [],
1739+
invokeType: 'dynamic',
1740+
type: 'dynamic',
1741+
);
1742+
}
1743+
1744+
test_hasReceiver_interfaceQ_Function_call_unchecked() async {
1745+
await assertErrorsInCode(r'''
1746+
void main(Function? foo) {
1747+
foo.call();
1748+
}
1749+
''', [
1750+
error(StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE, 29, 3),
1751+
]);
1752+
1753+
assertMethodInvocation2(
1754+
findNode.methodInvocation('foo.call()'),
1755+
element: null,
1756+
typeArgumentTypes: [],
1757+
invokeType: 'dynamic',
1758+
type: 'dynamic',
1759+
);
1760+
}
1761+
17281762
test_hasReceiver_interfaceTypeQ_defined() async {
17291763
await assertErrorsInCode(r'''
17301764
class A {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
ERROR|STATIC_WARNING|UNCHECKED_USE_OF_NULLABLE_VALUE|lib/_http/http.dart|7638|7|11|The expression is nullable and must be null-checked before it can be used.
2-
ERROR|STATIC_WARNING|UNCHECKED_USE_OF_NULLABLE_VALUE|lib/_http/http.dart|8726|5|24|The expression is nullable and must be null-checked before it can be used.
1+

0 commit comments

Comments
 (0)