Skip to content

Commit

Permalink
analyzer: Report unused public members of private and unnamed extensions
Browse files Browse the repository at this point in the history
Fixes #41592

Change-Id: I95b79724bfcfc5d48b8f6cca61c6a3a681de3662
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/144353
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
srawlins authored and commit-bot@chromium.org committed May 7, 2020
1 parent 1279bbf commit d0d0952
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
15 changes: 10 additions & 5 deletions pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,18 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor {
return _usedElements.elements.contains(element);
}

bool _isUsedMember(Element element) {
bool _isUsedMember(ExecutableElement element) {
var enclosingElement = element.enclosingElement;
if (element.isPublic) {
if (_isPrivateClassOrExtension(element.enclosingElement) &&
element is ExecutableElement &&
if (enclosingElement is ClassElement &&
enclosingElement.isPrivate &&
element.isStatic) {
// Public static members of private classes, mixins, and extensions are
// inaccessible from outside the library in which they are declared.
// Public static members of private classes and mixins are inaccessible
// from outside the library in which they are declared.
} else if (enclosingElement is ExtensionElement &&
enclosingElement.isPrivate) {
// Public members of private extensions are inaccessible from outside
// the library in which they are declared.
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ f() => E.a;

test_thisAccessOnDynamic() async {
await assertNoErrorsInCode('''
extension on dynamic {
extension E on dynamic {
int get d => 3;
void testDynamic() {
Expand All @@ -1427,7 +1427,7 @@ extension on dynamic {

test_thisAccessOnFunction() async {
await assertNoErrorsInCode('''
extension on Function {
extension E on Function {
int get f => 4;
void testFunction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension <T> on T {
}
}
''', [
error(HintCode.UNUSED_ELEMENT, 23, 1),
error(CompileTimeErrorCode.SUPER_IN_EXTENSION, 33, 5),
]);
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/analyzer/test/src/diagnostics/unused_element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,17 @@ main() {
''');
}

test_method_isUsed_privateExtension() async {
await assertNoErrorsInCode(r'''
extension _A on String {
void m() {}
}
void main() {
"hello".m();
}
''');
}

test_method_isUsed_staticInvocation() async {
await assertNoErrorsInCode(r'''
class A {
Expand All @@ -726,6 +737,17 @@ main() {
''');
}

test_method_isUsed_unnamedExtension() async {
await assertNoErrorsInCode(r'''
extension on String {
void m() {}
}
void main() {
"hello".m();
}
''');
}

test_method_notUsed_hasSameNameAsUsed() async {
await assertErrorsInCode(r'''
class A {
Expand All @@ -750,6 +772,16 @@ class A {
]);
}

test_method_notUsed_privateExtension() async {
await assertErrorsInCode(r'''
extension _A on String {
void m() {}
}
''', [
error(HintCode.UNUSED_ELEMENT, 32, 1),
]);
}

test_method_notUsed_referenceFromItself() async {
await assertErrorsInCode(r'''
class A {
Expand Down Expand Up @@ -785,6 +817,16 @@ int g() => 7;
]);
}

test_method_notUsed_unnamedExtension() async {
await assertErrorsInCode(r'''
extension on String {
void m() {}
}
''', [
error(HintCode.UNUSED_ELEMENT, 29, 1),
]);
}

test_publicStaticMethod_privateClass_isUsed() async {
await assertNoErrorsInCode(r'''
class _A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ extension on void {
}
}
''', [
error(HintCode.UNUSED_ELEMENT, 22, 8),
error(StaticWarningCode.USE_OF_VOID_RESULT, 96, 4),
]);
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/analyzer/test/src/lint/linter/resolve_name_in_scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ extension on A {
this.foo();
}
}
''');
''', [
error(HintCode.UNUSED_ELEMENT, 53, 3),
]);
_checkMethodNone();
}

Expand All @@ -566,7 +568,9 @@ extension on A {
this.foo();
}
}
''');
''', [
error(HintCode.UNUSED_ELEMENT, 53, 3),
]);
_checkMethodRequested(findElement.parameter('foo'));
}

Expand All @@ -583,7 +587,9 @@ extension on A {
}
var foo = 0;
''');
''', [
error(HintCode.UNUSED_ELEMENT, 53, 3),
]);
_checkMethodRequested(findElement.topGet('foo'));
}

Expand All @@ -598,7 +604,9 @@ extension on A {
this.foo();
}
}
''');
''', [
error(HintCode.UNUSED_ELEMENT, 53, 3),
]);
_checkMethodRequested(findElement.method('foo'));
}

Expand Down

0 comments on commit d0d0952

Please sign in to comment.