Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errchkjson linter #2362

Merged
merged 4 commits into from Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .golangci.example.yml
Expand Up @@ -137,6 +137,24 @@ linters-settings:
- io.Copy(*bytes.Buffer)
- io.Copy(os.Stdout)

errchkjson:
# with check-error-free-encoding set to true, errchkjson does warn about errors
# from json encoding functions that are safe to be ignored,
# because they are not possible to happen (default false)
#
# if check-error-free-encoding is set to true and errcheck linter is enabled,
# it is recommended to add the following exceptions to prevent from false positives:
#
# linters-settings:
# errcheck:
# exclude-functions:
# - encoding/json.Marshal
# - encoding/json.MarshalIndent
# - (*encoding/json.Encoder).Encode
check-error-free-encoding: false
# if report-no-exported is true, encoding a struct without exported fields is reported as issue (default false)
report-no-exported: false

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
errorf: true
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -16,6 +16,7 @@ require (
github.com/blizzy78/varnamelen v0.5.0
github.com/bombsimon/wsl/v3 v3.3.0
github.com/breml/bidichk v0.2.1
github.com/breml/errchkjson v0.2.0
github.com/butuzov/ireturn v0.1.1
github.com/charithe/durationcheck v0.0.9
github.com/daixiang0/gci v0.2.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -89,6 +89,7 @@ type LintersSettings struct {
Dogsled DogsledSettings
Dupl DuplSettings
Errcheck ErrcheckSettings
ErrChkJSON ErrChkJSONSettings
ErrorLint ErrorLintSettings
Exhaustive ExhaustiveSettings
ExhaustiveStruct ExhaustiveStructSettings
Expand Down Expand Up @@ -165,6 +166,11 @@ type Cyclop struct {
SkipTests bool `mapstructure:"skip-tests"`
}

type ErrChkJSONSettings struct {
CheckErrorFreeEncoding bool `mapstructure:"check-error-free-encoding"`
ReportNoExported bool `mapstructure:"report-no-exported"`
}

type DepGuardSettings struct {
ListType string `mapstructure:"list-type"`
Packages []string
Expand Down
33 changes: 33 additions & 0 deletions pkg/golinters/errchkjson.go
@@ -0,0 +1,33 @@
package golinters

import (
"github.com/breml/errchkjson"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewErrChkJSONFuncName(cfg *config.ErrChkJSONSettings) *goanalysis.Linter {
a := errchkjson.NewAnalyzer()

cfgMap := map[string]map[string]interface{}{}
cfgMap[a.Name] = map[string]interface{}{
"omit-safe": true,
}
if cfg != nil {
cfgMap[a.Name] = map[string]interface{}{
"omit-safe": !cfg.CheckErrorFreeEncoding,
"report-no-exported": cfg.ReportNoExported,
}
}

return goanalysis.NewLinter(
"errchkjson",
"Checks types passed to the json encoding functions. "+
"Reports unsupported types and optionally reports occations, "+
"where the check for the returned error can be omitted.",
[]*analysis.Analyzer{a},
cfgMap,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -102,6 +102,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var bidichkCfg *config.BiDiChkSettings
var cyclopCfg *config.Cyclop
var errchkjsonCfg *config.ErrChkJSONSettings
var errorlintCfg *config.ErrorLintSettings
var exhaustiveCfg *config.ExhaustiveSettings
var exhaustiveStructCfg *config.ExhaustiveStructSettings
Expand Down Expand Up @@ -129,6 +130,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
if m.cfg != nil {
bidichkCfg = &m.cfg.LintersSettings.BiDiChk
cyclopCfg = &m.cfg.LintersSettings.Cyclop
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
Expand Down Expand Up @@ -548,6 +550,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("1.43.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/breml/bidichk"),
linter.NewConfig(golinters.NewErrChkJSONFuncName(errchkjsonCfg)).
WithSince("1.44.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/breml/errchkjson"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/configs/errchkjson.yml
@@ -0,0 +1,2 @@
issues:
max-issues-per-linter: 100
@@ -0,0 +1,5 @@
issues:
max-issues-per-linter: 100
linters-settings:
errchkjson:
check-error-free-encoding: true
3 changes: 3 additions & 0 deletions test/testdata/configs/errchkjson_no_exported.yml
@@ -0,0 +1,3 @@
linters-settings:
errchkjson:
report-no-exported: true