Skip to content

Report unused public elements in an entrypoint library #58292

@srawlins

Description

@srawlins

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 #57580 and #57824.

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions