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

prefer_void_to_null: not for variable declarations #4210

Merged
merged 2 commits into from
Mar 29, 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
7 changes: 7 additions & 0 deletions lib/src/rules/prefer_void_to_null.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}

if (parent is VariableDeclarationList &&
node == parent.type &&
parent.parent is! FieldDeclaration) {
// We should not recommend to use `void` for local or top-level variables.
return;
}

// <Null>[] or <Null, Null>{}
if (parent is TypeArgumentList) {
var literal = parent.parent;
Expand Down
14 changes: 14 additions & 0 deletions test/rules/prefer_void_to_null_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ void f(int a) {
case var _ as Null:
}
}
''');
}

test_localVariable() async {
await assertNoDiagnostics(r'''
void f() {
Null _;
}
''');
}

test_topLevelVariable() async {
await assertNoDiagnostics(r'''
Null a;
''');
}
}
6 changes: 0 additions & 6 deletions test_data/rules/prefer_void_to_null.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class F implements E {
}

void void_; // OK
Null null_; // LINT
core.Null core_null; // LINT
Future<void>? future_void; // OK
Future<Null>? future_null; // LINT
Future<core.Null>? future_core_null; // LINT
Expand All @@ -72,8 +70,6 @@ void Function(Future<Null>)? voidFunctionFutureNull; // LINT

usage() {
void void_; // OK
Null null_; // LINT
core.Null core_null; // LINT
Future<void>? future_void; // OK
Future<Null> future_null; // LINT
Future<core.Null> future_core_null; // LINT
Expand Down Expand Up @@ -152,8 +148,6 @@ class AsMembers {

void usage() {
void void_; // OK
Null null_; // LINT
core.Null core_null; // LINT
Future<void>? future_void; // OK
Future<Null> future_null; // LINT
Future<core.Null> future_core_null; // LINT
Expand Down