在golinter的类型解析中,switch语句中缺少的情况。

huangapple go评论70阅读模式
英文:

missing cases in switch of type parsing.tokenType on golinter

问题

所以我有这段代码

token := NextToken()
switch token.Typ {
case tokenEOF:
    return nil
case tokenMentionTargetedControl:
    // 做一些操作
default:
    return nil
}

我的tokenType是一个枚举类型

我的golinter报了这个错误:

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

我不想为每种情况都创建一个case,因为我已经有了default子句,我该如何避免这个错误?

英文:

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?

答案1

得分: 1

exhaustive linter有一个default-signifies-exhaustive选项。将其设置为true

linters:
  enable:
    - exhaustive
linters-settings:
  exhaustive:
    # 即使没有列出所有枚举成员,在switch语句中存在"default" case也表示满足穷尽性。
    # 默认值: false
    default-signifies-exhaustive: true

请参阅https://golangci-lint.run/usage/linters/#exhaustive。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月14日 23:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定