go - VSCode 是如何发现这个 Go linting 问题的?我该如何忽略它?

标签 go visual-studio-code static-analysis

我正在我的 Go 项目中使用 golangci-lint 设置 linting。我有一个由 go-bindata 生成的文件,VSCode 在“问题”选项卡下列出了以下内容:

assets/assets.go: redundant type from array, slice, or map composite literal (simplifycompositelit)

我似乎无法摆脱它。这不是编译器错误,我会不时重新运行 go-bindata,所以我不想养成修改生成代码的习惯。

现在,使用下面的配置,我无法让 VSCode 停止报告此错误。如果我在工作区的根目录中运行 golangci-lint run ./...,我将不会得到任何输出。如果需要,我可以提供我的 linting 配置,但 VSCode 似乎正在运行其他东西。我如何找出报告此错误的原因以及如何让它停止报告此工作区中文件 assets/assets.go 的任何内容?

这是 Go 相关的 vscode 设置:

{
  "go.formatTool": "gofmt",
  "go.lintTool": "golangci-lint",
  "go.liveErrors": {
    "enabled": true,
    "delay": 500
  },
  "go.lintOnSave": "workspace",
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  },
  "go.useLanguageServer": true,
  "go.languageServerExperimentalFeatures": {
    "diagnostics": true,
    "documentLink": true
  },
}

这是有问题的行,即使有 nolint 注释也表明它没有按预期运行。如果是 golangci-lint 输出这个,nolint 会阻止显示警告。我重新加载窗口并关闭/重新打开 vscode 以确保注意到更改。

picture of the source where the linter is making this warning

最佳答案

在本地复制后,这条消息似乎来自gopls , 作为禁用 gopls使消息静音。 Go 问题跟踪器上有几个相关的投诉/问题:

两者都没有提供实际的解决方案。

然而,this issuevscode-go 上repo,提供了一个解决方法。在您的 VSCode 配置中,添加 gopls.analyses.simplifycompositelit键,值为 false :

    "gopls": {
        "analyses": {
            "simplifycompositelit": false
        },
    }

当然,这会为所有项目禁用它,不仅仅是生成的文件,但如果您还使用 golangci-lint ,它可以配置为捕获相同类型的错误,并且可以在更细粒度的基础上进行配置,这样您就不会错过非生成代码中的相同类别的错误。

关于go - VSCode 是如何发现这个 Go linting 问题的?我该如何忽略它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65484530/

相关文章:

regexp : multiline, 非贪婪匹配直到可选字符串

linux - 使用带有 Go 和 GoCV 的 OpenVINO 加载预训练的 DNN 模型 - 符号查找错误

go - Golang命令参数为空导致错误

typescript - 测试涉及打开文件夹/工作区的 VSCode 扩展

Golang Gorilla CEX.IO Websocket 认证错误

visual-studio-code - Google Cloud shell .customize_environment 不起作用

node.js - VSC 不会在正确的断点处中断

dart - 从dartanalyzer中排除所有生成的代码

java - 商业 Java 静态分析工具与免费的相比如何?

java - Findbugs 贡献 : Method throws alternative exception from catch block without history