Open srawlins opened 1 year ago
CC @goderbauer who has real examples from the Flutter framework.
Two examples (I think there are more):
For these, I think the suggestion should be to mark them "abstract final class". But not sure how to differentiate between what should be final and what should be interface.
The table in this section of the proposal (https://github.com/dart-lang/language/blob/master/accepted/future-releases/class-modifiers/feature-specification.md#syntax) does a good job of describing the semantics of each modifier combination and might be useful.
Maybe the lint should be: "If the class only contains static members*, consider marking it abstract final
"
*) Ideally, it would also recognize if the only non-static member is an empty private constructor and recommend removing it when marking the class abstract final
to account for existing code patterns I linked above.
I think in the analyzer cases, they we not classes which only declare static members. We direct users to avoid such classes in the first case. I don't see us adding lint rules that protect such classes.
prefer_abstract_final_over_extension_prohibiting_constructor
Description
Today you can prevent a public class from being extended by adding a private constructor:
Writing this singular private constructor means there is no default (implicit, unnamed, zero arg) constructor, and so the class cannot be extended outside of this library. The new way to achieve this behavior is to mark the class with
final
orinterface
.Kind
Does this enforce style advice? Guard against errors? Other?
Style, I guess. It would let us mark any unused constructor as unused. Today, we never mark a private constructor as unused if it is the only constructor, as it might be there to prevent extension.
Bad Examples
Good Examples
Discussion
I think we can maybe look at how the class is currently used (within the library?) to determine what modifier(s) we would recommend (and fix to).
Discussion checklist