【问题标题】:golang linter always complainsgolang linter 总是抱怨
【发布时间】:2022-07-05 05:48:07
【问题描述】:

问:如何解决 ireturnnolintlint linters 之间的这种疯狂?

详情:

我有一个带有这个签名的 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接口。

所以我向函数添加了一个 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 抱怨我正在返回一个接口。我为该函数添加了一个例外,只是为了让nolintlint 抱怨我有一个不适用的例外。

男人要做什么?

【问题讨论】:

  • “一个人要做什么?”我只会禁用nolintlint。这似乎太过分了。

标签: go static-analysis golangci-lint


【解决方案1】:

我尝试按照 Nico Huysamen 的建议添加允许规则,但这打开了一罐蠕虫。最后,我仍然想相信ireturn linter,并且还不想禁用它。我认为解决此问题的最简洁方法是为两个 linter 添加一个异常,ireturnnolintlint,如下所示:

//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) {

【讨论】:

    【解决方案2】:

    您可以将该接口添加到ireturn 的允许接口列表中。

    ireturn --allow="golang.org/x/oauth2.TokenSource"
    

    或通过.golangci.yml 中的linter-settings 部分

    linters-settings:
      ireturn:
        allow:
          - golang.org/x/oauth2.TokenSource
    

    【讨论】:

    • 我试过你的建议,但它打开了一罐蠕虫。在 .golangci.yml 中添加一个接口作为异常之后,linter 然后抱怨每个返回 error 的方法/函数,所以我为 error 添加了一个异常。然后它抱怨每个返回 net/http.Handler 的函数(它以前没有这样做过。)在那之后是最大的侮辱,它抱怨返回 interface{} 的函数,我找不到添加例外的方法interface{}(由于 Go 1.17.3 中缺少泛型,interface{} 已在有限的情况下使用。)
    • 哎哟。也许然后只是禁用'ireturn'??
    【解决方案3】:

    如上所述,您可以在 .golangci.yml 中允许它,但由于它取代了 standard defaults,因此您也需要包含它们,因此您需要:

      ireturn:
        # ireturn allows using `allow` and `reject` settings at the same time.
        # Both settings are lists of the keywords and regular expressions matched to interface or package names.
        # keywords:
        # - `empty` for `interface{}`
        # - `error` for errors
        # - `stdlib` for standard library
        # - `anon` for anonymous interfaces
    
        # By default, it allows using errors, empty interfaces, anonymous interfaces,
        # and interfaces provided by the standard library.
        allow:
          - anon
          - error
          - empty
          - stdlib
          # You can specify idiomatic endings for interface
          - (or|er)$
          # Your custom interfaces go here:
          - golang.org/x/oauth2.TokenSource
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 2016-10-30
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2012-01-28
      相关资源
      最近更新 更多