Skip to content

Commit

Permalink
[analyzer] Allow deprecated uses inside deprecated enums and extensions
Browse files Browse the repository at this point in the history
Bug: #51664
Change-Id: I65847a704e8a1dc17b47f2783d009669c703b1e5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287674
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Mar 15, 2023
1 parent 295e210 commit 7e72be6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/analyzer/lib/src/error/best_practices_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
super.visitDefaultFormalParameter(node);
}

@override
void visitEnumDeclaration(EnumDeclaration node) {
_deprecatedVerifier
.pushInDeprecatedValue(node.declaredElement!.hasDeprecated);

try {
super.visitEnumDeclaration(node);
} finally {
_deprecatedVerifier.popInDeprecated();
}
}

@override
void visitExportDirective(ExportDirective node) {
_deprecatedVerifier.exportDirective(node);
Expand All @@ -491,6 +503,18 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
super.visitExpressionFunctionBody(node);
}

@override
void visitExtensionDeclaration(ExtensionDeclaration node) {
_deprecatedVerifier
.pushInDeprecatedValue(node.declaredElement!.hasDeprecated);

try {
super.visitExtensionDeclaration(node);
} finally {
_deprecatedVerifier.popInDeprecated();
}
}

@override
void visitFieldDeclaration(FieldDeclaration node) {
_deprecatedVerifier.pushInDeprecatedMetadata(node.metadata);
Expand Down
62 changes: 62 additions & 0 deletions pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,36 @@ class C {
''');
}

test_inDeprecatedEnum() async {
await assertNoErrorsInCode(r'''
@deprecated
void f() {}
@deprecated
enum E {
one, two;
void m() {
f();
}
}
''');
}

test_inDeprecatedExtension() async {
await assertNoErrorsInCode(r'''
@deprecated
void f() {}
@deprecated
extension E on int {
void m() {
f();
}
}
''');
}

test_inDeprecatedField() async {
await assertNoErrorsInCode(r'''
@deprecated
Expand Down Expand Up @@ -892,6 +922,38 @@ f(A a) {
]);
}

test_inEnum() async {
await assertErrorsInCode(r'''
@deprecated
void f() {}
enum E {
one, two;
void m() {
f();
}
}
''', [
error(HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE, 64, 1),
]);
}

test_inExtension() async {
await assertErrorsInCode(r'''
@deprecated
void f() {}
extension E on int {
void m() {
f();
}
}
''', [
error(HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE, 63, 1),
]);
}

test_instanceCreation_namedConstructor() async {
await assertErrorsInCode(r'''
class A {
Expand Down

0 comments on commit 7e72be6

Please sign in to comment.