Skip to content

consider a use_enums lint to encourage migration from enum-like classes to enhanced enums #58624

Closed
Listed in
dart-archive/linter
#3253
@pq

Description

@pq
No description provided.

Activity

changed the title [-]consider a `prefer_enums` lint[/-] [+]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/+] on Jan 24, 2022
jacob314

jacob314 commented on Jan 24, 2022

@jacob314
Member

Like other language feature migration lints, this is a lint that I would want to have ship with a quickfix to make it easy for users to learn how to do the migration.

bwilkerson

bwilkerson commented on Jan 26, 2022

@bwilkerson
Member

As a starting point we can probably use the existing definition of an enum-like class to define which classes to lint, but we might need to make some adjustments. For reference, the documentation currently states:

An enum-like class must meet the following requirements:

  • is concrete,
  • has no public constructors,
  • has no factory constructors,
  • has two or more static const fields with the same type as the class,
  • has no subclasses declared in the defining library.

We probably need to add at least one additional criteria:

  • is a subclass of Object (because enum's can't extend other classes).

If enums aren't allowed to have mixins then we'll also need to check that the class being linted doesn't have a with clause.

Are there other criteria we need to check for?

pq

pq commented on Feb 26, 2022

@pq
MemberAuthor

Are there other criteria we need to check for?

My first cut implementation settled on a slight variation on the list above. (See PR.)

TL;DR:

Candidates for enums are classes that:
  * are concrete,
  * are private or have only private generative constructors,
  * have two or more static const fields with the same type as the class,
  * have generative constructors that are only invoked at the top-level of the
    initialization expression of these static fields,
  * do not define `hashCode`, `==`, `values` or `index`,
  * do not extend any class other than Object, and
  * have no subclasses declared in the defining library.

As mentioned in the WIP PR, the index restriction isn't strictly necessary and I'm open to expanding the check to handle cases where classes use an index in an enum-compatible way.

changed the title [-]consider a `prefer_enums` lint to encourage migration from enum-like classes to enhanced enums[/-] [+]consider a `use_enums` lint to encourage migration from enum-like classes to enhanced enums[/+] on Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @pq@jacob314@devoncarew@bwilkerson

      Issue actions

        consider a `use_enums` lint to encourage migration from enum-like classes to enhanced enums · Issue #58624 · dart-lang/sdk