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

don't overreport prefer_void_to_null on cast patterns and as expressions #4208

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/src/rules/prefer_void_to_null.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ class _Visitor extends SimpleAstVisitor<void> {
var parent = node.parent;

// Null Function()
if (parent is GenericFunctionType) {
return;
}
if (parent is GenericFunctionType) return;

// case var _ as Null
if (parent is CastPattern) return;

// a as Null
if (parent is AsExpression) return;

// Function(Null)
if (parent is SimpleFormalParameter &&
Expand Down
22 changes: 12 additions & 10 deletions test/rules/prefer_void_to_null_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ class PreferVoidToNullTestLanguage300 extends LintRuleTest
@override
String get lintRule => 'prefer_void_to_null';

/// https://github.com/dart-lang/linter/issues/4201
test_castAsExpression() async {
await assertNoDiagnostics(r'''
void f(int a) {
a as Null;
}
''');
}

/// https://github.com/dart-lang/linter/issues/4201
test_castPattern() async {
await assertDiagnostics(r'''
await assertNoDiagnostics(r'''
void f(int a) {
switch (a) {
case var _ as void:
case var _ as Null:
}
a as void;
}
''', [
// No lint.
error(WarningCode.UNNECESSARY_CAST_PATTERN, 46, 2),
error(ParserErrorCode.EXPECTED_TYPE_NAME, 49, 4),
error(HintCode.UNNECESSARY_CAST, 61, 9),
error(ParserErrorCode.EXPECTED_TYPE_NAME, 66, 4),
]);
''');
}
}