diff --git a/.golangci.example.yml b/.golangci.example.yml index 2dc024f6b73e..451e52856a45 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -67,6 +67,12 @@ output: # all available settings of specific linters linters-settings: + dogsled: + # checks assignments with too many blank identifiers; default is 2 + max-blank-identifiers: 2 + dupl: + # tokens count to trigger issue, 150 by default + threshold: 100 errcheck: # report about not checking of errors in type assetions: `a := b.(MyStruct)`; # default is false: such cases aren't reported by default. @@ -84,11 +90,64 @@ linters-settings: # path to a file containing a list of functions to exclude from checking # see https://github.com/kisielk/errcheck#excluding-functions for details exclude: /path/to/file.txt - funlen: lines: 60 statements: 40 + gocognit: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + goconst: + # minimal length of string constant, 3 by default + min-len: 3 + # minimal occurrences count to trigger, 3 by default + min-occurrences: 3 + gocritic: + # Which checks should be enabled; can't be combined with 'disabled-checks'; + # See https://go-critic.github.io/overview#checks-overview + # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` + # By default list of stable checks is used. + enabled-checks: + - rangeValCopy + + # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty + disabled-checks: + - regexpMust + + # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. + # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". + enabled-tags: + - performance + settings: # settings passed to gocritic + captLocal: # must be valid enabled check name + paramsOnly: true + rangeValCopy: + sizeThreshold: 32 + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + godox: + # report any comments starting with keywords, this is useful for TODO or FIXME comments that + # might be left in the code accidentally and should be resolved before merging + keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting + - NOTE + - OPTIMIZE # marks code that should be optimized before merging + - HACK # marks hack-arounds that should be removed before merging + gofmt: + # simplify code: gofmt with `-s` option, true by default + simplify: true + goimports: + # put imports beginning with prefix after 3rd-party packages; + # it's a comma-separated list of prefixes + local-prefixes: github.com/org/project + golint: + # minimal confidence for issues, default is 0.8 + min-confidence: 0.8 + gomnd: + settings: + mnd: + # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. + checks: argument,case,condition,operation,return,assign govet: # report about shadowed variables check-shadowing: true @@ -109,33 +168,6 @@ linters-settings: disable: - shadow disable-all: false - golint: - # minimal confidence for issues, default is 0.8 - min-confidence: 0.8 - gofmt: - # simplify code: gofmt with `-s` option, true by default - simplify: true - goimports: - # put imports beginning with prefix after 3rd-party packages; - # it's a comma-separated list of prefixes - local-prefixes: github.com/org/project - gocyclo: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - gocognit: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - maligned: - # print struct with more effective memory layout or not, false by default - suggest-new: true - dupl: - # tokens count to trigger issue, 150 by default - threshold: 100 - goconst: - # minimal length of string constant, 3 by default - min-len: 3 - # minimal occurrences count to trigger, 3 by default - min-occurrences: 3 depguard: list-type: blacklist include-go-root: false @@ -144,6 +176,15 @@ linters-settings: packages-with-error-message: # specify an error message to output when a blacklisted package is used - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" + lll: + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option + line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. @@ -151,27 +192,6 @@ linters-settings: locale: US ignore-words: - someword - lll: - # max line length, lines longer will be reported. Default is 120. - # '\t' is counted as 1 character by default, and can be changed with the tab-width option - line-length: 120 - # tab width in spaces. Default to 1. - tab-width: 1 - rowserrcheck: - packages: - - github.com/jmoiron/sqlx - unused: - # treat code as a program (not a library) and report unused exported identifiers; default is false. - # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find funcs usages. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false - unparam: - # Inspect exported functions, default is false. Set to true if no external program/library imports your code. - # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find external interfaces. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false nakedret: # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 max-func-lines: 30 @@ -184,39 +204,21 @@ linters-settings: simple: true range-loops: true # Report preallocation suggestions on range loops, true by default for-loops: false # Report preallocation suggestions on for loops, false by default - gocritic: - # Which checks should be enabled; can't be combined with 'disabled-checks'; - # See https://go-critic.github.io/overview#checks-overview - # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` - # By default list of stable checks is used. - enabled-checks: - - rangeValCopy - - # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty - disabled-checks: - - regexpMust - - # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. - # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". - enabled-tags: - - performance - - settings: # settings passed to gocritic - captLocal: # must be valid enabled check name - paramsOnly: true - rangeValCopy: - sizeThreshold: 32 - godox: - # report any comments starting with keywords, this is useful for TODO or FIXME comments that - # might be left in the code accidentally and should be resolved before merging - keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting - - NOTE - - OPTIMIZE # marks code that should be optimized before merging - - HACK # marks hack-arounds that should be removed before merging - dogsled: - # checks assignments with too many blank identifiers; default is 2 - max-blank-identifiers: 2 - + rowserrcheck: + packages: + - github.com/jmoiron/sqlx + unparam: + # Inspect exported functions, default is false. Set to true if no external program/library imports your code. + # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find external interfaces. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false + unused: + # treat code as a program (not a library) and report unused exported identifiers; default is false. + # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find funcs usages. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false whitespace: multi-if: false # Enforces newlines (or comments) after every multi-line if statement multi-func: false # Enforces newlines (or comments) after every multi-line function signature diff --git a/.golangci.yml b/.golangci.yml index eee2cc4c7724..f5a389be4936 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,24 +1,4 @@ linters-settings: - govet: - check-shadowing: true - settings: - printf: - funcs: - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf - golint: - min-confidence: 0 - gocyclo: - min-complexity: 15 - maligned: - suggest-new: true - dupl: - threshold: 100 - goconst: - min-len: 2 - min-occurrences: 2 depguard: list-type: blacklist packages: @@ -27,12 +7,14 @@ linters-settings: - github.com/sirupsen/logrus packages-with-error-message: - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" - misspell: - locale: US - lll: - line-length: 140 - goimports: - local-prefixes: github.com/golangci/golangci-lint + dupl: + threshold: 100 + funlen: + lines: 100 + statements: 50 + goconst: + min-len: 2 + min-occurrences: 2 gocritic: enabled-tags: - diagnostic @@ -46,16 +28,38 @@ linters-settings: - octalLiteral - whyNoLint - wrapperFunc - funlen: - lines: 100 - statements: 50 + gocyclo: + min-complexity: 15 + goimports: + local-prefixes: github.com/golangci/golangci-lint + golint: + min-confidence: 0 + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: argument,case,condition,return + govet: + check-shadowing: true + settings: + printf: + funcs: + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US linters: # please, do not use `enable-all`: it's deprecated and will be removed soon. # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint disable-all: true enable: - # - rowserrcheck - bodyclose - deadcode - depguard @@ -70,6 +74,8 @@ linters: - gofmt - goimports - golint +# - gomnd TODO: enable it with release > v1.23.0 + - goprintffuncname - gosec - gosimple - govet @@ -78,6 +84,7 @@ linters: - lll - misspell - nakedret + - rowserrcheck - scopelint - staticcheck - structcheck @@ -96,6 +103,13 @@ linters: # - maligned # - prealloc +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd + run: skip-dirs: - test/testdata_etc @@ -106,6 +120,6 @@ run: # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration service: - golangci-lint-version: 1.22.x # use the fixed version to not introduce new linters unexpectedly + golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly prepare: - echo "here I can run custom commands, but no preparation needed for this repo" diff --git a/README.md b/README.md index 23565d8996f7..bf8d47546570 100644 --- a/README.md +++ b/README.md @@ -676,6 +676,12 @@ output: # all available settings of specific linters linters-settings: + dogsled: + # checks assignments with too many blank identifiers; default is 2 + max-blank-identifiers: 2 + dupl: + # tokens count to trigger issue, 150 by default + threshold: 100 errcheck: # report about not checking of errors in type assetions: `a := b.(MyStruct)`; # default is false: such cases aren't reported by default. @@ -693,11 +699,64 @@ linters-settings: # path to a file containing a list of functions to exclude from checking # see https://github.com/kisielk/errcheck#excluding-functions for details exclude: /path/to/file.txt - funlen: lines: 60 statements: 40 + gocognit: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + goconst: + # minimal length of string constant, 3 by default + min-len: 3 + # minimal occurrences count to trigger, 3 by default + min-occurrences: 3 + gocritic: + # Which checks should be enabled; can't be combined with 'disabled-checks'; + # See https://go-critic.github.io/overview#checks-overview + # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` + # By default list of stable checks is used. + enabled-checks: + - rangeValCopy + # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty + disabled-checks: + - regexpMust + + # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. + # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". + enabled-tags: + - performance + + settings: # settings passed to gocritic + captLocal: # must be valid enabled check name + paramsOnly: true + rangeValCopy: + sizeThreshold: 32 + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + godox: + # report any comments starting with keywords, this is useful for TODO or FIXME comments that + # might be left in the code accidentally and should be resolved before merging + keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting + - NOTE + - OPTIMIZE # marks code that should be optimized before merging + - HACK # marks hack-arounds that should be removed before merging + gofmt: + # simplify code: gofmt with `-s` option, true by default + simplify: true + goimports: + # put imports beginning with prefix after 3rd-party packages; + # it's a comma-separated list of prefixes + local-prefixes: github.com/org/project + golint: + # minimal confidence for issues, default is 0.8 + min-confidence: 0.8 + gomnd: + settings: + mnd: + # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. + checks: argument,case,condition,operation,return,assign govet: # report about shadowed variables check-shadowing: true @@ -718,33 +777,6 @@ linters-settings: disable: - shadow disable-all: false - golint: - # minimal confidence for issues, default is 0.8 - min-confidence: 0.8 - gofmt: - # simplify code: gofmt with `-s` option, true by default - simplify: true - goimports: - # put imports beginning with prefix after 3rd-party packages; - # it's a comma-separated list of prefixes - local-prefixes: github.com/org/project - gocyclo: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - gocognit: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - maligned: - # print struct with more effective memory layout or not, false by default - suggest-new: true - dupl: - # tokens count to trigger issue, 150 by default - threshold: 100 - goconst: - # minimal length of string constant, 3 by default - min-len: 3 - # minimal occurrences count to trigger, 3 by default - min-occurrences: 3 depguard: list-type: blacklist include-go-root: false @@ -753,6 +785,15 @@ linters-settings: packages-with-error-message: # specify an error message to output when a blacklisted package is used - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" + lll: + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option + line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. @@ -760,27 +801,6 @@ linters-settings: locale: US ignore-words: - someword - lll: - # max line length, lines longer will be reported. Default is 120. - # '\t' is counted as 1 character by default, and can be changed with the tab-width option - line-length: 120 - # tab width in spaces. Default to 1. - tab-width: 1 - rowserrcheck: - packages: - - github.com/jmoiron/sqlx - unused: - # treat code as a program (not a library) and report unused exported identifiers; default is false. - # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find funcs usages. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false - unparam: - # Inspect exported functions, default is false. Set to true if no external program/library imports your code. - # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find external interfaces. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false nakedret: # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 max-func-lines: 30 @@ -793,39 +813,21 @@ linters-settings: simple: true range-loops: true # Report preallocation suggestions on range loops, true by default for-loops: false # Report preallocation suggestions on for loops, false by default - gocritic: - # Which checks should be enabled; can't be combined with 'disabled-checks'; - # See https://go-critic.github.io/overview#checks-overview - # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` - # By default list of stable checks is used. - enabled-checks: - - rangeValCopy - - # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty - disabled-checks: - - regexpMust - - # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. - # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". - enabled-tags: - - performance - - settings: # settings passed to gocritic - captLocal: # must be valid enabled check name - paramsOnly: true - rangeValCopy: - sizeThreshold: 32 - godox: - # report any comments starting with keywords, this is useful for TODO or FIXME comments that - # might be left in the code accidentally and should be resolved before merging - keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting - - NOTE - - OPTIMIZE # marks code that should be optimized before merging - - HACK # marks hack-arounds that should be removed before merging - dogsled: - # checks assignments with too many blank identifiers; default is 2 - max-blank-identifiers: 2 - + rowserrcheck: + packages: + - github.com/jmoiron/sqlx + unparam: + # Inspect exported functions, default is false. Set to true if no external program/library imports your code. + # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find external interfaces. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false + unused: + # treat code as a program (not a library) and report unused exported identifiers; default is false. + # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find funcs usages. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false whitespace: multi-if: false # Enforces newlines (or comments) after every multi-line if statement multi-func: false # Enforces newlines (or comments) after every multi-line function signature @@ -938,26 +940,6 @@ than the default and have more strict settings: ```yaml linters-settings: - govet: - check-shadowing: true - settings: - printf: - funcs: - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf - golint: - min-confidence: 0 - gocyclo: - min-complexity: 15 - maligned: - suggest-new: true - dupl: - threshold: 100 - goconst: - min-len: 2 - min-occurrences: 2 depguard: list-type: blacklist packages: @@ -966,12 +948,14 @@ linters-settings: - github.com/sirupsen/logrus packages-with-error-message: - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" - misspell: - locale: US - lll: - line-length: 140 - goimports: - local-prefixes: github.com/golangci/golangci-lint + dupl: + threshold: 100 + funlen: + lines: 100 + statements: 50 + goconst: + min-len: 2 + min-occurrences: 2 gocritic: enabled-tags: - diagnostic @@ -985,16 +969,38 @@ linters-settings: - octalLiteral - whyNoLint - wrapperFunc - funlen: - lines: 100 - statements: 50 + gocyclo: + min-complexity: 15 + goimports: + local-prefixes: github.com/golangci/golangci-lint + golint: + min-confidence: 0 + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: argument,case,condition,return + govet: + check-shadowing: true + settings: + printf: + funcs: + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US linters: # please, do not use `enable-all`: it's deprecated and will be removed soon. # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint disable-all: true enable: - # - rowserrcheck - bodyclose - deadcode - depguard @@ -1009,6 +1015,8 @@ linters: - gofmt - goimports - golint +# - gomnd TODO: enable it with release > v1.23.0 + - goprintffuncname - gosec - gosimple - govet @@ -1017,6 +1025,7 @@ linters: - lll - misspell - nakedret + - rowserrcheck - scopelint - staticcheck - structcheck @@ -1035,6 +1044,13 @@ linters: # - maligned # - prealloc +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + - path: _test\.go + linters: + - gomnd + run: skip-dirs: - test/testdata_etc @@ -1045,7 +1061,7 @@ run: # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration service: - golangci-lint-version: 1.22.x # use the fixed version to not introduce new linters unexpectedly + golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly prepare: - echo "here I can run custom commands, but no preparation needed for this repo" ``` diff --git a/cmd/golangci-lint/main.go b/cmd/golangci-lint/main.go index 079fc85d5562..282d794b8209 100644 --- a/cmd/golangci-lint/main.go +++ b/cmd/golangci-lint/main.go @@ -5,6 +5,7 @@ import ( "os" "github.com/golangci/golangci-lint/pkg/commands" + "github.com/golangci/golangci-lint/pkg/exitcodes" ) var ( @@ -19,6 +20,6 @@ func main() { if err := e.Execute(); err != nil { fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err) - os.Exit(1) + os.Exit(exitcodes.Failure) } } diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index fe598013d809..db2deb50c2be 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -210,7 +210,8 @@ func (e *Executor) acquireFileLock() bool { ctx, finish := context.WithTimeout(context.Background(), time.Minute) defer finish() - if ok, _ := f.TryLockContext(ctx, time.Second*3); !ok { + timeout := time.Second * 3 + if ok, _ := f.TryLockContext(ctx, timeout); !ok { return false } diff --git a/pkg/commands/root.go b/pkg/commands/root.go index e61ecc94bb57..c8548cd73400 100644 --- a/pkg/commands/root.go +++ b/pkg/commands/root.go @@ -93,18 +93,23 @@ func printMemStats(ms *runtime.MemStats, logger logutils.Log) { } func formatMemory(memBytes uint64) string { - if memBytes < 1024 { + const Kb = 1024 + const Mb = Kb * 1024 + + if memBytes < Kb { return fmt.Sprintf("%db", memBytes) } - if memBytes < 1024*1024 { - return fmt.Sprintf("%dkb", memBytes/1024) + if memBytes < Mb { + return fmt.Sprintf("%dkb", memBytes/Kb) } - return fmt.Sprintf("%dmb", memBytes/1024/1024) + return fmt.Sprintf("%dmb", memBytes/Mb) } func getDefaultConcurrency() int { if os.Getenv("HELP_RUN") == "1" { - return 8 // to make stable concurrency for README help generating builds + // Make stable concurrency for README help generating builds. + const prettyConcurrency = 8 + return prettyConcurrency } return runtime.NumCPU() diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 9358a651200e..b725e446bae1 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -473,7 +473,9 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log var maxRSSMB, totalRSSMB float64 var iterationsCount int - ticker := time.NewTicker(100 * time.Millisecond) + + const intervalMS = 100 + ticker := time.NewTicker(intervalMS * time.Millisecond) defer ticker.Stop() logEveryRecord := os.Getenv("GL_MEM_LOG_EVERY") == "1" diff --git a/pkg/config/config.go b/pkg/config/config.go index 01fa19b697c2..ba92cc00e36e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -158,6 +158,9 @@ type LintersSettings struct { MinStringLen int `mapstructure:"min-len"` MinOccurrencesCount int `mapstructure:"min-occurrences"` } + Gomnd struct { + Settings map[string]map[string]interface{} + } Depguard struct { ListType string `mapstructure:"list-type"` Packages []string @@ -268,6 +271,7 @@ type WSLSettings struct { CaseForceTrailingWhitespaceLimit int `mapstructure:"force-case-trailing-whitespace:"` } +//nolint:gomnd var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ LineLength: 120, @@ -360,7 +364,8 @@ func (e ExcludeRule) Validate() error { if e.Source != "" { nonBlank++ } - if nonBlank < 2 { + const minConditionsCount = 2 + if nonBlank < minConditionsCount { return errors.New("at least 2 of (text, source, path, linters) should be set") } return nil diff --git a/pkg/fsutils/linecache.go b/pkg/fsutils/linecache.go index 10f31422a912..ab408e7d5443 100644 --- a/pkg/fsutils/linecache.go +++ b/pkg/fsutils/linecache.go @@ -27,7 +27,8 @@ func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) { index1 = 1 } - rawLine, err := lc.getRawLine(filePath, index1-1) + const index1To0Offset = -1 + rawLine, err := lc.getRawLine(filePath, index1+index1To0Offset) if err != nil { return "", err } diff --git a/pkg/golinters/goanalysis/linter.go b/pkg/golinters/goanalysis/linter.go index 307a115696a2..778a1ef60be5 100644 --- a/pkg/golinters/goanalysis/linter.go +++ b/pkg/golinters/goanalysis/linter.go @@ -432,7 +432,9 @@ func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context, func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Issue, error) { log := lintCtx.Log.Child("goanalysis") sw := timeutils.NewStopwatch("analyzers", log) - defer sw.PrintTopStages(10) + + const stagesToPrint = 10 + defer sw.PrintTopStages(stagesToPrint) runner := newRunner(cfg.getName(), log, lintCtx.PkgCache, lintCtx.LoadGuard, cfg.getLoadMode(), sw) diff --git a/pkg/golinters/goanalysis/runner.go b/pkg/golinters/goanalysis/runner.go index 97c7f313c8a8..940e005331b8 100644 --- a/pkg/golinters/goanalysis/runner.go +++ b/pkg/golinters/goanalysis/runner.go @@ -312,7 +312,7 @@ func (r *runner) analyze(pkgs []*packages.Package, analyzers []*analysis.Analyze debugf("There are %d initial and %d total packages", len(initialPkgs), len(loadingPackages)) for _, lp := range loadingPackages { if lp.isInitial { - wg.Add(1) + wg.Add(1) //nolint:gomnd go func(lp *loadingPackage) { lp.analyzeRecursive(r.loadMode, loadSem) wg.Done() diff --git a/pkg/golinters/gomnd.go b/pkg/golinters/gomnd.go index 35b868579755..0a64f88f8a66 100644 --- a/pkg/golinters/gomnd.go +++ b/pkg/golinters/gomnd.go @@ -4,18 +4,24 @@ import ( mnd "github.com/tommy-muehle/go-mnd" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewGoMND() *goanalysis.Linter { +func NewGoMND(cfg *config.Config) *goanalysis.Linter { analyzers := []*analysis.Analyzer{ mnd.Analyzer, } + var linterCfg map[string]map[string]interface{} + if cfg != nil { + linterCfg = cfg.LintersSettings.Gomnd.Settings + } + return goanalysis.NewLinter( "gomnd", "An analyzer to detect magic numbers.", analyzers, - nil, + linterCfg, ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/golinters/scopelint.go b/pkg/golinters/scopelint.go index 309ff270802d..660b3ab625ed 100644 --- a/pkg/golinters/scopelint.go +++ b/pkg/golinters/scopelint.go @@ -166,10 +166,10 @@ func (f *Node) Visit(node ast.Node) ast.Visitor { //nolint:interfacer func (f *Node) errorf(n ast.Node, format string, args ...interface{}) { pos := f.fset.Position(n.Pos()) - f.errorfAt(pos, format, args...) + f.errorAtf(pos, format, args...) } -func (f *Node) errorfAt(pos token.Position, format string, args ...interface{}) { +func (f *Node) errorAtf(pos token.Position, format string, args ...interface{}) { *f.issues = append(*f.issues, result.Issue{ Pos: pos, Text: fmt.Sprintf(format, args...), diff --git a/pkg/lint/lintersdb/enabled_set.go b/pkg/lint/lintersdb/enabled_set.go index 73e3bcfed29e..a3b73e7074b6 100644 --- a/pkg/lint/lintersdb/enabled_set.go +++ b/pkg/lint/lintersdb/enabled_set.go @@ -113,7 +113,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config) } } - if len(goanalysisLinters) <= 1 { + if len(goanalysisLinters) <= 1 { //nolint:gomnd es.debugf("Didn't combine go/analysis linters: got only %d linters", len(goanalysisLinters)) return } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index f35265f5d402..3498b5e291aa 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -244,7 +244,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewGoPrintfFuncName()). WithPresets(linter.PresetStyle). WithURL("https://github.com/jirfag/go-printf-func-name"), - linter.NewConfig(golinters.NewGoMND()). + linter.NewConfig(golinters.NewGoMND(m.cfg)). WithPresets(linter.PresetStyle). WithURL("https://github.com/tommy-muehle/go-mnd"), } diff --git a/pkg/packages/errors.go b/pkg/packages/errors.go index 72fb8601ab70..c620573b9380 100644 --- a/pkg/packages/errors.go +++ b/pkg/packages/errors.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" ) +//nolint:gomnd func ParseErrorPosition(pos string) (*token.Position, error) { // file:line(:colon) parts := strings.Split(pos, ":") diff --git a/pkg/timeutils/track.go b/pkg/timeutils/track.go deleted file mode 100644 index ada1b9a4a7c2..000000000000 --- a/pkg/timeutils/track.go +++ /dev/null @@ -1,12 +0,0 @@ -package timeutils - -import ( - "fmt" - "time" - - "github.com/golangci/golangci-lint/pkg/logutils" -) - -func Track(from time.Time, log logutils.Log, format string, args ...interface{}) { - log.Infof("%s took %s", fmt.Sprintf(format, args...), time.Since(from)) -} diff --git a/test/errchk.go b/test/errchk.go index c06168871d20..e1b90e6a1117 100644 --- a/test/errchk.go +++ b/test/errchk.go @@ -85,7 +85,7 @@ func errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) { if len(errs) == 0 { return nil } - if len(errs) == 1 { + if len(errs) == 1 { //nolint:gomnd return errs[0] } var buf bytes.Buffer