Skip to content

Commit d437ac8

Browse files
committed
Implement auto-fixing for gofmt,goimports,misspell
Also, add more identifier marking patterns.
1 parent 1eb7125 commit d437ac8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1247
-180
lines changed

.golangci.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ run:
4343
skip-dirs:
4444
- test/testdata_etc
4545

46+
issues:
47+
exclude-rules:
48+
- text: "weak cryptographic primitive"
49+
linters:
50+
- gosec
51+
4652
# golangci.com configuration
4753
# https://github.com/golangci/golangci/wiki/Configuration
4854
service:
49-
golangci-lint-version: 1.13.x # use fixed version to not introduce new linters unexpectedly
55+
golangci-lint-version: 1.14.x # use the fixed version to not introduce new linters unexpectedly
5056
prepare:
5157
- echo "here I can run custom commands, but no preparation needed"
52-
53-
issues:
54-
exclude-rules:
55-
- text: "weak cryptographic primitive"
56-
linters:
57-
- gosec

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,16 @@ GolangCI-Lint can be used with zero configuration. By default the following lint
185185
```bash
186186
$ golangci-lint help linters
187187
Enabled by default linters:
188-
govet (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true]
189-
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true]
190-
staticcheck: Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false]
191-
unused: Checks Go code for unused constants, variables, functions and types [fast: false]
192-
gosimple: Linter for Go source code that specializes in simplifying a code [fast: false]
193-
structcheck: Finds an unused struct fields [fast: true]
194-
varcheck: Finds unused global variables and constants [fast: true]
195-
ineffassign: Detects when assignments to existing variables are not used [fast: true]
196-
deadcode: Finds unused code [fast: true]
197-
typecheck: Like the front-end of a Go compiler, parses and type-checks Go code [fast: true]
188+
govet (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true, auto-fix: false]
189+
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true, auto-fix: false]
190+
staticcheck: Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
191+
unused: Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
192+
gosimple: Linter for Go source code that specializes in simplifying a code [fast: false, auto-fix: false]
193+
structcheck: Finds an unused struct fields [fast: true, auto-fix: false]
194+
varcheck: Finds unused global variables and constants [fast: true, auto-fix: false]
195+
ineffassign: Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
196+
deadcode: Finds unused code [fast: true, auto-fix: false]
197+
typecheck: Like the front-end of a Go compiler, parses and type-checks Go code [fast: true, auto-fix: false]
198198
```
199199
200200
and the following linters are disabled by default:
@@ -203,27 +203,27 @@ and the following linters are disabled by default:
203203
$ golangci-lint help linters
204204
...
205205
Disabled by default linters:
206-
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true]
207-
stylecheck: Stylecheck is a replacement for golint [fast: false]
208-
gosec (gas): Inspects source code for security problems [fast: true]
209-
interfacer: Linter that suggests narrower interface types [fast: false]
210-
unconvert: Remove unnecessary type conversions [fast: true]
211-
dupl: Tool for code clone detection [fast: true]
212-
goconst: Finds repeated strings that could be replaced by a constant [fast: true]
213-
gocyclo: Computes and checks the cyclomatic complexity of functions [fast: true]
214-
gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true]
215-
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true]
216-
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: true]
217-
depguard: Go linter that checks if package imports are in a list of acceptable packages [fast: true]
218-
misspell: Finds commonly misspelled English words in comments [fast: true]
219-
lll: Reports long lines [fast: true]
220-
unparam: Reports unused function parameters [fast: false]
221-
nakedret: Finds naked returns in functions greater than a specified function length [fast: true]
222-
prealloc: Finds slice declarations that could potentially be preallocated [fast: true]
223-
scopelint: Scopelint checks for unpinned variables in go programs [fast: true]
224-
gocritic: The most opinionated Go source code linter [fast: true]
225-
gochecknoinits: Checks that no init functions are present in Go code [fast: true]
226-
gochecknoglobals: Checks that no globals are present in Go code [fast: true]
206+
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true, auto-fix: false]
207+
stylecheck: Stylecheck is a replacement for golint [fast: false, auto-fix: false]
208+
gosec (gas): Inspects source code for security problems [fast: true, auto-fix: false]
209+
interfacer: Linter that suggests narrower interface types [fast: false, auto-fix: false]
210+
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
211+
dupl: Tool for code clone detection [fast: true, auto-fix: false]
212+
goconst: Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
213+
gocyclo: Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
214+
gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
215+
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
216+
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
217+
depguard: Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
218+
misspell: Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
219+
lll: Reports long lines [fast: true, auto-fix: false]
220+
unparam: Reports unused function parameters [fast: false, auto-fix: false]
221+
nakedret: Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
222+
prealloc: Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
223+
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
224+
gocritic: The most opinionated Go source code linter [fast: true, auto-fix: false]
225+
gochecknoinits: Checks that no init functions are present in Go code [fast: true, auto-fix: false]
226+
gochecknoglobals: Checks that no globals are present in Go code [fast: true, auto-fix: false]
227227
```
228228
229229
Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
@@ -499,6 +499,7 @@ Flags:
499499
For CI setups, prefer --new-from-rev=HEAD~, as --new can skip linting the current patch if any scripts generate unstaged files before golangci-lint runs.
500500
--new-from-rev REV Show only new issues created after git revision REV
501501
--new-from-patch PATH Show only new issues created in git patch with file path PATH
502+
--fix Fix found issues (if it's supported by the linter)
502503
-h, --help help for run
503504
504505
Global Flags:
@@ -829,18 +830,18 @@ run:
829830
skip-dirs:
830831
- test/testdata_etc
831832
832-
# golangci.com configuration
833-
# https://github.com/golangci/golangci/wiki/Configuration
834-
service:
835-
golangci-lint-version: 1.13.x # use fixed version to not introduce new linters unexpectedly
836-
prepare:
837-
- echo "here I can run custom commands, but no preparation needed"
838-
839833
issues:
840834
exclude-rules:
841835
- text: "weak cryptographic primitive"
842836
linters:
843837
- gosec
838+
839+
# golangci.com configuration
840+
# https://github.com/golangci/golangci/wiki/Configuration
841+
service:
842+
golangci-lint-version: 1.14.x # use the fixed version to not introduce new linters unexpectedly
843+
prepare:
844+
- echo "here I can run custom commands, but no preparation needed"
844845
```
845846
846847
## False Positives

pkg/commands/help.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func printLinterConfigs(lcs []*linter.Config) {
4141
if len(lc.AlternativeNames) != 0 {
4242
altNamesStr = fmt.Sprintf(" (%s)", strings.Join(lc.AlternativeNames, ", "))
4343
}
44-
fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t]\n", color.YellowString(lc.Name()),
45-
altNamesStr, lc.Linter.Desc(), !lc.NeedsSSARepr)
44+
fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()),
45+
altNamesStr, lc.Linter.Desc(), !lc.NeedsSSARepr, lc.CanAutoFix)
4646
}
4747
}
4848

pkg/commands/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/golangci/golangci-lint/pkg/result/processors"
14+
1315
"github.com/fatih/color"
1416
"github.com/pkg/errors"
1517
"github.com/spf13/cobra"
@@ -177,7 +179,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
177179
wh("Show only new issues created after git revision `REV`"))
178180
fs.StringVar(&ic.DiffPatchFilePath, "new-from-patch", "",
179181
wh("Show only new issues created in git patch with file path `PATH`"))
180-
182+
fs.BoolVar(&ic.NeedFix, "fix", false, "Fix found issues (if it's supported by the linter)")
181183
}
182184

183185
func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
@@ -281,7 +283,9 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
281283
return nil, err
282284
}
283285

284-
return runner.Run(ctx, enabledLinters, lintCtx), nil
286+
issuesCh := runner.Run(ctx, enabledLinters, lintCtx)
287+
fixer := processors.NewFixer(e.cfg, e.log)
288+
return fixer.Process(issuesCh), nil
285289
}
286290

287291
func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ type Issues struct {
278278
DiffFromRevision string `mapstructure:"new-from-rev"`
279279
DiffPatchFilePath string `mapstructure:"new-from-patch"`
280280
Diff bool `mapstructure:"new"`
281+
282+
NeedFix bool `mapstructure:"fix"`
281283
}
282284

283285
type Config struct { //nolint:maligned

0 commit comments

Comments
 (0)