This repository was archived by the owner on Jan 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
add unused_top_members_in_executable_libraries #3513
Merged
srawlins
merged 8 commits into
dart-archive:main
from
a14n:unused_top_members_in_executable_libraries
Aug 10, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7fb8f15
add unused_top_members_in_executable_libraries
a14n 4b08f5f
address review comments
a14n da344fe
address review comments
a14n e666287
rename to unreachable_from_main
a14n 3a8e7e8
report on name
a14n a1d84ae
address review comments
a14n 80865bb
address review comments
a14n 7bbd899
fix post-rebase
a14n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:collection'; | ||
|
|
||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/dart/element/element.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:collection/collection.dart'; | ||
|
|
||
| import '../analyzer.dart'; | ||
| import '../util/dart_type_utilities.dart'; | ||
|
|
||
| const _desc = 'Unreachable top-level members in executable libraries.'; | ||
|
|
||
| const _details = r''' | ||
|
|
||
| Top-level members in an executable library should be used directly inside this | ||
| library. An executable library is a library that contains a `main` top-level | ||
| function or that contains a top-level function annotated with | ||
| `@pragma('vm:entry-point')`). Executable libraries are not usually imported | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a stray |
||
| and it's better to avoid defining unused members. | ||
|
|
||
| This rule assumes that an executable library isn't imported by other files | ||
| except to execute its `main` function. | ||
|
|
||
| **BAD:** | ||
|
|
||
| ```dart | ||
| main() {} | ||
| void f() {} | ||
| ``` | ||
|
|
||
| **GOOD:** | ||
|
|
||
| ```dart | ||
| main() { | ||
| f(); | ||
| } | ||
| void f() {} | ||
| ``` | ||
|
|
||
| '''; | ||
|
|
||
| class UnreachableFromMain extends LintRule { | ||
| UnreachableFromMain() | ||
| : super( | ||
| name: 'unreachable_from_main', | ||
| description: _desc, | ||
| details: _details, | ||
| group: Group.style, | ||
| maturity: Maturity.experimental, | ||
| ); | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| NodeLintRegistry registry, | ||
| LinterContext context, | ||
| ) { | ||
| var visitor = _Visitor(this); | ||
| registry.addCompilationUnit(this, visitor); | ||
| } | ||
| } | ||
|
|
||
| class _Visitor extends SimpleAstVisitor<void> { | ||
| _Visitor(this.rule); | ||
|
|
||
| final LintRule rule; | ||
|
|
||
| @override | ||
| void visitCompilationUnit(CompilationUnit node) { | ||
| // TODO(a14n): add support of libs with parts | ||
| if (node.directives.whereType<PartOfDirective>().isNotEmpty) return; | ||
| if (node.directives.whereType<PartDirective>().isNotEmpty) return; | ||
|
|
||
| var topDeclarations = node.declarations | ||
| .expand((e) => [ | ||
| if (e is TopLevelVariableDeclaration) | ||
| ...e.variables.variables | ||
| else | ||
| e, | ||
| ]) | ||
| .toSet(); | ||
|
|
||
| var entryPoints = topDeclarations.where(_isEntryPoint).toList(); | ||
| if (entryPoints.isEmpty) return; | ||
|
|
||
| var declarationByElement = <Element, Declaration>{}; | ||
| for (var declaration in topDeclarations) { | ||
| var element = declaration.declaredElement; | ||
| if (element != null) { | ||
| if (element is TopLevelVariableElement) { | ||
| declarationByElement[element] = declaration; | ||
| var getter = element.getter; | ||
| if (getter != null) declarationByElement[getter] = declaration; | ||
| var setter = element.setter; | ||
| if (setter != null) declarationByElement[setter] = declaration; | ||
| } else { | ||
| declarationByElement[element] = declaration; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The following map contains for every declaration the set of the | ||
| // declarations it references. | ||
| var dependencies = Map<Declaration, Set<Declaration>>.fromIterable( | ||
srawlins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| topDeclarations, | ||
| value: (declaration) => | ||
| DartTypeUtilities.traverseNodesInDFS(declaration as Declaration) | ||
| .expand((e) => [ | ||
| if (e is SimpleIdentifier) e.staticElement, | ||
| // with `id++` staticElement of `id` is null | ||
| if (e is CompoundAssignmentExpression) ...[ | ||
| e.readElement, | ||
| e.writeElement, | ||
| ], | ||
| ]) | ||
| .whereNotNull() | ||
| .map((e) => e.thisOrAncestorMatching((a) => | ||
| a.enclosingElement3 == null || | ||
| a.enclosingElement3 is CompilationUnitElement)) | ||
| .map((e) => declarationByElement[e]) | ||
| .whereNotNull() | ||
| .where((e) => e != declaration) | ||
| .toSet(), | ||
| ); | ||
|
|
||
| var usedMembers = entryPoints.toSet(); | ||
| // The following variable will be used to visit every reachable declaration | ||
| // starting from entry-points. At every loop an element is removed. This | ||
| // element is marked as used and we add its dependencies in the declaration | ||
| // list to traverse. Once this list is empty `usedMembers` contains every | ||
| // declarations reachable from an entry-point. | ||
| var toTraverse = Queue.of(usedMembers); | ||
| while (toTraverse.isNotEmpty) { | ||
| var declaration = toTraverse.removeLast(); | ||
| for (var dep in dependencies[declaration]!) { | ||
| if (usedMembers.add(dep)) { | ||
| toTraverse.add(dep); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var unusedMembers = topDeclarations.difference(usedMembers).where((e) { | ||
| var element = e.declaredElement; | ||
| return element != null && | ||
| element.isPublic && | ||
| !element.hasVisibleForTesting; | ||
| }); | ||
|
|
||
| for (var member in unusedMembers) { | ||
| if (member is NamedCompilationUnitMember) { | ||
| rule.reportLintForToken(member.name2); | ||
| } else if (member is VariableDeclaration) { | ||
| rule.reportLintForToken(member.name2); | ||
| } else if (member is ExtensionDeclaration) { | ||
| rule.reportLintForToken( | ||
| member.name2 ?? member.firstTokenAfterCommentAndMetadata); | ||
| } else { | ||
| rule.reportLintForToken(member.firstTokenAfterCommentAndMetadata); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool _isEntryPoint(Declaration e) => | ||
| e is FunctionDeclaration && | ||
| (e.name2.lexeme == 'main' || e.metadata.any(_isPragmaVmEntry)); | ||
|
|
||
| bool _isPragmaVmEntry(Annotation annotation) { | ||
| if (!annotation.isPragma) return false; | ||
| var value = annotation.elementAnnotation?.computeConstantValue(); | ||
| if (value == null) return false; | ||
| var name = value.getField('name'); | ||
| return name != null && | ||
| name.hasKnownValue && | ||
| name.toStringValue() == 'vm:entry-point'; | ||
| } | ||
| } | ||
|
|
||
| extension on Element { | ||
| bool get isPragma => (library?.isDartCore ?? false) && name == 'pragma'; | ||
| } | ||
|
|
||
| extension on Annotation { | ||
| bool get isPragma { | ||
| var element = elementAnnotation?.element; | ||
| DartType type; | ||
| if (element is ConstructorElement) { | ||
| type = element.returnType; | ||
| } else if (element is PropertyAccessorElement && element.isGetter) { | ||
| type = element.returnType; | ||
| } else { | ||
| // Dunno what this is. | ||
| return false; | ||
| } | ||
| return type is InterfaceType && type.element2.isPragma; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| // test w/ `dart test -N unreachable_from_main` | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| /// see [Comment] | ||
| main() // OK | ||
| { | ||
| _f5(); | ||
| f1(); | ||
| f3(() { | ||
| f4(b); | ||
| }); | ||
| f4(b); | ||
| usageInTypeBound(); | ||
| usageInFunctionType(); | ||
| usageInDefaultValue(); | ||
| usageInAnnotation(); | ||
| Future<C5>.value(C5()).extensionUsage(); | ||
| accessors(); | ||
| print(c2); | ||
| } | ||
|
|
||
| class Comment {} // OK | ||
|
|
||
| const a = 1; // LINT | ||
srawlins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const b = 1; // OK | ||
|
|
||
| final int // | ||
| c1 = 1, // LINT | ||
| c2 = 2; // OK | ||
|
|
||
| int v = 1; // LINT | ||
|
|
||
| typedef A = String; // LINT | ||
|
|
||
| class C {} // LINT | ||
|
|
||
| mixin M {} // LINT | ||
|
|
||
| enum E { e } // LINT | ||
|
|
||
| void f() {} // LINT | ||
|
|
||
| @visibleForTesting | ||
| void forTest() {} // OK | ||
|
|
||
| void f1() // OK | ||
| { | ||
| f2(); | ||
| } | ||
|
|
||
| void f2() // OK | ||
| { | ||
| f1(); | ||
| } | ||
|
|
||
| void f3(Function f) {} // OK | ||
| void f4(int p) {} // OK | ||
|
|
||
| int id = 0; // OK | ||
| void _f5() { | ||
| id++; | ||
| } | ||
|
|
||
| @pragma('vm:entry-point') | ||
| void f6() {} // OK | ||
|
|
||
| const entryPoint = pragma('vm:entry-point'); | ||
| @entryPoint | ||
| void f7() {} // OK | ||
|
|
||
| @pragma('other') | ||
| void f8() {} // LINT | ||
|
|
||
| // test accessors | ||
| int get id9 => 0; | ||
| void set id9(int value) {} | ||
| void accessors() { | ||
| id9 += 4; // usage | ||
| } | ||
|
|
||
| // Usage in type bound | ||
| class C1 {} | ||
|
|
||
| void usageInTypeBound<T extends C1>() {} | ||
|
|
||
| // Usage in Function type | ||
| class C2 {} | ||
|
|
||
| void Function(C2)? usageInFunctionType() => null; | ||
|
|
||
| // Usage in default value | ||
| class C3 { | ||
| const C3(); | ||
| } | ||
|
|
||
| void usageInDefaultValue([Object? p = const C3()]) {} | ||
|
|
||
| // Usage in annotation | ||
| class C4 { | ||
| const C4(); | ||
| } | ||
|
|
||
| @C4() | ||
| void usageInAnnotation() {} | ||
|
|
||
| // Usage in type parameter in extension `on` clause. | ||
| class C5 {} | ||
|
|
||
| extension UsedPublicExt on Future<C5> { | ||
| extensionUsage() {} | ||
| } | ||
|
|
||
| // Usage in type parameter in extension `on` clause. | ||
| class C6 {} //LINT | ||
|
|
||
| extension UnusedPublicExt on C6 //LINT | ||
| { | ||
| m() {} | ||
| } | ||
|
|
||
| class C7 // LINT | ||
| { | ||
| C7(); | ||
| C7.named(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.