don't overreport prefer_void_to_null in augmentations

See: https://github.com/dart-lang/linter/issues/4890

Change-Id: Ie2f3364e781c5b62cbea252460b28ba48482a552
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353203
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2024-02-20 21:01:50 +00:00 committed by Commit Queue
parent b0a96bf950
commit 3f5a96bbf4
2 changed files with 113 additions and 0 deletions

View file

@ -153,6 +153,12 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}
if (parent != null) {
AstNode? member = parent.thisOrAncestorOfType<ClassMember>();
member ??= parent.thisOrAncestorOfType<NamedCompilationUnitMember>();
if (member?.isAugmentation ?? false) return;
}
rule.reportLintForToken(node.name2);
}
}

View file

@ -17,6 +17,113 @@ class PreferVoidToNullTest extends LintRuleTest {
@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';
class A {
Future<Null>? f;
}
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment class A {
augment Future<Null>? f;
}
''');
}
test_augmentedFunction() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';
Future<Null>? f() => null;
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment Future<Null>? f() => null;
''');
}
test_augmentedGetter() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';
class A {
Future<Null>? get v => null;
}
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment class A {
augment Future<Null>? get v => null;
}
''');
}
test_augmentedMethod() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';
class A {
Future<Null>? f() => null;
}
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment class A {
augment Future<Null>? f() => null;
}
''');
}
@FailingTest(
issue: 'https://github.com/dart-lang/linter/issues/4890',
reason:
"CompileTimeErrorCode.DUPLICATE_DEFINITION [55, 1, The name 'v' is already defined.]")
test_augmentedTopLevelGetter() async {
newFile('$testPackageLibPath/a.dart', r'''
import augment 'test.dart';
Future<Null>? get v => null;
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment Future<Null>? get v => null;
''');
}
@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';
Future<Null>? v;
''');
await assertNoDiagnostics(r'''
library augment 'a.dart';
augment Future<Null>? v;
''');
}
/// https://github.com/dart-lang/linter/issues/4201
test_castAsExpression() async {
await assertNoDiagnostics(r'''