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

Commit 40dd80c

Browse files
authored
don't overreport prefer_void_to_null on cast patterns and as expressions (#4208)
1 parent b416cd4 commit 40dd80c

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

lib/src/rules/prefer_void_to_null.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ class _Visitor extends SimpleAstVisitor<void> {
125125
var parent = node.parent;
126126

127127
// Null Function()
128-
if (parent is GenericFunctionType) {
129-
return;
130-
}
128+
if (parent is GenericFunctionType) return;
129+
130+
// case var _ as Null
131+
if (parent is CastPattern) return;
132+
133+
// a as Null
134+
if (parent is AsExpression) return;
131135

132136
// Function(Null)
133137
if (parent is SimpleFormalParameter &&

test/rules/prefer_void_to_null_test.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ class PreferVoidToNullTestLanguage300 extends LintRuleTest
1818
@override
1919
String get lintRule => 'prefer_void_to_null';
2020

21+
/// https://github.com/dart-lang/linter/issues/4201
22+
test_castAsExpression() async {
23+
await assertNoDiagnostics(r'''
24+
void f(int a) {
25+
a as Null;
26+
}
27+
''');
28+
}
29+
2130
/// https://github.com/dart-lang/linter/issues/4201
2231
test_castPattern() async {
23-
await assertDiagnostics(r'''
32+
await assertNoDiagnostics(r'''
2433
void f(int a) {
2534
switch (a) {
26-
case var _ as void:
35+
case var _ as Null:
2736
}
28-
a as void;
2937
}
30-
''', [
31-
// No lint.
32-
error(WarningCode.UNNECESSARY_CAST_PATTERN, 46, 2),
33-
error(ParserErrorCode.EXPECTED_TYPE_NAME, 49, 4),
34-
error(HintCode.UNNECESSARY_CAST, 61, 9),
35-
error(ParserErrorCode.EXPECTED_TYPE_NAME, 66, 4),
36-
]);
38+
''');
3739
}
3840
}

0 commit comments

Comments
 (0)