Skip to content

Commit a8b7b00

Browse files
authored
Update gochecknoglobals, use source analyzer (#1422)
1 parent 58234f0 commit a8b7b00

File tree

6 files changed

+39
-102
lines changed

6 files changed

+39
-102
lines changed

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/golangci/golangci-lint
33
go 1.12
44

55
require (
6+
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a
67
github.com/Djarvur/go-err113 v0.0.0-20200511133814-5174e21577d5
78
github.com/OpenPeeDeeP/depguard v1.0.1
89
github.com/bombsimon/wsl/v3 v3.1.0
@@ -62,9 +63,10 @@ require (
6263
github.com/ultraware/whitespace v0.0.4
6364
github.com/uudashr/gocognit v1.0.1
6465
github.com/valyala/quicktemplate v1.6.3
65-
golang.org/x/tools v0.0.0-20201011145850-ed2f50202694
66+
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
67+
golang.org/x/tools v0.0.0-20201013201025-64a9e34f3752
6668
gopkg.in/yaml.v2 v2.3.0
67-
honnef.co/go/tools v0.0.1-2020.1.6
69+
honnef.co/go/tools v0.0.1-2020.1.5
6870
mvdan.cc/gofumpt v0.0.0-20200802201014-ab5a8192947d
6971
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
7072
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect

go.sum

+11-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gochecknoglobals.go

+17-87
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,30 @@
11
package golinters
22

33
import (
4-
"fmt"
5-
"go/ast"
6-
"go/token"
7-
"strings"
8-
"sync"
9-
104
"golang.org/x/tools/go/analysis"
115

6+
"4d63.com/gochecknoglobals/checknoglobals"
7+
128
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
13-
"github.com/golangci/golangci-lint/pkg/lint/linter"
14-
"github.com/golangci/golangci-lint/pkg/result"
159
)
1610

17-
const gochecknoglobalsName = "gochecknoglobals"
18-
19-
//nolint:dupl
2011
func NewGochecknoglobals() *goanalysis.Linter {
21-
var mu sync.Mutex
22-
var resIssues []goanalysis.Issue
23-
24-
analyzer := &analysis.Analyzer{
25-
Name: gochecknoglobalsName,
26-
Doc: goanalysis.TheOnlyanalyzerDoc,
27-
Run: func(pass *analysis.Pass) (interface{}, error) {
28-
var res []goanalysis.Issue
29-
for _, file := range pass.Files {
30-
fileIssues := checkFileForGlobals(file, pass.Fset)
31-
for i := range fileIssues {
32-
res = append(res, goanalysis.NewIssue(&fileIssues[i], pass))
33-
}
34-
}
35-
if len(res) == 0 {
36-
return nil, nil
37-
}
38-
39-
mu.Lock()
40-
resIssues = append(resIssues, res...)
41-
mu.Unlock()
42-
43-
return nil, nil
12+
gochecknoglobals := checknoglobals.Analyzer()
13+
14+
// gochecknoglobals only lints test files if the `-t` flag is passed so we
15+
// pass the `t` flag as true to the analyzer before running it. This can be
16+
// turned of by using the regular golangci-lint flags such as `--tests` or
17+
// `--skip-files`.
18+
linterConfig := map[string]map[string]interface{}{
19+
gochecknoglobals.Name: {
20+
"t": true,
4421
},
4522
}
46-
return goanalysis.NewLinter(
47-
gochecknoglobalsName,
48-
"Checks that no globals are present in Go code",
49-
[]*analysis.Analyzer{analyzer},
50-
nil,
51-
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
52-
return resIssues
53-
}).WithLoadMode(goanalysis.LoadModeSyntax)
54-
}
55-
56-
func checkFileForGlobals(f *ast.File, fset *token.FileSet) []result.Issue {
57-
var res []result.Issue
58-
for _, decl := range f.Decls {
59-
genDecl, ok := decl.(*ast.GenDecl)
60-
if !ok {
61-
continue
62-
}
63-
if genDecl.Tok != token.VAR {
64-
continue
65-
}
66-
67-
for _, spec := range genDecl.Specs {
68-
valueSpec := spec.(*ast.ValueSpec)
69-
for _, vn := range valueSpec.Names {
70-
if isWhitelisted(vn) {
71-
continue
72-
}
73-
74-
res = append(res, result.Issue{
75-
Pos: fset.Position(vn.Pos()),
76-
Text: fmt.Sprintf("%s is a global variable", formatCode(vn.Name, nil)),
77-
FromLinter: gochecknoglobalsName,
78-
})
79-
}
80-
}
81-
}
82-
83-
return res
84-
}
8523

86-
func isWhitelisted(i *ast.Ident) bool {
87-
return i.Name == "_" || i.Name == "version" || looksLikeError(i)
88-
}
89-
90-
// looksLikeError returns true if the AST identifier starts
91-
// with 'err' or 'Err', or false otherwise.
92-
//
93-
// TODO: https://github.com/leighmcculloch/gochecknoglobals/issues/5
94-
func looksLikeError(i *ast.Ident) bool {
95-
prefix := "err"
96-
if i.IsExported() {
97-
prefix = "Err"
98-
}
99-
return strings.HasPrefix(i.Name, prefix)
24+
return goanalysis.NewLinter(
25+
gochecknoglobals.Name,
26+
gochecknoglobals.Doc,
27+
[]*analysis.Analyzer{gochecknoglobals},
28+
linterConfig,
29+
).WithLoadMode(goanalysis.LoadModeSyntax)
10030
}

pkg/golinters/gochecknoinits.go

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

1616
const gochecknoinitsName = "gochecknoinits"
1717

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

test/testdata/gochecknoglobals.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ package testdata
44
import (
55
"errors"
66
"fmt"
7+
"regexp"
78
)
89

9-
var noGlobalsVar int // ERROR "`noGlobalsVar` is a global variable"
10+
var noGlobalsVar int // ERROR "noGlobalsVar is a global variable"
1011
var ErrSomeType = errors.New("test that global erorrs aren't warned")
1112

13+
var (
14+
OnlyDigites = regexp.MustCompile(`^\d+$`)
15+
BadNamedErr = errors.New("this is bad") // ERROR "BadNamedErr is a global variable"
16+
)
17+
1218
func NoGlobals() {
1319
fmt.Print(noGlobalsVar)
1420
}

test/testdata/testpackage_internal_test.go

-6
This file was deleted.

0 commit comments

Comments
 (0)