diff --git a/pkg/fsutils/fsutils.go b/pkg/fsutils/fsutils.go index 715a3a4a..80bb9c5b 100644 --- a/pkg/fsutils/fsutils.go +++ b/pkg/fsutils/fsutils.go @@ -12,10 +12,12 @@ func IsDir(filename string) bool { return err == nil && fi.IsDir() } -var cachedWd string -var cachedWdError error -var getWdOnce sync.Once -var useCache = true +var ( + cachedWd string + cachedWdError error + getWdOnce sync.Once + useCache = true +) func UseWdCache(use bool) { useCache = use diff --git a/pkg/golinters/goanalysis/runner.go b/pkg/golinters/goanalysis/runner.go index a8660b61..8243c345 100644 --- a/pkg/golinters/goanalysis/runner.go +++ b/pkg/golinters/goanalysis/runner.go @@ -61,7 +61,8 @@ type runner struct { } func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loadGuard *load.Guard, - loadMode LoadMode, sw *timeutils.Stopwatch) *runner { + loadMode LoadMode, sw *timeutils.Stopwatch, +) *runner { return &runner{ prefix: prefix, log: logger, @@ -80,7 +81,8 @@ func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loa // singlechecker and the multi-analysis commands. // It returns the appropriate exit code. func (r *runner) run(analyzers []*analysis.Analyzer, initialPackages []*packages.Package) ([]Diagnostic, - []error, map[*analysis.Pass]*packages.Package) { + []error, map[*analysis.Pass]*packages.Package, +) { debugf("Analyzing %d packages on load mode %s", len(initialPackages), r.loadMode) defer r.pkgCache.Trim() @@ -116,7 +118,8 @@ func (r *runner) markAllActions(a *analysis.Analyzer, pkg *packages.Package, mar } func (r *runner) makeAction(a *analysis.Analyzer, pkg *packages.Package, - initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator) *action { + initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator, +) *action { k := actKey{a, pkg} act, ok := actions[k] if ok { @@ -150,7 +153,8 @@ func (r *runner) makeAction(a *analysis.Analyzer, pkg *packages.Package, } func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *packages.Package, - initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator) { + initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator, +) { // An analysis that consumes/produces facts // must run on the package's dependencies too. if len(a.FactTypes) == 0 { @@ -175,7 +179,8 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac //nolint:gocritic func (r *runner) prepareAnalysis(pkgs []*packages.Package, - analyzers []*analysis.Analyzer) (map[*packages.Package]bool, []*action, []*action) { + analyzers []*analysis.Analyzer, +) (map[*packages.Package]bool, []*action, []*action) { // Construct the action graph. // Each graph node (action) is one unit of analysis. diff --git a/pkg/golinters/goanalysis/runners.go b/pkg/golinters/goanalysis/runners.go index d5a9c157..c5a0b612 100644 --- a/pkg/golinters/goanalysis/runners.go +++ b/pkg/golinters/goanalysis/runners.go @@ -124,7 +124,8 @@ func getIssuesCacheKey(analyzers []*analysis.Analyzer) string { } func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.Package]bool, - issues []result.Issue, lintCtx *linter.Context, analyzers []*analysis.Analyzer) { + issues []result.Issue, lintCtx *linter.Context, analyzers []*analysis.Analyzer, +) { startedAt := time.Now() perPkgIssues := map[*packages.Package][]result.Issue{} for ind := range issues { @@ -185,7 +186,8 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages. //nolint:gocritic func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context, - analyzers []*analysis.Analyzer) ([]result.Issue, map[*packages.Package]bool) { + analyzers []*analysis.Analyzer, +) ([]result.Issue, map[*packages.Package]bool) { startedAt := time.Now() lintResKey := getIssuesCacheKey(analyzers) diff --git a/pkg/golinters/nolintlint/nolintlint.go b/pkg/golinters/nolintlint/nolintlint.go index bdfceb18..aadc7eb6 100644 --- a/pkg/golinters/nolintlint/nolintlint.go +++ b/pkg/golinters/nolintlint/nolintlint.go @@ -151,8 +151,10 @@ func NewLinter(needs Needs, excludes []string) (*Linter, error) { }, nil } -var leadingSpacePattern = regexp.MustCompile(`^//(\s*)`) -var trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`) +var ( + leadingSpacePattern = regexp.MustCompile(`^//(\s*)`) + trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`) +) //nolint:funlen,gocyclo func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) { diff --git a/pkg/golinters/testpackage.go b/pkg/golinters/testpackage.go index db1ead96..b8a867eb 100644 --- a/pkg/golinters/testpackage.go +++ b/pkg/golinters/testpackage.go @@ -11,7 +11,7 @@ import ( ) func NewTestpackage(cfg *config.TestpackageSettings) *goanalysis.Linter { - var a = testpackage.NewAnalyzer() + a := testpackage.NewAnalyzer() var settings map[string]map[string]any if cfg != nil { diff --git a/pkg/lint/load.go b/pkg/lint/load.go index 7f892e94..ffc0ac5d 100644 --- a/pkg/lint/load.go +++ b/pkg/lint/load.go @@ -36,7 +36,8 @@ type ContextLoader struct { } func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env, - lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard) *ContextLoader { + lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard, +) *ContextLoader { return &ContextLoader{ cfg: cfg, log: log, diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 13daa3b3..aa26ea08 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -34,7 +34,8 @@ type Runner struct { func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env, lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, - dbManager *lintersdb.Manager, lintCtx *linter.Context) (*Runner, error) { + dbManager *lintersdb.Manager, lintCtx *linter.Context, +) (*Runner, error) { // Beware that some processors need to add the path prefix when working with paths // because they get invoked before the path prefixer (exclude and severity rules) // or process other paths (skip files). @@ -129,7 +130,8 @@ func (r *Runner) Run(ctx context.Context, linters []*linter.Config) ([]result.Is } func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, - lc *linter.Config) (ret []result.Issue, err error) { + lc *linter.Config, +) (ret []result.Issue, err error) { defer func() { if panicData := recover(); panicData != nil { if pe, ok := panicData.(*errorutil.PanicError); ok { diff --git a/pkg/logutils/out.go b/pkg/logutils/out.go index 67c70dc8..ef137548 100644 --- a/pkg/logutils/out.go +++ b/pkg/logutils/out.go @@ -5,5 +5,7 @@ import ( colorable "github.com/mattn/go-colorable" ) -var StdOut = color.Output // https://github.com/golangci/golangci-lint/issues/14 -var StdErr = colorable.NewColorableStderr() +var ( + StdOut = color.Output // https://github.com/golangci/golangci-lint/issues/14 + StdErr = colorable.NewColorableStderr() +) diff --git a/pkg/printers/printer.go b/pkg/printers/printer.go index a42fda7e..ba2bd2d3 100644 --- a/pkg/printers/printer.go +++ b/pkg/printers/printer.go @@ -13,7 +13,7 @@ import ( "github.com/golangci/golangci-lint/pkg/result" ) -const defaultFileMode = 0644 +const defaultFileMode = 0o644 type issuePrinter interface { Print(issues []result.Issue) error diff --git a/pkg/result/processors/identifier_marker.go b/pkg/result/processors/identifier_marker.go index 6c971023..1975f6d0 100644 --- a/pkg/result/processors/identifier_marker.go +++ b/pkg/result/processors/identifier_marker.go @@ -31,27 +31,39 @@ var replacePatterns = []replacePattern{ {`^composites: (\S+) composite literal uses unkeyed fields$`, "composites: `${1}` composite literal uses unkeyed fields"}, // gosec - {`^(\S+): Blacklisted import (\S+): weak cryptographic primitive$`, - "${1}: Blacklisted import `${2}`: weak cryptographic primitive"}, + { + `^(\S+): Blacklisted import (\S+): weak cryptographic primitive$`, + "${1}: Blacklisted import `${2}`: weak cryptographic primitive", + }, {`^TLS InsecureSkipVerify set true.$`, "TLS `InsecureSkipVerify` set true."}, // gosimple {`should replace loop with (.*)$`, "should replace loop with `${1}`"}, - {`should use a simple channel send/receive instead of select with a single case`, - "should use a simple channel send/receive instead of `select` with a single case"}, - {`should omit comparison to bool constant, can be simplified to (.+)$`, - "should omit comparison to bool constant, can be simplified to `${1}`"}, + { + `should use a simple channel send/receive instead of select with a single case`, + "should use a simple channel send/receive instead of `select` with a single case", + }, + { + `should omit comparison to bool constant, can be simplified to (.+)$`, + "should omit comparison to bool constant, can be simplified to `${1}`", + }, {`should write (.+) instead of (.+)$`, "should write `${1}` instead of `${2}`"}, {`redundant return statement$`, "redundant `return` statement"}, - {`should replace this if statement with an unconditional strings.TrimPrefix`, - "should replace this `if` statement with an unconditional `strings.TrimPrefix`"}, + { + `should replace this if statement with an unconditional strings.TrimPrefix`, + "should replace this `if` statement with an unconditional `strings.TrimPrefix`", + }, // staticcheck {`this value of (\S+) is never used$`, "this value of `${1}` is never used"}, - {`should use time.Since instead of time.Now\(\).Sub$`, - "should use `time.Since` instead of `time.Now().Sub`"}, - {`should check returned error before deferring response.Close\(\)$`, - "should check returned error before deferring `response.Close()`"}, + { + `should use time.Since instead of time.Now\(\).Sub$`, + "should use `time.Since` instead of `time.Now().Sub`", + }, + { + `should check returned error before deferring response.Close\(\)$`, + "should check returned error before deferring `response.Close()`", + }, {`no value of type uint is less than 0$`, "no value of type `uint` is less than `0`"}, // unused @@ -59,26 +71,40 @@ var replacePatterns = []replacePattern{ // typecheck {`^unknown field (\S+) in struct literal$`, "unknown field `${1}` in struct literal"}, - {`^invalid operation: (\S+) \(variable of type (\S+)\) has no field or method (\S+)$`, - "invalid operation: `${1}` (variable of type `${2}`) has no field or method `${3}`"}, + { + `^invalid operation: (\S+) \(variable of type (\S+)\) has no field or method (\S+)$`, + "invalid operation: `${1}` (variable of type `${2}`) has no field or method `${3}`", + }, {`^undeclared name: (\S+)$`, "undeclared name: `${1}`"}, - {`^cannot use addr \(variable of type (\S+)\) as (\S+) value in argument to (\S+)$`, - "cannot use addr (variable of type `${1}`) as `${2}` value in argument to `${3}`"}, + { + `^cannot use addr \(variable of type (\S+)\) as (\S+) value in argument to (\S+)$`, + "cannot use addr (variable of type `${1}`) as `${2}` value in argument to `${3}`", + }, {`^other declaration of (\S+)$`, "other declaration of `${1}`"}, {`^(\S+) redeclared in this block$`, "`${1}` redeclared in this block"}, // golint - {`^exported (type|method|function|var|const) (\S+) should have comment or be unexported$`, - "exported ${1} `${2}` should have comment or be unexported"}, - {`^comment on exported (type|method|function|var|const) (\S+) should be of the form "(\S+) ..."$`, - "comment on exported ${1} `${2}` should be of the form `${3} ...`"}, + { + `^exported (type|method|function|var|const) (\S+) should have comment or be unexported$`, + "exported ${1} `${2}` should have comment or be unexported", + }, + { + `^comment on exported (type|method|function|var|const) (\S+) should be of the form "(\S+) ..."$`, + "comment on exported ${1} `${2}` should be of the form `${3} ...`", + }, {`^should replace (.+) with (.+)$`, "should replace `${1}` with `${2}`"}, - {`^if block ends with a return statement, so drop this else and outdent its block$`, - "`if` block ends with a `return` statement, so drop this `else` and outdent its block"}, - {`^(struct field|var|range var|const|type|(?:func|method|interface method) (?:parameter|result)) (\S+) should be (\S+)$`, - "${1} `${2}` should be `${3}`"}, - {`^don't use underscores in Go names; var (\S+) should be (\S+)$`, - "don't use underscores in Go names; var `${1}` should be `${2}`"}, + { + `^if block ends with a return statement, so drop this else and outdent its block$`, + "`if` block ends with a `return` statement, so drop this `else` and outdent its block", + }, + { + `^(struct field|var|range var|const|type|(?:func|method|interface method) (?:parameter|result)) (\S+) should be (\S+)$`, + "${1} `${2}` should be `${3}`", + }, + { + `^don't use underscores in Go names; var (\S+) should be (\S+)$`, + "don't use underscores in Go names; var `${1}` should be `${2}`", + }, } type IdentifierMarker struct { diff --git a/pkg/result/processors/identifier_marker_test.go b/pkg/result/processors/identifier_marker_test.go index 831d3758..bef3f06a 100644 --- a/pkg/result/processors/identifier_marker_test.go +++ b/pkg/result/processors/identifier_marker_test.go @@ -13,39 +13,61 @@ func TestIdentifierMarker(t *testing.T) { //nolint:lll cases := []struct{ in, out string }{ {"unknown field Address in struct literal", "unknown field `Address` in struct literal"}, - {"invalid operation: res (variable of type github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse) has no field or method Address", - "invalid operation: `res` (variable of type `github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse`) has no field or method `Address`"}, - {"should use a simple channel send/receive instead of select with a single case", - "should use a simple channel send/receive instead of `select` with a single case"}, + { + "invalid operation: res (variable of type github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse) has no field or method Address", + "invalid operation: `res` (variable of type `github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse`) has no field or method `Address`", + }, + { + "should use a simple channel send/receive instead of select with a single case", + "should use a simple channel send/receive instead of `select` with a single case", + }, {"var testInputs is unused", "var `testInputs` is unused"}, {"undeclared name: stateIDLabel", "undeclared name: `stateIDLabel`"}, - {"exported type Metrics should have comment or be unexported", - "exported type `Metrics` should have comment or be unexported"}, - {`comment on exported function NewMetrics should be of the form "NewMetrics ..."`, - "comment on exported function `NewMetrics` should be of the form `NewMetrics ...`"}, - {"cannot use addr (variable of type string) as github.com/iotexproject/iotex-core/pkg/keypair.PublicKey value in argument to action.FakeSeal", - "cannot use addr (variable of type `string`) as `github.com/iotexproject/iotex-core/pkg/keypair.PublicKey` value in argument to `action.FakeSeal`"}, + { + "exported type Metrics should have comment or be unexported", + "exported type `Metrics` should have comment or be unexported", + }, + { + `comment on exported function NewMetrics should be of the form "NewMetrics ..."`, + "comment on exported function `NewMetrics` should be of the form `NewMetrics ...`", + }, + { + "cannot use addr (variable of type string) as github.com/iotexproject/iotex-core/pkg/keypair.PublicKey value in argument to action.FakeSeal", + "cannot use addr (variable of type `string`) as `github.com/iotexproject/iotex-core/pkg/keypair.PublicKey` value in argument to `action.FakeSeal`", + }, {"other declaration of out", "other declaration of `out`"}, {"should check returned error before deferring response.Close()", "should check returned error before deferring `response.Close()`"}, {"should use time.Since instead of time.Now().Sub", "should use `time.Since` instead of `time.Now().Sub`"}, {"TestFibZeroCount redeclared in this block", "`TestFibZeroCount` redeclared in this block"}, {"should replace i += 1 with i++", "should replace `i += 1` with `i++`"}, {"createEntry - result err is always nil", "`createEntry` - result `err` is always `nil`"}, - {"should omit comparison to bool constant, can be simplified to !projectIntegration.Model.Storage", - "should omit comparison to bool constant, can be simplified to `!projectIntegration.Model.Storage`"}, - {"if block ends with a return statement, so drop this else and outdent its block", - "`if` block ends with a `return` statement, so drop this `else` and outdent its block"}, - {"should write pupData := ms.m[pupID] instead of pupData, _ := ms.m[pupID]", - "should write `pupData := ms.m[pupID]` instead of `pupData, _ := ms.m[pupID]`"}, + { + "should omit comparison to bool constant, can be simplified to !projectIntegration.Model.Storage", + "should omit comparison to bool constant, can be simplified to `!projectIntegration.Model.Storage`", + }, + { + "if block ends with a return statement, so drop this else and outdent its block", + "`if` block ends with a `return` statement, so drop this `else` and outdent its block", + }, + { + "should write pupData := ms.m[pupID] instead of pupData, _ := ms.m[pupID]", + "should write `pupData := ms.m[pupID]` instead of `pupData, _ := ms.m[pupID]`", + }, {"no value of type uint is less than 0", "no value of type `uint` is less than `0`"}, {"redundant return statement", "redundant `return` statement"}, {"struct field Id should be ID", "struct field `Id` should be `ID`"}, - {"don't use underscores in Go names; var Go_lint should be GoLint", - "don't use underscores in Go names; var `Go_lint` should be `GoLint`"}, - {"G501: Blacklisted import crypto/md5: weak cryptographic primitive", - "G501: Blacklisted import `crypto/md5`: weak cryptographic primitive"}, - {"S1017: should replace this if statement with an unconditional strings.TrimPrefix", - "S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix`"}, + { + "don't use underscores in Go names; var Go_lint should be GoLint", + "don't use underscores in Go names; var `Go_lint` should be `GoLint`", + }, + { + "G501: Blacklisted import crypto/md5: weak cryptographic primitive", + "G501: Blacklisted import `crypto/md5`: weak cryptographic primitive", + }, + { + "S1017: should replace this if statement with an unconditional strings.TrimPrefix", + "S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix`", + }, } p := NewIdentifierMarker() diff --git a/pkg/result/processors/max_per_file_from_linter.go b/pkg/result/processors/max_per_file_from_linter.go index 51939fce..372f40cc 100644 --- a/pkg/result/processors/max_per_file_from_linter.go +++ b/pkg/result/processors/max_per_file_from_linter.go @@ -5,8 +5,10 @@ import ( "github.com/golangci/golangci-lint/pkg/result" ) -type linterToCountMap map[string]int -type fileToLinterToCountMap map[string]linterToCountMap +type ( + linterToCountMap map[string]int + fileToLinterToCountMap map[string]linterToCountMap +) type MaxPerFileFromLinter struct { flc fileToLinterToCountMap diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index 189430bb..48be85b6 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -18,8 +18,10 @@ import ( "github.com/golangci/golangci-lint/pkg/result" ) -var nolintDebugf = logutils.Debug(logutils.DebugKeyNolint) -var nolintRe = regexp.MustCompile(`^nolint( |:|$)`) +var ( + nolintDebugf = logutils.Debug(logutils.DebugKeyNolint) + nolintRe = regexp.MustCompile(`^nolint( |:|$)`) +) type ignoredRange struct { linters []string diff --git a/pkg/result/processors/nolint_test.go b/pkg/result/processors/nolint_test.go index d6fb56a8..bca041da 100644 --- a/pkg/result/processors/nolint_test.go +++ b/pkg/result/processors/nolint_test.go @@ -192,7 +192,7 @@ func TestNolintAliases(t *testing.T) { } func TestIgnoredRangeMatches(t *testing.T) { - var testcases = []struct { + testcases := []struct { doc string issue result.Issue linters []string diff --git a/pkg/result/processors/uniq_by_line.go b/pkg/result/processors/uniq_by_line.go index a7ca4467..aad1e019 100644 --- a/pkg/result/processors/uniq_by_line.go +++ b/pkg/result/processors/uniq_by_line.go @@ -5,8 +5,10 @@ import ( "github.com/golangci/golangci-lint/pkg/result" ) -type lineToCount map[int]int -type fileToLineToCount map[string]lineToCount +type ( + lineToCount map[int]int + fileToLineToCount map[string]lineToCount +) type UniqByLine struct { flc fileToLineToCount diff --git a/scripts/website/expand_templates/linters.go b/scripts/website/expand_templates/linters.go index 27dcc07b..3c7a42db 100644 --- a/scripts/website/expand_templates/linters.go +++ b/scripts/website/expand_templates/linters.go @@ -256,7 +256,7 @@ func getLintersSettingSections(node, nextNode *yaml.Node) (string, error) { return "", err } - var lintersDesc = make(map[string]string) + lintersDesc := make(map[string]string) for _, lc := range linters { if lc.Internal { continue