Skip to content

Commit 1685402

Browse files
authored
gomnd: new configuration (#2498)
1 parent dff995c commit 1685402

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

.golangci.example.yml

+24-14
Original file line numberDiff line numberDiff line change
@@ -414,20 +414,30 @@ linters-settings:
414414
min-confidence: 0.8
415415

416416
gomnd:
417-
settings:
418-
mnd:
419-
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
420-
checks:
421-
- argument
422-
- case
423-
- condition
424-
- operation
425-
- return
426-
- assign
427-
# Next settings are expecting comma separated string values
428-
ignored-numbers: "0666,0755,42" # values always ignored: 1, 1.0, 0 and 0.0
429-
ignored-files: "magic1_.*.go" # values always ignored:_test.go
430-
ignored-functions: "math.*,http.StatusText,make" # values always ignored: time.Time
417+
# List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
418+
checks:
419+
- argument
420+
- case
421+
- condition
422+
- operation
423+
- return
424+
- assign
425+
# List of numbers to exclude from analysis.
426+
# The numbers should be written as string.
427+
# Values always ignored: "1", "1.0", "0" and "0.0"
428+
ignored-numbers:
429+
- '0666'
430+
- '0755'
431+
- '42'
432+
# List of file patterns to exclude from analysis.
433+
# Values always ignored: `.+_test.go`
434+
ignored-files:
435+
- 'magic1_.*.go'
436+
# List of function patterns to exclude from analysis.
437+
# Values always ignored: `time.Time`
438+
ignored-functions:
439+
- 'math.*'
440+
- 'http.StatusText'
431441

432442
gomoddirectives:
433443
# Allow local `replace` directives. Default is false.

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ linters-settings:
3535
goimports:
3636
local-prefixes: github.com/golangci/golangci-lint
3737
gomnd:
38+
# TODO(ldez) must be rewritten after the v1.44.0 release.
3839
settings:
3940
mnd:
4041
# don't include the "operation" and "assign"

pkg/config/linters_settings.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ type GoLintSettings struct {
312312
}
313313

314314
type GoMndSettings struct {
315-
Settings map[string]map[string]interface{}
315+
Settings map[string]map[string]interface{} // Deprecated
316+
Checks []string `mapstructure:"checks"`
317+
IgnoredNumbers []string `mapstructure:"ignored-numbers"`
318+
IgnoredFiles []string `mapstructure:"ignored-files"`
319+
IgnoredFunctions []string `mapstructure:"ignored-functions"`
316320
}
317321

318322
type GoModDirectivesSettings struct {

pkg/golinters/gomnd.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,38 @@ import (
88
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
99
)
1010

11-
func NewGoMND(cfg *config.Config) *goanalysis.Linter {
12-
analyzers := []*analysis.Analyzer{
13-
mnd.Analyzer,
14-
}
15-
11+
func NewGoMND(settings *config.GoMndSettings) *goanalysis.Linter {
1612
var linterCfg map[string]map[string]interface{}
17-
if cfg != nil {
18-
linterCfg = cfg.LintersSettings.Gomnd.Settings
13+
14+
if settings != nil {
15+
// TODO(ldez) For compatibility only, must be drop in v2.
16+
if len(settings.Settings) > 0 {
17+
linterCfg = settings.Settings
18+
} else {
19+
cfg := make(map[string]interface{})
20+
if len(settings.Checks) > 0 {
21+
cfg["checks"] = settings.Checks
22+
}
23+
if len(settings.IgnoredNumbers) > 0 {
24+
cfg["ignored-numbers"] = settings.IgnoredNumbers
25+
}
26+
if len(settings.IgnoredFiles) > 0 {
27+
cfg["ignored-files"] = settings.IgnoredFiles
28+
}
29+
if len(settings.IgnoredFunctions) > 0 {
30+
cfg["ignored-functions"] = settings.IgnoredFunctions
31+
}
32+
33+
linterCfg = map[string]map[string]interface{}{
34+
"mnd": cfg,
35+
}
36+
}
1937
}
2038

2139
return goanalysis.NewLinter(
2240
"gomnd",
2341
"An analyzer to detect magic numbers.",
24-
analyzers,
42+
[]*analysis.Analyzer{mnd.Analyzer},
2543
linterCfg,
2644
).WithLoadMode(goanalysis.LoadModeSyntax)
2745
}

pkg/lint/lintersdb/manager.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
108108
var exhaustiveCfg *config.ExhaustiveSettings
109109
var exhaustiveStructCfg *config.ExhaustiveStructSettings
110110
var goModDirectivesCfg *config.GoModDirectivesSettings
111+
var goMndCfg *config.GoMndSettings
111112
var gosecCfg *config.GoSecSettings
112113
var gosimpleCfg *config.StaticCheckSettings
113114
var govetCfg *config.GovetSettings
@@ -138,6 +139,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
138139
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
139140
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
140141
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
142+
goMndCfg = &m.cfg.LintersSettings.Gomnd
141143
gosecCfg = &m.cfg.LintersSettings.Gosec
142144
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
143145
govetCfg = &m.cfg.LintersSettings.Govet
@@ -367,7 +369,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
367369
WithURL("https://github.com/golang/lint").
368370
Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"),
369371

370-
linter.NewConfig(golinters.NewGoMND(m.cfg)).
372+
linter.NewConfig(golinters.NewGoMND(goMndCfg)).
371373
WithSince("v1.22.0").
372374
WithPresets(linter.PresetStyle).
373375
WithURL("https://github.com/tommy-muehle/go-mnd"),

0 commit comments

Comments
 (0)