2

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?

Raul Quinzani
  • 493
  • 1
  • 4
  • 16
  • Disable this linter or this check. Or have a case with all that `Typ`s the linter complains and a default that panics. – Volker May 14 '23 at 18:59
  • I would like to avoid disabling this linter, he is there for a reason, for now, I have a case with all the linter complaints, but seems like a poor solution – Raul Quinzani May 14 '23 at 20:13

1 Answers1

1

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.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23