问:如何解决 ireturn
和 nolintlint
linter 之间的这种疯狂?
详细信息:
我有一个带有这个签名的 Golang 函数
func NewClientCredentialsTokenSource(
issuer string,
clientId string,
clientSecret string,
scope []string,
) (oauth2.TokenSource, error) {
当我运行 golangci-lint v1.43.0 时它会报告
golangci-lint run
oidc/token_utils.go:19:1: NewClientCredentialsTokenSource returns interface (golang.org/x/oauth2.TokenSource) (ireturn)
func NewClientCredentialsTokenSource(
由于该函数只有两个返回参数,因此很容易推断它是在提示 oauth2.TokenSource
而不是 error
。
NewClientCredentialsTokenSource
调用的下游函数返回 oauth2.TokenSource
的实例,因此我没有要返回的具体类型。我别无选择,只能返回oauth2.TokenSource
接口(interface)。
所以我像这样向函数添加一个 lint 异常:
//nolint:ireturn
func NewClientCredentialsTokenSource(
issuer string,
clientId string,
clientSecret string,
scope []string,
) (oauth2.TokenSource, error) {
您认为应该可以解决问题,但不是!现在报告了一个新的 lint 问题:
golangci-lint run
oidc/token_utils.go:19:1: directive `//nolint:ireturn` is unused for linter "ireturn" (nolintlint)
//nolint:ireturn
所以现在我在追我的尾部。 ireturn
提示我正在返回一个接口(interface)。我为该函数添加了一个异常(exception),结果 nolintlint
提示我有一个不适用的异常(exception)。
男人要做什么?
最佳答案
我尝试按照 Nico Huysamen 的建议添加允许规则,但这打开了一 jar 蠕虫。最后我还是想相信 ireturn
linter 并且还不想禁用它。我认为解决这个问题最简洁的方法是为两个 linters 添加一个异常(exception),ireturn
和 nolintlint
如下所示:
//nolint:nolintlint,ireturn
func NewClientCredentialsTokenSource(
issuer string,
clientId string,
clientSecret string,
scope []string,
) (oauth2.TokenSource, error) {
2022 年 5 月 25 日更新:
也许更好的解决方案如下所示。出于某种原因,ireturn
异常必须进入函数签名内部。
func NewPasswordGrantTokenSource( //nolint:ireturn
issuer string,
clientId string,
clientSecret string,
username string,
password string,
scope []string,
) (oauth2.TokenSource, error) {
关于golang linter 总是提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72385337/