feat: replace gomnd by mnd (#4572)
This commit is contained in:
parent
a7868b3e84
commit
288c847019
@ -780,38 +780,6 @@ linters-settings:
|
|||||||
# Default: ""
|
# Default: ""
|
||||||
local-prefixes: github.com/org/project
|
local-prefixes: github.com/org/project
|
||||||
|
|
||||||
gomnd:
|
|
||||||
# List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
|
|
||||||
# Default: ["argument", "case", "condition", "operation", "return", "assign"]
|
|
||||||
checks:
|
|
||||||
- argument
|
|
||||||
- case
|
|
||||||
- condition
|
|
||||||
- operation
|
|
||||||
- return
|
|
||||||
- assign
|
|
||||||
# List of numbers to exclude from analysis.
|
|
||||||
# The numbers should be written as string.
|
|
||||||
# Values always ignored: "1", "1.0", "0" and "0.0"
|
|
||||||
# Default: []
|
|
||||||
ignored-numbers:
|
|
||||||
- '0666'
|
|
||||||
- '0755'
|
|
||||||
- '42'
|
|
||||||
# List of file patterns to exclude from analysis.
|
|
||||||
# Values always ignored: `.+_test.go`
|
|
||||||
# Default: []
|
|
||||||
ignored-files:
|
|
||||||
- 'magic1_.+\.go$'
|
|
||||||
# List of function patterns to exclude from analysis.
|
|
||||||
# Following functions are always ignored: `time.Date`,
|
|
||||||
# `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
|
|
||||||
# `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`.
|
|
||||||
# Default: []
|
|
||||||
ignored-functions:
|
|
||||||
- '^math\.'
|
|
||||||
- '^http\.StatusText$'
|
|
||||||
|
|
||||||
gomoddirectives:
|
gomoddirectives:
|
||||||
# Allow local `replace` directives.
|
# Allow local `replace` directives.
|
||||||
# Default: false
|
# Default: false
|
||||||
@ -1368,6 +1336,38 @@ linters-settings:
|
|||||||
# Default: ""
|
# Default: ""
|
||||||
mode: restricted
|
mode: restricted
|
||||||
|
|
||||||
|
mnd:
|
||||||
|
# List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
|
||||||
|
# Default: ["argument", "case", "condition", "operation", "return", "assign"]
|
||||||
|
checks:
|
||||||
|
- argument
|
||||||
|
- case
|
||||||
|
- condition
|
||||||
|
- operation
|
||||||
|
- return
|
||||||
|
- assign
|
||||||
|
# List of numbers to exclude from analysis.
|
||||||
|
# The numbers should be written as string.
|
||||||
|
# Values always ignored: "1", "1.0", "0" and "0.0"
|
||||||
|
# Default: []
|
||||||
|
ignored-numbers:
|
||||||
|
- '0666'
|
||||||
|
- '0755'
|
||||||
|
- '42'
|
||||||
|
# List of file patterns to exclude from analysis.
|
||||||
|
# Values always ignored: `.+_test.go`
|
||||||
|
# Default: []
|
||||||
|
ignored-files:
|
||||||
|
- 'magic1_.+\.go$'
|
||||||
|
# List of function patterns to exclude from analysis.
|
||||||
|
# Following functions are always ignored: `time.Date`,
|
||||||
|
# `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
|
||||||
|
# `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`.
|
||||||
|
# Default: []
|
||||||
|
ignored-functions:
|
||||||
|
- '^math\.'
|
||||||
|
- '^http\.StatusText$'
|
||||||
|
|
||||||
musttag:
|
musttag:
|
||||||
# A set of custom functions to check in addition to the builtin ones.
|
# A set of custom functions to check in addition to the builtin ones.
|
||||||
# Default: json, xml, gopkg.in/yaml.v3, BurntSushi/toml, mitchellh/mapstructure, jmoiron/sqlx
|
# Default: json, xml, gopkg.in/yaml.v3, BurntSushi/toml, mitchellh/mapstructure, jmoiron/sqlx
|
||||||
@ -2553,7 +2553,6 @@ linters:
|
|||||||
- gofumpt
|
- gofumpt
|
||||||
- goheader
|
- goheader
|
||||||
- goimports
|
- goimports
|
||||||
- gomnd
|
|
||||||
- gomoddirectives
|
- gomoddirectives
|
||||||
- gomodguard
|
- gomodguard
|
||||||
- goprintffuncname
|
- goprintffuncname
|
||||||
@ -2574,6 +2573,7 @@ linters:
|
|||||||
- makezero
|
- makezero
|
||||||
- mirror
|
- mirror
|
||||||
- misspell
|
- misspell
|
||||||
|
- mnd
|
||||||
- musttag
|
- musttag
|
||||||
- nakedret
|
- nakedret
|
||||||
- nestif
|
- nestif
|
||||||
@ -2666,7 +2666,6 @@ linters:
|
|||||||
- gofumpt
|
- gofumpt
|
||||||
- goheader
|
- goheader
|
||||||
- goimports
|
- goimports
|
||||||
- gomnd
|
|
||||||
- gomoddirectives
|
- gomoddirectives
|
||||||
- gomodguard
|
- gomodguard
|
||||||
- goprintffuncname
|
- goprintffuncname
|
||||||
@ -2687,6 +2686,7 @@ linters:
|
|||||||
- makezero
|
- makezero
|
||||||
- mirror
|
- mirror
|
||||||
- misspell
|
- misspell
|
||||||
|
- mnd
|
||||||
- musttag
|
- musttag
|
||||||
- nakedret
|
- nakedret
|
||||||
- nestif
|
- nestif
|
||||||
@ -2736,6 +2736,7 @@ linters:
|
|||||||
- ifshort # Deprecated
|
- ifshort # Deprecated
|
||||||
- interfacer # Deprecated
|
- interfacer # Deprecated
|
||||||
- maligned # Deprecated
|
- maligned # Deprecated
|
||||||
|
- gomnd # Deprecated
|
||||||
- nosnakecase # Deprecated
|
- nosnakecase # Deprecated
|
||||||
- scopelint # Deprecated
|
- scopelint # Deprecated
|
||||||
- structcheck # Deprecated
|
- structcheck # Deprecated
|
||||||
|
@ -182,9 +182,12 @@ issues:
|
|||||||
- path: pkg/golinters/gci.go
|
- path: pkg/golinters/gci.go
|
||||||
linters: [staticcheck]
|
linters: [staticcheck]
|
||||||
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
|
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
|
||||||
- path: pkg/golinters/gomnd.go
|
- path: pkg/golinters/mnd.go
|
||||||
linters: [staticcheck]
|
linters: [staticcheck]
|
||||||
text: "SA1019: settings.Settings is deprecated: use root level settings instead."
|
text: "SA1019: settings.Settings is deprecated: use root level settings instead."
|
||||||
|
- path: pkg/golinters/mnd.go
|
||||||
|
linters: [staticcheck]
|
||||||
|
text: "SA1019: config.GoMndSettings is deprecated: use MndSettings."
|
||||||
|
|
||||||
# Related to `run.go`, it cannot be removed.
|
# Related to `run.go`, it cannot be removed.
|
||||||
- path: pkg/golinters/gofumpt.go
|
- path: pkg/golinters/gofumpt.go
|
||||||
|
@ -49,8 +49,8 @@ In the following example, all the reports from the linters (`linters`) that cont
|
|||||||
issues:
|
issues:
|
||||||
exclude-rules:
|
exclude-rules:
|
||||||
- linters:
|
- linters:
|
||||||
- gomnd
|
- mnd
|
||||||
text: "mnd: Magic number: 9"
|
text: "Magic number: 9"
|
||||||
```
|
```
|
||||||
|
|
||||||
In the following example, all the reports from the linters (`linters`) that originated from the source (`source`) are excluded:
|
In the following example, all the reports from the linters (`linters`) that originated from the source (`source`) are excluded:
|
||||||
|
@ -256,7 +256,6 @@
|
|||||||
"goheader",
|
"goheader",
|
||||||
"goimports",
|
"goimports",
|
||||||
"golint",
|
"golint",
|
||||||
"gomnd",
|
|
||||||
"gomoddirectives",
|
"gomoddirectives",
|
||||||
"gomodguard",
|
"gomodguard",
|
||||||
"goprintffuncname",
|
"goprintffuncname",
|
||||||
@ -280,6 +279,7 @@
|
|||||||
"maligned",
|
"maligned",
|
||||||
"mirror",
|
"mirror",
|
||||||
"misspell",
|
"misspell",
|
||||||
|
"mnd",
|
||||||
"musttag",
|
"musttag",
|
||||||
"nakedret",
|
"nakedret",
|
||||||
"nestif",
|
"nestif",
|
||||||
@ -1408,50 +1408,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"gomnd": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"ignored-files": {
|
|
||||||
"description": "List of file patterns to exclude from analysis.",
|
|
||||||
"examples": [["magic1_.*.go"]],
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"ignored-functions": {
|
|
||||||
"description": "Comma-separated list of function patterns to exclude from the analysis.",
|
|
||||||
"examples": [["math.*", "http.StatusText", "make"]],
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"ignored-numbers": {
|
|
||||||
"description": "List of numbers to exclude from analysis.",
|
|
||||||
"examples": [["1000", "1234_567_890", "3.14159264"]],
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"checks": {
|
|
||||||
"description": "The list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"enum": [
|
|
||||||
"argument",
|
|
||||||
"case",
|
|
||||||
"condition",
|
|
||||||
"operation",
|
|
||||||
"return",
|
|
||||||
"assign"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"gomoddirectives": {
|
"gomoddirectives": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
@ -2041,6 +1997,50 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"mnd": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"ignored-files": {
|
||||||
|
"description": "List of file patterns to exclude from analysis.",
|
||||||
|
"examples": [["magic1_.*.go"]],
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ignored-functions": {
|
||||||
|
"description": "Comma-separated list of function patterns to exclude from the analysis.",
|
||||||
|
"examples": [["math.*", "http.StatusText", "make"]],
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ignored-numbers": {
|
||||||
|
"description": "List of numbers to exclude from analysis.",
|
||||||
|
"examples": [["1000", "1234_567_890", "3.14159264"]],
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"checks": {
|
||||||
|
"description": "The list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"enum": [
|
||||||
|
"argument",
|
||||||
|
"case",
|
||||||
|
"condition",
|
||||||
|
"operation",
|
||||||
|
"return",
|
||||||
|
"assign"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"nolintlint": {
|
"nolintlint": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
@ -408,7 +408,7 @@ func (c *runCommand) setExitCodeIfIssuesFound(issues []result.Issue) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *runCommand) printDeprecatedLinterMessages(enabledLinters map[string]*linter.Config) {
|
func (c *runCommand) printDeprecatedLinterMessages(enabledLinters map[string]*linter.Config) {
|
||||||
if c.cfg.InternalCmdTest {
|
if c.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,6 +243,7 @@ type LintersSettings struct {
|
|||||||
MaintIdx MaintIdxSettings
|
MaintIdx MaintIdxSettings
|
||||||
Makezero MakezeroSettings
|
Makezero MakezeroSettings
|
||||||
Misspell MisspellSettings
|
Misspell MisspellSettings
|
||||||
|
Mnd MndSettings
|
||||||
MustTag MustTagSettings
|
MustTag MustTagSettings
|
||||||
Nakedret NakedretSettings
|
Nakedret NakedretSettings
|
||||||
Nestif NestifSettings
|
Nestif NestifSettings
|
||||||
@ -549,11 +550,9 @@ type GoImportsSettings struct {
|
|||||||
LocalPrefixes string `mapstructure:"local-prefixes"`
|
LocalPrefixes string `mapstructure:"local-prefixes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use MndSettings.
|
||||||
type GoMndSettings struct {
|
type GoMndSettings struct {
|
||||||
Checks []string `mapstructure:"checks"`
|
MndSettings `mapstructure:",squash"`
|
||||||
IgnoredNumbers []string `mapstructure:"ignored-numbers"`
|
|
||||||
IgnoredFiles []string `mapstructure:"ignored-files"`
|
|
||||||
IgnoredFunctions []string `mapstructure:"ignored-functions"`
|
|
||||||
|
|
||||||
// Deprecated: use root level settings instead.
|
// Deprecated: use root level settings instead.
|
||||||
Settings map[string]map[string]any
|
Settings map[string]map[string]any
|
||||||
@ -723,6 +722,13 @@ type NlreturnSettings struct {
|
|||||||
BlockSize int `mapstructure:"block-size"`
|
BlockSize int `mapstructure:"block-size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MndSettings struct {
|
||||||
|
Checks []string `mapstructure:"checks"`
|
||||||
|
IgnoredNumbers []string `mapstructure:"ignored-numbers"`
|
||||||
|
IgnoredFiles []string `mapstructure:"ignored-files"`
|
||||||
|
IgnoredFunctions []string `mapstructure:"ignored-functions"`
|
||||||
|
}
|
||||||
|
|
||||||
type NoLintLintSettings struct {
|
type NoLintLintSettings struct {
|
||||||
RequireExplanation bool `mapstructure:"require-explanation"`
|
RequireExplanation bool `mapstructure:"require-explanation"`
|
||||||
RequireSpecific bool `mapstructure:"require-specific"`
|
RequireSpecific bool `mapstructure:"require-specific"`
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
package golinters
|
|
||||||
|
|
||||||
import (
|
|
||||||
mnd "github.com/tommy-muehle/go-mnd/v2"
|
|
||||||
"golang.org/x/tools/go/analysis"
|
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/config"
|
|
||||||
"github.com/golangci/golangci-lint/pkg/goanalysis"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewGoMND(settings *config.GoMndSettings) *goanalysis.Linter {
|
|
||||||
var linterCfg map[string]map[string]any
|
|
||||||
|
|
||||||
if settings != nil {
|
|
||||||
// TODO(ldez) For compatibility only, must be drop in v2.
|
|
||||||
if len(settings.Settings) > 0 {
|
|
||||||
linterCfg = settings.Settings
|
|
||||||
} else {
|
|
||||||
cfg := make(map[string]any)
|
|
||||||
if len(settings.Checks) > 0 {
|
|
||||||
cfg["checks"] = settings.Checks
|
|
||||||
}
|
|
||||||
if len(settings.IgnoredNumbers) > 0 {
|
|
||||||
cfg["ignored-numbers"] = settings.IgnoredNumbers
|
|
||||||
}
|
|
||||||
if len(settings.IgnoredFiles) > 0 {
|
|
||||||
cfg["ignored-files"] = settings.IgnoredFiles
|
|
||||||
}
|
|
||||||
if len(settings.IgnoredFunctions) > 0 {
|
|
||||||
cfg["ignored-functions"] = settings.IgnoredFunctions
|
|
||||||
}
|
|
||||||
|
|
||||||
linterCfg = map[string]map[string]any{
|
|
||||||
"mnd": cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return goanalysis.NewLinter(
|
|
||||||
"gomnd",
|
|
||||||
"An analyzer to detect magic numbers.",
|
|
||||||
[]*analysis.Analyzer{mnd.Analyzer},
|
|
||||||
linterCfg,
|
|
||||||
).WithLoadMode(goanalysis.LoadModeSyntax)
|
|
||||||
}
|
|
61
pkg/golinters/mnd.go
Normal file
61
pkg/golinters/mnd.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package golinters
|
||||||
|
|
||||||
|
import (
|
||||||
|
mnd "github.com/tommy-muehle/go-mnd/v2"
|
||||||
|
"golang.org/x/tools/go/analysis"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
|
"github.com/golangci/golangci-lint/pkg/goanalysis"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMND(settings *config.MndSettings) *goanalysis.Linter {
|
||||||
|
return newMND(mnd.Analyzer, settings, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGoMND(settings *config.GoMndSettings) *goanalysis.Linter {
|
||||||
|
// shallow copy because mnd.Analyzer is a global variable.
|
||||||
|
a := new(analysis.Analyzer)
|
||||||
|
*a = *mnd.Analyzer
|
||||||
|
|
||||||
|
// Used to force the analyzer name to use the same name as the linter.
|
||||||
|
// This is required to avoid displaying the analyzer name inside the issue text.
|
||||||
|
a.Name = "gomnd"
|
||||||
|
|
||||||
|
var linterCfg map[string]map[string]any
|
||||||
|
|
||||||
|
if settings != nil && len(settings.Settings) > 0 {
|
||||||
|
// Convert deprecated setting.
|
||||||
|
linterCfg = settings.Settings
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMND(a, &settings.MndSettings, linterCfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMND(a *analysis.Analyzer, settings *config.MndSettings, linterCfg map[string]map[string]any) *goanalysis.Linter {
|
||||||
|
if len(linterCfg) == 0 && settings != nil {
|
||||||
|
cfg := make(map[string]any)
|
||||||
|
if len(settings.Checks) > 0 {
|
||||||
|
cfg["checks"] = settings.Checks
|
||||||
|
}
|
||||||
|
if len(settings.IgnoredNumbers) > 0 {
|
||||||
|
cfg["ignored-numbers"] = settings.IgnoredNumbers
|
||||||
|
}
|
||||||
|
if len(settings.IgnoredFiles) > 0 {
|
||||||
|
cfg["ignored-files"] = settings.IgnoredFiles
|
||||||
|
}
|
||||||
|
if len(settings.IgnoredFunctions) > 0 {
|
||||||
|
cfg["ignored-functions"] = settings.IgnoredFunctions
|
||||||
|
}
|
||||||
|
|
||||||
|
linterCfg = map[string]map[string]any{
|
||||||
|
a.Name: cfg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return goanalysis.NewLinter(
|
||||||
|
a.Name,
|
||||||
|
"An analyzer to detect magic numbers.",
|
||||||
|
[]*analysis.Analyzer{a},
|
||||||
|
linterCfg,
|
||||||
|
).WithLoadMode(goanalysis.LoadModeSyntax)
|
||||||
|
}
|
@ -290,9 +290,15 @@ func (b LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
|
|||||||
WithURL("https://github.com/golang/lint").
|
WithURL("https://github.com/golang/lint").
|
||||||
Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"),
|
Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"),
|
||||||
|
|
||||||
|
linter.NewConfig(golinters.NewMND(&cfg.LintersSettings.Mnd)).
|
||||||
|
WithSince("v1.22.0").
|
||||||
|
WithPresets(linter.PresetStyle).
|
||||||
|
WithURL("https://github.com/tommy-muehle/go-mnd"),
|
||||||
|
|
||||||
linter.NewConfig(golinters.NewGoMND(&cfg.LintersSettings.Gomnd)).
|
linter.NewConfig(golinters.NewGoMND(&cfg.LintersSettings.Gomnd)).
|
||||||
WithSince("v1.22.0").
|
WithSince("v1.22.0").
|
||||||
WithPresets(linter.PresetStyle).
|
WithPresets(linter.PresetStyle).
|
||||||
|
Deprecated("The linter has been renamed.", "v1.58.0", "mnd").
|
||||||
WithURL("https://github.com/tommy-muehle/go-mnd"),
|
WithURL("https://github.com/tommy-muehle/go-mnd"),
|
||||||
|
|
||||||
linter.NewConfig(golinters.NewGoModDirectives(&cfg.LintersSettings.GoModDirectives)).
|
linter.NewConfig(golinters.NewGoModDirectives(&cfg.LintersSettings.GoModDirectives)).
|
||||||
|
2
pkg/printers/testdata/golden-json.json
vendored
2
pkg/printers/testdata/golden-json.json
vendored
File diff suppressed because one or more lines are too long
2
pkg/printers/testdata/in-report-data.json
vendored
2
pkg/printers/testdata/in-report-data.json
vendored
@ -155,7 +155,7 @@
|
|||||||
"Name": "golint"
|
"Name": "golint"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Name": "gomnd",
|
"Name": "mnd",
|
||||||
"Enabled": true
|
"Enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//golangcitest:args -Egomnd
|
//golangcitest:args -Emnd
|
||||||
package testdata
|
package testdata
|
||||||
|
|
||||||
import (
|
import (
|
Loading…
x
Reference in New Issue
Block a user