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 <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/linter/lib/src/extensions.dart b/pkg/linter/lib/src/extensions.dart
index 72a6a51..d6b19a7 100644
--- a/pkg/linter/lib/src/extensions.dart
+++ b/pkg/linter/lib/src/extensions.dart
@@ -30,10 +30,12 @@
return switch (self) {
ClassDeclaration() => self.augmentKeyword != null,
ConstructorDeclaration() => self.augmentKeyword != null,
+ FieldDeclaration() => self.augmentKeyword != null,
FunctionDeclarationImpl() => self.augmentKeyword != null,
FunctionExpression() => self.parent?.isAugmentation ?? false,
MethodDeclaration() => self.augmentKeyword != null,
MixinDeclaration() => self.augmentKeyword != null,
+ TopLevelVariableDeclaration() => self.augmentKeyword != null,
VariableDeclaration(declaredElement: var element) =>
element is PropertyInducingElement && element.isAugmentation,
_ => false
diff --git a/pkg/linter/lib/src/rules/prefer_void_to_null.dart b/pkg/linter/lib/src/rules/prefer_void_to_null.dart
index fb6e78c..3790d9c 100644
--- a/pkg/linter/lib/src/rules/prefer_void_to_null.dart
+++ b/pkg/linter/lib/src/rules/prefer_void_to_null.dart
@@ -154,9 +154,11 @@
}
if (parent != null) {
- AstNode? member = parent.thisOrAncestorOfType<ClassMember>();
- member ??= parent.thisOrAncestorOfType<NamedCompilationUnitMember>();
- if (member?.isAugmentation ?? false) return;
+ AstNode? declaration = parent.thisOrAncestorOfType<ClassMember>();
+ declaration ??= parent.thisOrAncestorOfType<NamedCompilationUnitMember>();
+ declaration ??=
+ parent.thisOrAncestorOfType<TopLevelVariableDeclaration>();
+ if (declaration?.isAugmentation ?? false) return;
}
rule.reportLintForToken(node.name2);
diff --git a/pkg/linter/test/rules/prefer_void_to_null_test.dart b/pkg/linter/test/rules/prefer_void_to_null_test.dart
index d798d0f..00d8a85 100644
--- a/pkg/linter/test/rules/prefer_void_to_null_test.dart
+++ b/pkg/linter/test/rules/prefer_void_to_null_test.dart
@@ -17,9 +17,6 @@
@override
String get lintRule => 'prefer_void_to_null';
- @FailingTest(
- issue: 'https://github.com/dart-lang/linter/issues/4890',
- reason: 'Null check operator used on a null value')
test_augmentedField() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';
@@ -102,10 +99,6 @@
''');
}
- @FailingTest(
- issue: 'https://github.com/dart-lang/linter/issues/4890',
- reason:
- "CompileTimeErrorCode.DUPLICATE_DEFINITION [49, 1, The name 'v' is already defined.]")
test_augmentedTopLevelVariable() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';