feat: replace gomnd by mnd ()

This commit is contained in:
Ludovic Fernandez 2024-03-28 21:23:44 +01:00 committed by GitHub
parent a7868b3e84
commit 288c847019
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 167 additions and 135 deletions

@ -780,38 +780,6 @@ linters-settings:
# Default: ""
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:
# Allow local `replace` directives.
# Default: false
@ -1368,6 +1336,38 @@ linters-settings:
# Default: ""
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:
# 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
@ -2553,7 +2553,6 @@ linters:
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
@ -2574,6 +2573,7 @@ linters:
- makezero
- mirror
- misspell
- mnd
- musttag
- nakedret
- nestif
@ -2666,7 +2666,6 @@ linters:
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
@ -2687,6 +2686,7 @@ linters:
- makezero
- mirror
- misspell
- mnd
- musttag
- nakedret
- nestif
@ -2736,6 +2736,7 @@ linters:
- ifshort # Deprecated
- interfacer # Deprecated
- maligned # Deprecated
- gomnd # Deprecated
- nosnakecase # Deprecated
- scopelint # Deprecated
- structcheck # Deprecated

@ -182,9 +182,12 @@ issues:
- path: pkg/golinters/gci.go
linters: [staticcheck]
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
- path: pkg/golinters/gomnd.go
- path: pkg/golinters/mnd.go
linters: [staticcheck]
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.
- path: pkg/golinters/gofumpt.go

@ -49,8 +49,8 @@ In the following example, all the reports from the linters (`linters`) that cont
issues:
exclude-rules:
- linters:
- gomnd
text: "mnd: Magic number: 9"
- mnd
text: "Magic number: 9"
```
In the following example, all the reports from the linters (`linters`) that originated from the source (`source`) are excluded:

@ -256,7 +256,6 @@
"goheader",
"goimports",
"golint",
"gomnd",
"gomoddirectives",
"gomodguard",
"goprintffuncname",
@ -280,6 +279,7 @@
"maligned",
"mirror",
"misspell",
"mnd",
"musttag",
"nakedret",
"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": {
"type": "object",
"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": {
"type": "object",
"additionalProperties": false,

@ -408,7 +408,7 @@ func (c *runCommand) setExitCodeIfIssuesFound(issues []result.Issue) {
}
func (c *runCommand) printDeprecatedLinterMessages(enabledLinters map[string]*linter.Config) {
if c.cfg.InternalCmdTest {
if c.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
return
}

@ -243,6 +243,7 @@ type LintersSettings struct {
MaintIdx MaintIdxSettings
Makezero MakezeroSettings
Misspell MisspellSettings
Mnd MndSettings
MustTag MustTagSettings
Nakedret NakedretSettings
Nestif NestifSettings
@ -549,11 +550,9 @@ type GoImportsSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"`
}
// Deprecated: use MndSettings.
type GoMndSettings struct {
Checks []string `mapstructure:"checks"`
IgnoredNumbers []string `mapstructure:"ignored-numbers"`
IgnoredFiles []string `mapstructure:"ignored-files"`
IgnoredFunctions []string `mapstructure:"ignored-functions"`
MndSettings `mapstructure:",squash"`
// Deprecated: use root level settings instead.
Settings map[string]map[string]any
@ -723,6 +722,13 @@ type NlreturnSettings struct {
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 {
RequireExplanation bool `mapstructure:"require-explanation"`
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

@ -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").
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)).
WithSince("v1.22.0").
WithPresets(linter.PresetStyle).
Deprecated("The linter has been renamed.", "v1.58.0", "mnd").
WithURL("https://github.com/tommy-muehle/go-mnd"),
linter.NewConfig(golinters.NewGoModDirectives(&cfg.LintersSettings.GoModDirectives)).

File diff suppressed because one or more lines are too long

@ -155,7 +155,7 @@
"Name": "golint"
},
{
"Name": "gomnd",
"Name": "mnd",
"Enabled": true
},
{

@ -1,4 +1,4 @@
//golangcitest:args -Egomnd
//golangcitest:args -Emnd
package testdata
import (