Skip to content

Commit 5535897

Browse files
authored
Add errchkjson linter (#2362)
1 parent fc888cf commit 5535897

12 files changed

+1362
-0
lines changed

.golangci.example.yml

+18
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ linters-settings:
137137
- io.Copy(*bytes.Buffer)
138138
- io.Copy(os.Stdout)
139139

140+
errchkjson:
141+
# with check-error-free-encoding set to true, errchkjson does warn about errors
142+
# from json encoding functions that are safe to be ignored,
143+
# because they are not possible to happen (default false)
144+
#
145+
# if check-error-free-encoding is set to true and errcheck linter is enabled,
146+
# it is recommended to add the following exceptions to prevent from false positives:
147+
#
148+
# linters-settings:
149+
# errcheck:
150+
# exclude-functions:
151+
# - encoding/json.Marshal
152+
# - encoding/json.MarshalIndent
153+
# - (*encoding/json.Encoder).Encode
154+
check-error-free-encoding: false
155+
# if report-no-exported is true, encoding a struct without exported fields is reported as issue (default false)
156+
report-no-exported: false
157+
140158
errorlint:
141159
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
142160
errorf: true

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/blizzy78/varnamelen v0.5.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

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+6
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

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

pkg/lint/lintersdb/manager.go

+7
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).
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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
issues:
2+
max-issues-per-linter: 100
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
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)