Skip to content

exhaustive check for variables #5223

@huykingsofm

Description

@huykingsofm

Welcome

Your feature request related to a problem? Please describe

When I create an enum, sometimes I want to use a struct for that enum. But if I do that, the exhaustive linter cannot check the exhaustiveness.

Describe the solution you'd like

Check the exhaustiveness for variables which are declared right after the type definition.

type Enum struct {s string}

var (
     Enum1 = Enum{"1"}
     Enum2 = Enum{"2"}
     Enum3 = Enum{"3"}
)

var Enum4 = Enum{"4"}

func main() {
    var enum Enum
     switch enum {
     case Enum1:
     case Enum2:
     } 

     // Expected: missing Enum3.
     // Enum4 should be ignored.
}

Describe alternatives you've considered

I think it should check the exhaustiveness for constants which are declared right after type definition, rather than for all constants.

type Enum int

coinst (
     Enum1 Enum = iota
     Enum2
     Enum3
)

func main() {
    var enum Enum
     switch enum {
     case Enum1:
     case Enum2:
     }

    const Enum4 Enum = 42

     // Expected: missing Enum3.
     // Got: missing Enum3, Enum4.
     // In this case, Enum4 doesn't exist in scope of switch statement.
}

Additional context

No response

Supporter

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions