Skip to content

Commit

Permalink
analyzer: stop reporting on constructors exposed via type alias
Browse files Browse the repository at this point in the history
Fixes #54543

Change-Id: Iaedcbbee1b11acc9bea52857d087ce3099da056e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381104
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Aug 19, 2024
1 parent 5377cb5 commit 9a2dfac
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
Expand Down Expand Up @@ -156,6 +157,21 @@ class GatherUsedLocalElementsVisitor extends RecursiveAstVisitor<void> {
super.visitFunctionExpressionInvocation(node);
}

@override
void visitGenericTypeAlias(GenericTypeAlias node) {
if (!Identifier.isPrivateName(node.name.lexeme)) {
var type = node.type.type;
if (type is InterfaceType) {
for (var constructor in type.constructors) {
if (!Identifier.isPrivateName(constructor.name)) {
usedElements.addElement(constructor);
}
}
}
}
super.visitGenericTypeAlias(node);
}

@override
void visitIndexExpression(IndexExpression node) {
var element = node.writeOrReadElement;
Expand Down
94 changes: 93 additions & 1 deletion pkg/analyzer/test/src/diagnostics/unused_element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class A<T> {
''');
}

test_class_isUsed_exposedViaTypeAlias() async {
await assertNoErrorsInCode(r'''
class _A {}
typedef T = _A;
''');
}

test_class_isUsed_extends() async {
await assertNoErrorsInCode(r'''
class _A {}
Expand Down Expand Up @@ -387,6 +394,27 @@ class B extends A {
]);
}

test_constructorPublic_privateClass_exposedViaTypeAlias() async {
await assertNoErrorsInCode(r'''
class _A {
_A.constructor();
}
typedef T = _A;
''');
}

test_constructorPublic_privateClass_notUsed() async {
await assertErrorsInCode(r'''
class _A {
_A.named();
_A();
}
var a = _A();
''', [
error(WarningCode.UNUSED_ELEMENT, 16, 5),
]);
}

test_enum_constructor_parameter_optionalNamed_isUsed() async {
await assertNoErrorsInCode(r'''
enum E {
Expand Down Expand Up @@ -542,6 +570,56 @@ void f() {
]);
}

test_extensionType_privateConstructor() async {
await assertErrorsInCode('''
extension type E(int i) {
E._named(this.i);
}
''', [
error(WarningCode.UNUSED_ELEMENT, 30, 6),
]);
}

test_extensionType_privateConstructor_notExposedViaTypeAlias() async {
await assertErrorsInCode('''
extension type E(int i) {
E._named(this.i);
}
typedef A = E;
''', [
error(WarningCode.UNUSED_ELEMENT, 30, 6),
]);
}

test_extensionTypePrivate_publicConstructor() async {
await assertErrorsInCode('''
extension type _E(int i) {
_E.named(this.i);
}
''', [
error(WarningCode.UNUSED_ELEMENT, 32, 5),
]);
}

test_extensionTypePrivate_publicConstructor_exposedViaTypeAlias() async {
await assertNoErrorsInCode('''
extension type _E(int i) {
_E.named(this.i);
}
typedef A = _E;
''');
}

test_extensionTypePrivate_publicConstructor_exposedViaTypeAlias_indirect() async {
await assertNoErrorsInCode('''
extension type _E(int i) {
_E.named(this.i);
}
typedef _A = _E;
typedef B = _A;
''');
}

test_factoryConstructor_notUsed_multiple() async {
await assertErrorsInCode(r'''
class A {
Expand Down Expand Up @@ -2442,6 +2520,21 @@ enum E {
''');
}

test_publicEnum_privateConstructor_notExposedViaTypeAlias() async {
await assertNoErrorsInCode(r'''
enum _E {
one(), two();
const _E();
const _E.named();
}
typedef T = _E;
void f() {
_E.one;
_E.two;
}
''');
}

test_publicEnum_privateConstructor_notUsed() async {
await assertErrorsInCode(r'''
enum E {
Expand Down Expand Up @@ -2554,7 +2647,6 @@ enum E {
v.foo();
const E.foo();
const E.bar();
factory E.baz() => throw 0;
}
''', [
error(WarningCode.UNUSED_ELEMENT, 47, 3),
Expand Down

0 comments on commit 9a2dfac

Please sign in to comment.