Skip to content

Commit 613a95c

Browse files
committed
Add errchkjson linter
1 parent 1b53520 commit 613a95c

File tree

10 files changed

+1286
-0
lines changed

10 files changed

+1286
-0
lines changed

.golangci.example.yml

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ linters-settings:
9191
# should ignore tests (default false)
9292
skip-tests: false
9393

94+
errchkjson:
95+
# with omit-safe set to true, errchkjson does not warn about errors from json encoding functions that are safe
96+
# to be ignored, because they are not possible to happen (default false)
97+
omit-safe: false
98+
9499
dogsled:
95100
# checks assignments with too many blank identifiers; default is 2
96101
max-blank-identifiers: 2

go.mod

+1
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.1.1
19+
github.com/breml/errchkjson v0.1.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

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type LintersSettings struct {
8888
Dogsled DogsledSettings
8989
Dupl DuplSettings
9090
Errcheck ErrcheckSettings
91+
ErrChkJSON ErrChkJSONSettings
9192
ErrorLint ErrorLintSettings
9293
Exhaustive ExhaustiveSettings
9394
ExhaustiveStruct ExhaustiveStructSettings
@@ -152,6 +153,10 @@ type Cyclop struct {
152153
SkipTests bool `mapstructure:"skip-tests"`
153154
}
154155

156+
type ErrChkJSONSettings struct {
157+
OmitSafe bool `mapstructure:"omit-safe"`
158+
}
159+
155160
type DepGuardSettings struct {
156161
ListType string `mapstructure:"list-type"`
157162
Packages []string

pkg/golinters/errchkjson.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
if cfg != nil {
17+
if cfg.OmitSafe {
18+
cfgMap[a.Name] = map[string]interface{}{
19+
"omit-safe": "true",
20+
}
21+
}
22+
}
23+
24+
return goanalysis.NewLinter(
25+
"errchkjson",
26+
"Checks types passed to the json encoding functions. "+
27+
"Reports unsupported types and reports occations, where the check for the returned error can be omitted.",
28+
[]*analysis.Analyzer{a},
29+
cfgMap,
30+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
31+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
101101
//nolint:funlen
102102
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
103103
var cyclopCfg *config.Cyclop
104+
var errchkjsonCfg *config.ErrChkJSONSettings
104105
var errorlintCfg *config.ErrorLintSettings
105106
var exhaustiveCfg *config.ExhaustiveSettings
106107
var exhaustiveStructCfg *config.ExhaustiveStructSettings
@@ -127,6 +128,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
127128

128129
if m.cfg != nil {
129130
cyclopCfg = &m.cfg.LintersSettings.Cyclop
131+
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
130132
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
131133
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
132134
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
@@ -546,6 +548,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
546548
WithSince("1.43.0").
547549
WithPresets(linter.PresetBugs).
548550
WithURL("https://github.com/breml/bidichk"),
551+
linter.NewConfig(golinters.NewErrChkJSONFuncName(errchkjsonCfg)).
552+
WithSince("1.44.0").
553+
WithPresets(linter.PresetBugs, linter.PresetUnused).
554+
WithLoadForGoAnalysis().
555+
WithURL("https://github.com/breml/errchkjson"),
549556

550557
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
551558
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+
omit-safe: true

0 commit comments

Comments
 (0)