Skip to content

Commit 18c0a39

Browse files
pqCommit Queue
authored and
Commit Queue
committed
don't lint prefer_void_to_null on augmented fields/top-level vars
Fixes: https://github.com/dart-lang/linter/issues/4890 Change-Id: I9a44793e2e79ae946b5f010b9e588a6b8f29fc80 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362065 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent cb785c7 commit 18c0a39

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

pkg/linter/lib/src/extensions.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ extension AstNodeExtension on AstNode {
3030
return switch (self) {
3131
ClassDeclaration() => self.augmentKeyword != null,
3232
ConstructorDeclaration() => self.augmentKeyword != null,
33+
FieldDeclaration() => self.augmentKeyword != null,
3334
FunctionDeclarationImpl() => self.augmentKeyword != null,
3435
FunctionExpression() => self.parent?.isAugmentation ?? false,
3536
MethodDeclaration() => self.augmentKeyword != null,
3637
MixinDeclaration() => self.augmentKeyword != null,
38+
TopLevelVariableDeclaration() => self.augmentKeyword != null,
3739
VariableDeclaration(declaredElement: var element) =>
3840
element is PropertyInducingElement && element.isAugmentation,
3941
_ => false

pkg/linter/lib/src/rules/prefer_void_to_null.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ class _Visitor extends SimpleAstVisitor<void> {
154154
}
155155

156156
if (parent != null) {
157-
AstNode? member = parent.thisOrAncestorOfType<ClassMember>();
158-
member ??= parent.thisOrAncestorOfType<NamedCompilationUnitMember>();
159-
if (member?.isAugmentation ?? false) return;
157+
AstNode? declaration = parent.thisOrAncestorOfType<ClassMember>();
158+
declaration ??= parent.thisOrAncestorOfType<NamedCompilationUnitMember>();
159+
declaration ??=
160+
parent.thisOrAncestorOfType<TopLevelVariableDeclaration>();
161+
if (declaration?.isAugmentation ?? false) return;
160162
}
161163

162164
rule.reportLintForToken(node.name2);

pkg/linter/test/rules/prefer_void_to_null_test.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class PreferVoidToNullTest extends LintRuleTest {
1717
@override
1818
String get lintRule => 'prefer_void_to_null';
1919

20-
@FailingTest(
21-
issue: 'https://github.com/dart-lang/linter/issues/4890',
22-
reason: 'Null check operator used on a null value')
2320
test_augmentedField() async {
2421
newFile('$testPackageLibPath/a.dart', r'''
2522
import augment 'test.dart';
@@ -102,10 +99,6 @@ augment Future<Null>? get v => null;
10299
''');
103100
}
104101

105-
@FailingTest(
106-
issue: 'https://github.com/dart-lang/linter/issues/4890',
107-
reason:
108-
"CompileTimeErrorCode.DUPLICATE_DEFINITION [49, 1, The name 'v' is already defined.]")
109102
test_augmentedTopLevelVariable() async {
110103
newFile('$testPackageLibPath/a.dart', r'''
111104
import augment 'test.dart';

0 commit comments

Comments
 (0)