Skip to content

Commit 6709c97

Browse files
authored
dev: refactor .golangci.yml configuration and fix up nolintlint issues (#4537)
1 parent 02ea91d commit 6709c97

Some content is hidden

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

42 files changed

+58
-75
lines changed

.golangci.yml

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ linters-settings:
1717
logger:
1818
deny:
1919
# logging is allowed only by logutils.Log,
20-
# logrus is allowed to use only in logutils package.
2120
- pkg: "github.com/sirupsen/logrus"
2221
desc: logging is allowed only by logutils.Log.
2322
- pkg: "github.com/pkg/errors"
2423
desc: Should be replaced by standard lib errors package.
2524
- pkg: "github.com/instana/testify"
2625
desc: It's a fork of github.com/stretchr/testify.
26+
files:
27+
# logrus is allowed to use only in logutils package.
28+
- "!**/pkg/logutils/**.go"
2729
dupl:
2830
threshold: 100
2931
funlen:
@@ -86,10 +88,12 @@ linters-settings:
8688
line-length: 140
8789
misspell:
8890
locale: US
91+
ignore-words:
92+
- "importas" # linter name
8993
nolintlint:
9094
allow-unused: false # report any unused nolint directives
91-
require-explanation: false # don't require an explanation for nolint directives
92-
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
95+
require-explanation: true # require an explanation for nolint directives
96+
require-specific: true # require nolint directives to be specific about which linter is being skipped
9397
revive:
9498
rules:
9599
- name: unexported-return
@@ -144,7 +148,9 @@ issues:
144148
exclude-rules:
145149
- path: _test\.go
146150
linters:
151+
- dupl
147152
- gomnd
153+
- lll
148154

149155
- path: pkg/golinters/errcheck.go
150156
linters: [staticcheck]
@@ -158,6 +164,10 @@ issues:
158164
- path: pkg/golinters/govet.go
159165
text: "SA1019: cfg.CheckShadowing is deprecated: the linter should be enabled inside `Enable`."
160166

167+
- path: pkg/golinters
168+
linters:
169+
- dupl
170+
161171
- path: pkg/golinters/gofumpt.go
162172
linters: [staticcheck]
163173
text: "SA1019: settings.LangVersion is deprecated: use the global `run.go` instead."

pkg/commands/flagsets.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
8080
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))
8181
}
8282

83-
//nolint:gomnd
83+
//nolint:gomnd // magic numbers here is ok
8484
func setupIssuesFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
8585
internal.AddHackedStringSliceP(fs, "exclude", "e", color.GreenString("Exclude issue by regexp"))
8686
internal.AddFlagAndBind(v, fs, fs.Bool, "exclude-use-default", "issues.exclude-use-default", true,

pkg/golinters/dogsled.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const dogsledName = "dogsled"
1818

19-
//nolint:dupl
2019
func NewDogsled(settings *config.DogsledSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/dupl.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
const duplName = "dupl"
1919

20-
//nolint:dupl
2120
func NewDupl(settings *config.DuplSettings) *goanalysis.Linter {
2221
var mu sync.Mutex
2322
var resIssues []goanalysis.Issue

pkg/golinters/forbidigo.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const forbidigoName = "forbidigo"
1818

19-
//nolint:dupl
2019
func NewForbidigo(settings *config.ForbidigoSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/funlen.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const funlenName = "funlen"
1818

19-
//nolint:dupl
2019
func NewFunlen(settings *config.FunlenSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/goanalysis/runner.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,9 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac
177177
}
178178
}
179179

180-
//nolint:gocritic
181180
func (r *runner) prepareAnalysis(pkgs []*packages.Package,
182181
analyzers []*analysis.Analyzer,
183-
) (map[*packages.Package]bool, []*action, []*action) {
182+
) (initialPkgs map[*packages.Package]bool, allActions, roots []*action) {
184183
// Construct the action graph.
185184

186185
// Each graph node (action) is one unit of analysis.
@@ -200,13 +199,13 @@ func (r *runner) prepareAnalysis(pkgs []*packages.Package,
200199
actions := make(map[actKey]*action, totalActionsCount)
201200
actAlloc := newActionAllocator(totalActionsCount)
202201

203-
initialPkgs := make(map[*packages.Package]bool, len(pkgs))
202+
initialPkgs = make(map[*packages.Package]bool, len(pkgs))
204203
for _, pkg := range pkgs {
205204
initialPkgs[pkg] = true
206205
}
207206

208207
// Build nodes for initial packages.
209-
roots := make([]*action, 0, len(pkgs)*len(analyzers))
208+
roots = make([]*action, 0, len(pkgs)*len(analyzers))
210209
for _, a := range analyzers {
211210
for _, pkg := range pkgs {
212211
root := r.makeAction(a, pkg, initialPkgs, actions, actAlloc)
@@ -215,7 +214,7 @@ func (r *runner) prepareAnalysis(pkgs []*packages.Package,
215214
}
216215
}
217216

218-
allActions := maps.Values(actions)
217+
allActions = maps.Values(actions)
219218

220219
debugf("Built %d actions", len(actions))
221220

@@ -281,7 +280,6 @@ func (r *runner) analyze(pkgs []*packages.Package, analyzers []*analysis.Analyze
281280
return rootActions
282281
}
283282

284-
//nolint:nakedret
285283
func extractDiagnostics(roots []*action) (retDiags []Diagnostic, retErrors []error) {
286284
extracted := make(map[*action]bool)
287285
var extract func(*action)
@@ -338,5 +336,5 @@ func extractDiagnostics(roots []*action) (retDiags []Diagnostic, retErrors []err
338336
}
339337
}
340338
visitAll(roots)
341-
return
339+
return retDiags, retErrors
342340
}

pkg/golinters/goanalysis/runners.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.
184184
issuesCacheDebugf("Saved %d issues from %d packages to cache in %s", savedIssuesCount, len(allPkgs), time.Since(startedAt))
185185
}
186186

187-
//nolint:gocritic
188187
func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context,
189188
analyzers []*analysis.Analyzer,
190-
) ([]result.Issue, map[*packages.Package]bool) {
189+
) (issuesFromCache []result.Issue, pkgsFromCache map[*packages.Package]bool) {
191190
startedAt := time.Now()
192191

193192
lintResKey := getIssuesCacheKey(analyzers)
@@ -221,17 +220,18 @@ func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context,
221220
}
222221

223222
issues := make([]result.Issue, 0, len(pkgIssues))
224-
for _, i := range pkgIssues {
223+
for i := range pkgIssues {
224+
issue := &pkgIssues[i]
225225
issues = append(issues, result.Issue{
226-
FromLinter: i.FromLinter,
227-
Text: i.Text,
228-
Severity: i.Severity,
229-
Pos: i.Pos,
230-
LineRange: i.LineRange,
231-
Replacement: i.Replacement,
226+
FromLinter: issue.FromLinter,
227+
Text: issue.Text,
228+
Severity: issue.Severity,
229+
Pos: issue.Pos,
230+
LineRange: issue.LineRange,
231+
Replacement: issue.Replacement,
232232
Pkg: pkg,
233-
ExpectNoLint: i.ExpectNoLint,
234-
ExpectedNoLintLinter: i.ExpectedNoLintLinter,
233+
ExpectNoLint: issue.ExpectNoLint,
234+
ExpectedNoLintLinter: issue.ExpectedNoLintLinter,
235235
})
236236
}
237237
cacheRes.issues = issues
@@ -246,21 +246,20 @@ func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context,
246246
wg.Wait()
247247

248248
loadedIssuesCount := 0
249-
var issues []result.Issue
250-
pkgsFromCache := map[*packages.Package]bool{}
249+
pkgsFromCache = map[*packages.Package]bool{}
251250
for pkg, cacheRes := range pkgToCacheRes {
252251
if cacheRes.loadErr == nil {
253252
loadedIssuesCount += len(cacheRes.issues)
254253
pkgsFromCache[pkg] = true
255-
issues = append(issues, cacheRes.issues...)
254+
issuesFromCache = append(issuesFromCache, cacheRes.issues...)
256255
issuesCacheDebugf("Loaded package %s issues (%d) from cache", pkg, len(cacheRes.issues))
257256
} else {
258257
issuesCacheDebugf("Didn't load package %s issues from cache: %s", pkg, cacheRes.loadErr)
259258
}
260259
}
261260
issuesCacheDebugf("Loaded %d issues from cache in %s, analyzing %d/%d packages",
262261
loadedIssuesCount, time.Since(startedAt), len(pkgs)-len(pkgsFromCache), len(pkgs))
263-
return issues, pkgsFromCache
262+
return issuesFromCache, pkgsFromCache
264263
}
265264

266265
func analyzersHashID(analyzers []*analysis.Analyzer) string {

pkg/golinters/gocognit.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const gocognitName = "gocognit"
1818

19-
//nolint:dupl
2019
func NewGocognit(settings *config.GocognitSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/goconst.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const goconstName = "goconst"
1717

18-
//nolint:dupl
1918
func NewGoconst(settings *config.GoConstSettings) *goanalysis.Linter {
2019
var mu sync.Mutex
2120
var resIssues []goanalysis.Issue

pkg/golinters/gocyclo.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const gocycloName = "gocyclo"
1717

18-
//nolint:dupl
1918
func NewGocyclo(settings *config.GoCycloSettings) *goanalysis.Linter {
2019
var mu sync.Mutex
2120
var resIssues []goanalysis.Issue

pkg/golinters/godox.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const godoxName = "godox"
1818

19-
//nolint:dupl
2019
func NewGodox(settings *config.GodoxSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/gofmt_common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func (p *hunkChangesParser) handleDeletedLines(deletedLines []diffLine, addedLin
133133
}
134134

135135
if len(addedLines) != 0 {
136-
//nolint:gocritic
137-
change.Replacement.NewLines = append(p.replacementLinesToPrepend, addedLines...)
136+
change.Replacement.NewLines = append([]string{}, p.replacementLinesToPrepend...)
137+
change.Replacement.NewLines = append(change.Replacement.NewLines, addedLines...)
138138
if len(p.replacementLinesToPrepend) != 0 {
139139
p.replacementLinesToPrepend = nil
140140
}

pkg/golinters/importas.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strconv"
66
"strings"
77

8-
"github.com/julz/importas" //nolint:misspell
8+
"github.com/julz/importas"
99
"golang.org/x/tools/go/analysis"
1010

1111
"github.com/golangci/golangci-lint/pkg/config"
@@ -26,7 +26,7 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
2626
return
2727
}
2828
if len(settings.Alias) == 0 {
29-
lintCtx.Log.Infof("importas settings found, but no aliases listed. List aliases under alias: key.") //nolint:misspell
29+
lintCtx.Log.Infof("importas settings found, but no aliases listed. List aliases under alias: key.")
3030
}
3131

3232
if err := analyzer.Flags.Set("no-unaliased", strconv.FormatBool(settings.NoUnaliased)); err != nil {

pkg/golinters/lll.go

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const lllName = "lll"
2222

2323
const goCommentDirectivePrefix = "//go:"
2424

25-
//nolint:dupl
2625
func NewLLL(settings *config.LllSettings) *goanalysis.Linter {
2726
var mu sync.Mutex
2827
var resIssues []goanalysis.Issue

pkg/golinters/makezero.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const makezeroName = "makezero"
1717

18-
//nolint:dupl
1918
func NewMakezero(settings *config.MakezeroSettings) *goanalysis.Linter {
2019
var mu sync.Mutex
2120
var resIssues []goanalysis.Issue

pkg/golinters/nestif.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const nestifName = "nestif"
1717

18-
//nolint:dupl
1918
func NewNestif(settings *config.NestifSettings) *goanalysis.Linter {
2019
var mu sync.Mutex
2120
var resIssues []goanalysis.Issue

pkg/golinters/nolintlint.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
const NoLintLintName = "nolintlint"
1818

19-
//nolint:dupl
2019
func NewNoLintLint(settings *config.NoLintLintSettings) *goanalysis.Linter {
2120
var mu sync.Mutex
2221
var resIssues []goanalysis.Issue

pkg/golinters/nolintlint/nolintlint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ var (
156156
trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`)
157157
)
158158

159-
//nolint:funlen,gocyclo
159+
//nolint:funlen,gocyclo // the function is going to be refactored in the future
160160
func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
161161
var issues []Issue
162162

pkg/golinters/nolintlint/nolintlint_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func foo() {
131131
good() //nolint: linter1, linter2
132132
}`,
133133
expected: []issueWithReplacement{
134-
{issue: "directive `//nolint:linter1 linter2` should match `//nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9"}, //nolint:lll // this is a string
134+
{issue: "directive `//nolint:linter1 linter2` should match `//nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9"},
135135
},
136136
},
137137
{

pkg/golinters/prealloc.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
const preallocName = "prealloc"
1717

18-
//nolint:dupl
1918
func NewPreAlloc(settings *config.PreallocSettings) *goanalysis.Linter {
2019
var mu sync.Mutex
2120
var resIssues []goanalysis.Issue

pkg/golinters/unconvert.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
const unconvertName = "unconvert"
1616

17-
//nolint:dupl // This is not a duplicate of dogsled.
1817
func NewUnconvert(settings *config.UnconvertSettings) *goanalysis.Linter {
1918
var mu sync.Mutex
2019
var resIssues []goanalysis.Issue

pkg/logutils/stderr_log.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"time"
77

8-
"github.com/sirupsen/logrus" //nolint:depguard
8+
"github.com/sirupsen/logrus"
99

1010
"github.com/golangci/golangci-lint/pkg/exitcodes"
1111
)

pkg/packages/util_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
//nolint:lll
109
func Test_stackCrusher(t *testing.T) {
1110
testCases := []struct {
1211
desc string

pkg/printers/checkstyle_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestCheckstyle_Print(t *testing.T) {
4949
err := printer.Print(issues)
5050
require.NoError(t, err)
5151

52-
//nolint:lll
5352
expected := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<checkstyle version=\"5.0\">\n <file name=\"path/to/filea.go\">\n <error column=\"4\" line=\"10\" message=\"some issue\" severity=\"warning\" source=\"linter-a\"></error>\n </file>\n <file name=\"path/to/fileb.go\">\n <error column=\"9\" line=\"300\" message=\"another issue\" severity=\"error\" source=\"linter-b\"></error>\n </file>\n</checkstyle>\n"
5453

5554
assert.Equal(t, expected, strings.ReplaceAll(buf.String(), "\r", ""))

pkg/printers/codeclimate_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func TestCodeClimate_Print(t *testing.T) {
6363
err := printer.Print(issues)
6464
require.NoError(t, err)
6565

66-
//nolint:lll
6766
expected := `[{"description":"linter-a: some issue","severity":"warning","fingerprint":"BA73C5DF4A6FD8462FFF1D3140235777","location":{"path":"path/to/filea.go","lines":{"begin":10}}},{"description":"linter-b: another issue","severity":"error","fingerprint":"0777B4FE60242BD8B2E9B7E92C4B9521","location":{"path":"path/to/fileb.go","lines":{"begin":300}}},{"description":"linter-c: issue c","severity":"critical","fingerprint":"BEE6E9FBB6BFA4B7DB9FB036697FB036","location":{"path":"path/to/filec.go","lines":{"begin":200}}}]
6867
`
6968

pkg/printers/github_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//nolint:dupl
21
package printers
32

43
import (

pkg/printers/html_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/golangci/golangci-lint/pkg/result"
1212
)
1313

14-
//nolint:lll
1514
const expectedHTML = `<!doctype html>
1615
<html lang="en">
1716
<head>

pkg/printers/json_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestJSON_Print(t *testing.T) {
4949
err := printer.Print(issues)
5050
require.NoError(t, err)
5151

52-
//nolint:lll
5352
expected := `{"Issues":[{"FromLinter":"linter-a","Text":"some issue","Severity":"warning","SourceLines":null,"Replacement":null,"Pos":{"Filename":"path/to/filea.go","Offset":2,"Line":10,"Column":4},"ExpectNoLint":false,"ExpectedNoLintLinter":""},{"FromLinter":"linter-b","Text":"another issue","Severity":"error","SourceLines":["func foo() {","\tfmt.Println(\"bar\")","}"],"Replacement":null,"Pos":{"Filename":"path/to/fileb.go","Offset":5,"Line":300,"Column":9},"ExpectNoLint":false,"ExpectedNoLintLinter":""}],"Report":null}
5453
`
5554

pkg/printers/junitxml_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//nolint:dupl
21
package printers
32

43
import (

0 commit comments

Comments
 (0)