英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论