Skip to content

Commit

Permalink
Add errchkjson linter (#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
breml authored Nov 26, 2021
1 parent fc888cf commit 5535897
Show file tree
Hide file tree
Showing 12 changed files with 1,362 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
issues:
max-issues-per-linter: 100
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
errchkjson:
report-no-exported: true
Loading

0 comments on commit 5535897

Please sign in to comment.