golang linter 总是提示

标签 go static-analysis golangci-lint

问:如何解决 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接口(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),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) {

关于golang linter 总是提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72385337/

相关文章:

go - shim.GetStateByPartialCompositeKey迭代器在此代码中如何工作?

python - 我如何有效地将列表的python3列表更改为golang多维数组?

regex - Golang 正则表达式在括号内获取值

go - 为什么没有类型不匹配错误?

java - 简单、普遍、基于代码分析器的 Java 问题

c# - .Net/C# 是否有工具来捕获类之间的*运行时*依赖关系?

c# - 检测缺失处置的工具

go - 在 Go 中使用 init() 函数真的很糟糕吗?