Skip to content

Commit 78f61e3

Browse files
committed
Add errchkjson linter
1 parent 9cdc7af commit 78f61e3

12 files changed

+1363
-0
lines changed

.golangci.example.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ linters-settings:
103103
# should ignore tests (default false)
104104
skip-tests: false
105105

106+
errchkjson:
107+
# with check-error-free-encoding set to true, errchkjson does warn about errors
108+
# from json encoding functions that are safe to be ignored,
109+
# because they are not possible to happen (default false)
110+
#
111+
# if check-error-free-encoding is set to true and errcheck linter is enabled,
112+
# it is recommended to add the following exceptions to prevent from false positives:
113+
#
114+
# linters-settings:
115+
# errcheck:
116+
# exclude-functions:
117+
# - encoding/json.Marshal
118+
# - encoding/json.MarshalIndent
119+
# - (*encoding/json.Encoder).Encode
120+
check-error-free-encoding: false
121+
# if report-no-exported is true, encoding a struct without exported fields is reported as issue (default false)
122+
report-no-exported: false
123+
106124
dogsled:
107125
# checks assignments with too many blank identifiers; default is 2
108126
max-blank-identifiers: 2

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/blizzy78/varnamelen v0.4.0
1717
github.com/bombsimon/wsl/v3 v3.3.0
1818
github.com/breml/bidichk v0.2.1
19+
github.com/breml/errchkjson v0.2.0
1920
github.com/butuzov/ireturn v0.1.1
2021
github.com/charithe/durationcheck v0.0.9
2122
github.com/daixiang0/gci v0.2.9

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type LintersSettings struct {
8989
Dogsled DogsledSettings
9090
Dupl DuplSettings
9191
Errcheck ErrcheckSettings
92+
ErrChkJSON ErrChkJSONSettings
9293
ErrorLint ErrorLintSettings
9394
Exhaustive ExhaustiveSettings
9495
ExhaustiveStruct ExhaustiveStructSettings
@@ -165,6 +166,11 @@ type Cyclop struct {
165166
SkipTests bool `mapstructure:"skip-tests"`
166167
}
167168

169+
type ErrChkJSONSettings struct {
170+
CheckErrorFreeEncoding bool `mapstructure:"check-error-free-encoding"`
171+
ReportNoExported bool `mapstructure:"report-no-exported"`
172+
}
173+
168174
type DepGuardSettings struct {
169175
ListType string `mapstructure:"list-type"`
170176
Packages []string

pkg/golinters/errchkjson.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package golinters
2+
3+
import (
4+
"golang.org/x/tools/go/analysis"
5+
6+
"github.com/breml/errchkjson"
7+
8+
"github.com/golangci/golangci-lint/pkg/config"
9+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
10+
)
11+
12+
func NewErrChkJSONFuncName(cfg *config.ErrChkJSONSettings) *goanalysis.Linter {
13+
a := errchkjson.NewAnalyzer()
14+
15+
cfgMap := map[string]map[string]interface{}{}
16+
cfgMap[a.Name] = map[string]interface{}{
17+
"omit-safe": true,
18+
}
19+
if cfg != nil {
20+
cfgMap[a.Name] = map[string]interface{}{
21+
"omit-safe": !cfg.CheckErrorFreeEncoding,
22+
"report-no-exported": cfg.ReportNoExported,
23+
}
24+
}
25+
26+
return goanalysis.NewLinter(
27+
"errchkjson",
28+
"Checks types passed to the json encoding functions. "+
29+
"Reports unsupported types and optionally reports occations, "+
30+
"where the check for the returned error can be omitted.",
31+
[]*analysis.Analyzer{a},
32+
cfgMap,
33+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
34+
}

pkg/lint/lintersdb/manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
102102
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
103103
var bidichkCfg *config.BiDiChkSettings
104104
var cyclopCfg *config.Cyclop
105+
var errchkjsonCfg *config.ErrChkJSONSettings
105106
var errorlintCfg *config.ErrorLintSettings
106107
var exhaustiveCfg *config.ExhaustiveSettings
107108
var exhaustiveStructCfg *config.ExhaustiveStructSettings
@@ -129,6 +130,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
129130
if m.cfg != nil {
130131
bidichkCfg = &m.cfg.LintersSettings.BiDiChk
131132
cyclopCfg = &m.cfg.LintersSettings.Cyclop
133+
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
132134
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
133135
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
134136
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
@@ -548,6 +550,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
548550
WithSince("1.43.0").
549551
WithPresets(linter.PresetBugs).
550552
WithURL("https://github.com/breml/bidichk"),
553+
linter.NewConfig(golinters.NewErrChkJSONFuncName(errchkjsonCfg)).
554+
WithSince("1.44.0").
555+
WithPresets(linter.PresetBugs, linter.PresetUnused).
556+
WithLoadForGoAnalysis().
557+
WithURL("https://github.com/breml/errchkjson"),
551558

552559
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
553560
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/configs/errchkjson.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
issues:
2+
max-issues-per-linter: 100
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
issues:
2+
max-issues-per-linter: 100
3+
linters-settings:
4+
errchkjson:
5+
check-error-free-encoding: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
errchkjson:
3+
report-no-exported: true

0 commit comments

Comments
 (0)