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

Commit a056a0a

Browse files
authored
null assert pattern support for null_check_on_nullable_type_parameter (#4148)
* null assert pattern support for `null_check_on_nullable_type_parameter` * ++ * fmt
1 parent 2bf8ae2 commit a056a0a

4 files changed

+54
-4
lines changed

lib/src/rules/null_check_on_nullable_type_parameter.dart

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

7171
var visitor = _Visitor(this, context);
7272
registry.addPostfixExpression(this, visitor);
73+
registry.addRecordPattern(this, visitor);
7374
}
7475
}
7576

@@ -79,19 +80,33 @@ class _Visitor extends SimpleAstVisitor<void> {
7980
final LinterContext context;
8081
_Visitor(this.rule, this.context);
8182

83+
bool isNullableTypeParameterType(DartType? type) =>
84+
type is TypeParameterType && context.typeSystem.isNullable(type);
85+
8286
@override
8387
void visitPostfixExpression(PostfixExpression node) {
8488
if (node.operator.type != TokenType.BANG) return;
8589

8690
var expectedType = getExpectedType(node);
8791
var type = node.operand.staticType;
88-
if (type is TypeParameterType &&
89-
context.typeSystem.isNullable(type) &&
92+
if (isNullableTypeParameterType(type) &&
9093
expectedType != null &&
9194
context.typeSystem.isPotentiallyNullable(expectedType) &&
92-
context.typeSystem.promoteToNonNull(type) ==
95+
context.typeSystem.promoteToNonNull(type!) ==
9396
context.typeSystem.promoteToNonNull(expectedType)) {
9497
rule.reportLintForToken(node.operator);
9598
}
9699
}
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+
}
97112
}

test/rule_test_support.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ mixin LanguageVersion300Mixin on PubPackageResolutionTest {
129129
}
130130

131131
abstract class LintRuleTest extends PubPackageResolutionTest {
132-
bool get dumpAstOnFailures => false;
132+
bool get dumpAstOnFailures => true;
133133

134134
String? get lintRule;
135135

test/rules/all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import 'no_duplicate_case_values_test.dart' as no_duplicate_case_values;
5858
import 'non_adjacent_strings_in_list_test.dart' as no_adjacent_strings_in_list;
5959
import 'non_constant_identifier_names_test.dart'
6060
as non_constant_identifier_names;
61+
import 'null_check_on_nullable_type_parameter_test.dart'
62+
as null_check_on_nullable_type_parameter;
6163
import 'null_closures_test.dart' as null_closures;
6264
import 'omit_local_variable_types_test.dart' as omit_local_variable_types;
6365
import 'one_member_abstracts_test.dart' as one_member_abstracts;
@@ -157,6 +159,7 @@ void main() {
157159
no_adjacent_strings_in_list.main();
158160
no_duplicate_case_values.main();
159161
non_constant_identifier_names.main();
162+
null_check_on_nullable_type_parameter.main();
160163
null_closures.main();
161164
omit_local_variable_types.main();
162165
one_member_abstracts.main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(NullCheckOnNullableTypeParameterTestLanguage300);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class NullCheckOnNullableTypeParameterTestLanguage300 extends LintRuleTest
17+
with LanguageVersion300Mixin {
18+
@override
19+
String get lintRule => 'null_check_on_nullable_type_parameter';
20+
21+
test_nullAssertPattern() async {
22+
await assertDiagnostics(r'''
23+
void f<T>((T?, T?) p){
24+
var (x!, y) = p;
25+
}
26+
''', [
27+
error(HintCode.UNUSED_LOCAL_VARIABLE, 30, 1),
28+
lint(31, 1),
29+
error(HintCode.UNUSED_LOCAL_VARIABLE, 34, 1),
30+
]);
31+
}
32+
}

0 commit comments

Comments
 (0)