Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit dcf3a07

Browse files
authored
unnecessary_null_checks to ignore Future.value and Completer.complete. (#4079)
Fixes #4077
1 parent b96f861 commit dcf3a07

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/src/rules/unnecessary_null_checks.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@ DartType? getExpectedType(PostfixExpression node) {
129129
parent = parent.parent;
130130
}
131131
if (parent is ArgumentList && realNode is Expression) {
132+
var grandParent = parent.parent;
133+
if (grandParent is InstanceCreationExpression) {
134+
var constructor = grandParent.constructorName.staticElement;
135+
if (constructor != null) {
136+
if (constructor.returnType.isDartAsyncFuture &&
137+
constructor.name == 'value') {
138+
return null;
139+
}
140+
}
141+
} else if (grandParent is MethodInvocation) {
142+
var targetType = grandParent.realTarget?.staticType;
143+
if (targetType is InterfaceType) {
144+
var targetClass = targetType.element;
145+
146+
if (targetClass.library.isDartAsync &&
147+
targetClass.name == 'Completer' &&
148+
grandParent.methodName.name == 'complete') {
149+
return null;
150+
}
151+
}
152+
}
132153
return realNode.staticParameterElement?.type;
133154
}
134155
return null;

test/rules/unnecessary_null_checks_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ class UnnecessaryNullChecksTest extends LintRuleTest {
1717
@override
1818
String get lintRule => 'unnecessary_null_checks';
1919

20+
test_completerComplete() async {
21+
await assertNoDiagnostics(r'''
22+
import 'dart:async';
23+
void f(int? i) => Completer<int>().complete(i!);
24+
''');
25+
}
26+
27+
test_futureValue() async {
28+
await assertNoDiagnostics(r'''
29+
void f(int? i) => Future<int>.value(i!);
30+
''');
31+
}
32+
2033
test_undefinedFunction() async {
2134
await assertDiagnostics(r'''
2235
f6(int? p) {

0 commit comments

Comments
 (0)