Skip to content

Commit c094272

Browse files
authored
null_check_on_nullable_type_parameter pattern assignment support (#4224)
* `null_check_on_nullable_type_parameter` pattern assignment support * ++ * rework
1 parent 80ac891 commit c094272

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

lib/src/rules/null_check_on_nullable_type_parameter.dart

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class NullCheckOnNullableTypeParameter extends LintRule {
7070

7171
var visitor = _Visitor(this, context);
7272
registry.addPostfixExpression(this, visitor);
73-
registry.addRecordPattern(this, visitor);
73+
registry.addNullAssertPattern(this, visitor);
7474
}
7575
}
7676

@@ -83,6 +83,13 @@ class _Visitor extends SimpleAstVisitor<void> {
8383
bool isNullableTypeParameterType(DartType? type) =>
8484
type is TypeParameterType && context.typeSystem.isNullable(type);
8585

86+
@override
87+
void visitNullAssertPattern(NullAssertPattern node) {
88+
if (isNullableTypeParameterType(node.matchedValueType)) {
89+
rule.reportLintForToken(node.operator);
90+
}
91+
}
92+
8693
@override
8794
void visitPostfixExpression(PostfixExpression node) {
8895
if (node.operator.type != TokenType.BANG) return;
@@ -97,16 +104,4 @@ class _Visitor extends SimpleAstVisitor<void> {
97104
rule.reportLintForToken(node.operator);
98105
}
99106
}
100-
101-
@override
102-
void visitRecordPattern(RecordPattern node) {
103-
for (var field in node.fields) {
104-
var pattern = field.pattern;
105-
if (pattern is NullAssertPattern) {
106-
if (isNullableTypeParameterType(pattern.matchedValueType)) {
107-
rule.reportLintForToken(pattern.operator);
108-
}
109-
}
110-
}
111-
}
112107
}

test/rules/null_check_on_nullable_type_parameter_test.dart

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,73 @@ class NullCheckOnNullableTypeParameterTestLanguage300 extends LintRuleTest
1818
@override
1919
String get lintRule => 'null_check_on_nullable_type_parameter';
2020

21-
test_nullAssertPattern() async {
21+
test_nullAssertPattern_ifCase() async {
2222
await assertDiagnostics(r'''
23-
void f<T>((T?, T?) p){
23+
f<T>(T? x){
24+
if (x case var y!) print(y);
25+
}
26+
''', [
27+
lint(30, 1),
28+
]);
29+
}
30+
31+
test_nullAssertPattern_list() async {
32+
await assertDiagnostics(r'''
33+
f<T>(List<T?> l){
34+
var [x!, y] = l;
35+
}
36+
''', [
37+
lint(26, 1),
38+
]);
39+
}
40+
41+
@FailingTest(
42+
reason: 'Exception raised in resolver',
43+
issue: 'https://github.com/dart-lang/linter/issues/4258')
44+
test_nullAssertPattern_logicalOr() async {
45+
await assertDiagnostics(r'''
46+
f<T>(T? x){
47+
switch(x) {
48+
case var y! || y == 2 : print(y);
49+
}
50+
}
51+
''', [
52+
lint(39, 1),
53+
]);
54+
}
55+
56+
test_nullAssertPattern_map() async {
57+
await assertDiagnostics(r'''
58+
f<T>(Map<String, T?> m){
59+
var {'x': y!} = m;
60+
}
61+
''', [
62+
lint(38, 1),
63+
]);
64+
}
65+
66+
test_nullAssertPattern_object() async {
67+
await assertDiagnostics(r'''
68+
class A<E> {
69+
E? a;
70+
A(this.a);
71+
}
72+
73+
f<T>(T? t, A<T> u) {
74+
A(a: t!) = u;
75+
}
76+
''', [
77+
lint(66, 1),
78+
]);
79+
}
80+
81+
test_nullAssertPattern_record() async {
82+
await assertDiagnostics(r'''
83+
f<T>((T?, T?) p){
2484
var (x!, y) = p;
2585
}
2686
''', [
27-
lint(31, 1),
87+
lint(26, 1),
2888
]);
2989
}
3090
}

0 commit comments

Comments
 (0)