Description
Proposal
Given a library with URI with pattern /test/.*_test.dart
, report any public top-level members except main
. "Top-level members in a test should be private (or relocated to within main
), to prevent unused members."
Secondary proposal
Given a library which does not live in it's declaring package's public API (i.e. files in bin/
, test/
, and tool/
), report any public top-level members except main
.
Justification
This lint helps prevent code bloat. Without full-program analysis, we cannot tell if a public member is used anywhere, so it is not reported by the analyzer as unused_variable or something similar. For top-level members, only private ones can be rigorously checked for usage. But in tests (and bin/
and tool/
scripts), which are typically their own entry points and never imported by another library, we want to be able to report unused top-level members.
This includes top-level variables, constants, functions, typedefs, enums, mixins, and classes.
This is a counter-request to #57580. I think this is better choice for a few reasons. #57580 proposes either a change to the analyzer's unused analysis to take into account the library URI ("does it end with _test.dart?"), leading to possible false positives. I don't think that's going to go well. The alternative is to implement a new unused analysis in the linter. Unused analysis is expensive though, and adding a second one in linter (duplicating a ton of code) will make it more expensive.
Downsides
One downside to recommending private top-level members is that it might be counter to what users typically do today. I think a lot of people just write regular, public, top-level consts and helper functions. Personally, I always write private top-level members, so that I know when something becomes unused. Perhaps a study should be done on what most people do. #57580 does not have this downside.