Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report unused public elements in an entrypoint library #2395

Closed
srawlins opened this issue Dec 22, 2020 · 1 comment · Fixed by #3513
Closed

Report unused public elements in an entrypoint library #2395

srawlins opened this issue Dec 22, 2020 · 1 comment · Fixed by #3513
Labels
lint-request type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

Describe the rule you'd like to see implemented

If a library has an entrypoint, I'd like to report any publicly accessible elements which are not used within the library as unused.

An entrypoint is a top-level main function, or a function annotated with @pragma(vm:entry-point). These files are typically:

  • a VM script in bin/,
  • a web app in web/,
  • a VM tool in tool/,
  • a test like test/**/*_test.dart,

I think there will be very very few false positives. And perhaps any false positive indicates a code smell in library arrangement (the analyzer codebase I believe has some test files which reference another test file, each of which has a main).

Any element which is not publicly visible should already be reported with analyzer's unused_local_variable, unused_element, or unused_field diagnostics. This lint would cover publicly visible elements:

  • Any public class instance members (fields, methods, constructors), even those on private classes and mixins.
  • Any public top-level functions, variables, classes, enums, mixins, extensions, and typedefs.

Examples

// foo_test.dart

void main() {
  test(...);
}

// Unused class
class MockStream extends Mock implements Stream {
  // Unused field
  int y = 7;

  // Unused method
  void m() {}
}

// Unused top-level function
void helper() {}

// Unused top-level variable
const x = 1;

Additional context

This is an alternative route to #685 and #1258.

@goderbauer
Copy link
Contributor

As a data point: Recently did some clean-up in the framework with the help of this tool: https://pub.dev/packages/dart_code_metrics. We had accumulated quite a bit of dead code in tests: flutter/flutter#104550.

This proposal (or #1258 or #685) probably could have prevented that build-up of dead code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants