Golang的代码检查工具总是在抱怨。

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

golang linter always complains

问题

如何解决ireturnnolintlint两个 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 接口。

因此,我在函数中添加了一个 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 又抱怨我有一个不适用的异常。

这该怎么办呢?

英文:

Q: How do I resolve this madness between the ireturn and nolintlint linters?

Details:

I have a Golang function with this signature

func NewClientCredentialsTokenSource(
	issuer string,
	clientId string,
	clientSecret string,
	scope []string,
) (oauth2.TokenSource, error) {

When I run golangci-lint v1.43.0 it reports

golangci-lint run
oidc/token_utils.go:19:1: NewClientCredentialsTokenSource returns interface (golang.org/x/oauth2.TokenSource) (ireturn)
func NewClientCredentialsTokenSource(

Since the function has only two return params it is easy to deduce it is complaining about oauth2.TokenSource and not error.

The downstream function called by NewClientCredentialsTokenSource returns an instance of oauth2.TokenSource so I don't have a concrete type to return. I have no choice but to return the oauth2.TokenSource interface.

So I add a lint exception to the function like this:

//nolint:ireturn
func NewClientCredentialsTokenSource(
	issuer string,
	clientId string,
	clientSecret string,
	scope []string,
) (oauth2.TokenSource, error) {

You'd think that should fix it but no! Now there's a new lint issue is reported:

golangci-lint run
oidc/token_utils.go:19:1: directive `//nolint:ireturn` is unused for linter "ireturn" (nolintlint)
//nolint:ireturn

So now I'm chasing my tail. ireturn complains I am returning an interface. I add an exception for that function only to have nolintlint complain I have an exception that doesn't apply.

What's a guy to do?

答案1

得分: 3

我尝试按照Nico Huysamen的建议添加了allow规则,但这引发了一系列问题。最终,我仍然希望相信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) {
英文:

I tried adding the allow rule as suggested by Nico Huysamen but that opened a can of worms. In the end I still want to believe in the ireturn linter and don't want to disable it yet. I figure the cleanest way to resolve this issue is to add an exception for both linters, ireturn and nolintlint as shown here:

//nolint:nolintlint,ireturn
func NewClientCredentialsTokenSource(
	issuer string,
	clientId string,
	clientSecret string,
	scope []string,
) (oauth2.TokenSource, error) {

Update 5/25/2022:

Perhaps a better solution is shown below. For some reason the ireturn exception has to go inside the function signature.

func NewPasswordGrantTokenSource( //nolint:ireturn
	issuer string,
	clientId string,
	clientSecret string,
	username string,
	password string,
	scope []string,
) (oauth2.TokenSource, error) {

答案2

得分: 2

你可以在 .golangci.yml 中允许这个功能,就像上面提到的那样,但是由于它替换了标准默认值,你还需要包含它们,所以你需要:

  ireturn:
    # ireturn 允许同时使用 `allow` 和 `reject` 设置。
    # 这两个设置都是匹配接口或包名称的关键字和正则表达式列表。
    # 关键字:
    # - `empty` 表示 `interface{}`
    # - `error` 表示错误
    # - `stdlib` 表示标准库
    # - `anon` 表示匿名接口

    # 默认情况下,它允许使用错误、空接口、匿名接口和标准库提供的接口。
    allow:
      - anon
      - error
      - empty
      - stdlib
      # 您可以指定接口的惯用结尾
      - (or|er)$
      # 在这里添加您自定义的接口:
      - golang.org/x/oauth2.TokenSource
英文:

You can allow this in .golangci.yml as mentioned above but as it replaces the standard defaults, you need to include them too so you would need:

  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

答案3

得分: 0

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

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

或者通过.golangci.yml中的linter-settings部分进行设置

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

You can add the interface to ireturn's list of allowed interfaces.

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

or via the linter-settings section in .golangci.yml

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

huangapple
  • 本文由 发表于 2022年5月26日 08:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/72385337.html
匿名

发表评论

匿名网友

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

确定