Search code examples
goglide-golanggolintgolangci-lint

missing cases in switch of type parsing.tokenType on golinter


So I have this code

token := NextToken()
switch token.Typ {
case tokenEOF:
    return nil
case tokenMentionTargetedControl:
    # do stuff
default:
    return nil
}

my tokentype is a enum

my golinter is throwing this error:

missing cases in switch of type parsing.tokenType: parsing.tokenControl, parsing.tokenLinkControl, parsing.tokenMailtoControl, parsing.tokenMentionControl, parsing.tokenChannelControl, parsing.tokenText (exhaustive)

I don't want to create a case for each scenario, since I have the default clause, how to I avoid this?


Solution

  • The exhaustive linter has the default-signifies-exhaustive option. Set it to true:

    linters:
      enable:
        - exhaustive
    linters-settings:
      exhaustive:
        # Presence of "default" case in switch statements satisfies exhaustiveness,
        # even if all enum members are not listed.
        # Default: false
        default-signifies-exhaustive: true
    

    See https://golangci-lint.run/usage/linters/#exhaustive.