Skip to content

Commit a06ba02

Browse files
authored
prefer_void_to_null: not for variable declarations (dart-archive/linter#4210)
1 parent 5a1681b commit a06ba02

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

lib/src/rules/prefer_void_to_null.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ class _Visitor extends SimpleAstVisitor<void> {
140140
return;
141141
}
142142

143+
if (parent is VariableDeclarationList &&
144+
node == parent.type &&
145+
parent.parent is! FieldDeclaration) {
146+
// We should not recommend to use `void` for local or top-level variables.
147+
return;
148+
}
149+
143150
// <Null>[] or <Null, Null>{}
144151
if (parent is TypeArgumentList) {
145152
var literal = parent.parent;

test/rules/prefer_void_to_null_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ void f(int a) {
3535
case var _ as Null:
3636
}
3737
}
38+
''');
39+
}
40+
41+
test_localVariable() async {
42+
await assertNoDiagnostics(r'''
43+
void f() {
44+
Null _;
45+
}
46+
''');
47+
}
48+
49+
test_topLevelVariable() async {
50+
await assertNoDiagnostics(r'''
51+
Null a;
3852
''');
3953
}
4054
}

test_data/rules/prefer_void_to_null.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class F implements E {
5252
}
5353

5454
void void_; // OK
55-
Null null_; // LINT
56-
core.Null core_null; // LINT
5755
Future<void>? future_void; // OK
5856
Future<Null>? future_null; // LINT
5957
Future<core.Null>? future_core_null; // LINT
@@ -72,8 +70,6 @@ void Function(Future<Null>)? voidFunctionFutureNull; // LINT
7270

7371
usage() {
7472
void void_; // OK
75-
Null null_; // LINT
76-
core.Null core_null; // LINT
7773
Future<void>? future_void; // OK
7874
Future<Null> future_null; // LINT
7975
Future<core.Null> future_core_null; // LINT
@@ -152,8 +148,6 @@ class AsMembers {
152148

153149
void usage() {
154150
void void_; // OK
155-
Null null_; // LINT
156-
core.Null core_null; // LINT
157151
Future<void>? future_void; // OK
158152
Future<Null> future_null; // LINT
159153
Future<core.Null> future_core_null; // LINT

0 commit comments

Comments
 (0)