diff --git a/.gitignore b/.gitignore index ba78129..b61b71d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -gochecknoglobals +./gochecknoglobals diff --git a/README.md b/README.md index 67ef370..78deb7c 100644 --- a/README.md +++ b/README.md @@ -29,22 +29,23 @@ go get 4d63.com/gochecknoglobals ## Usage -``` -gochecknoglobals -``` +The linter is built on [Go's analysis package] and does thus support all the +built in flags and features from this type. The analyzer is executed by +specifying packages. + +[Go's analysis package]: https://pkg.go.dev/golang.org/x/tools/go/analysis ``` -gochecknoglobals ./... +gochecknoglobals [package] ``` ``` -gochecknoglobals [path] [path] [path] [etc] +gochecknoglobals ./... ``` -Add `-t` to include tests. +By default, test files will not be checked but can be included by adding the +`-t` flag. ``` -gochecknoglobals -t [path] +gochecknoglobals -t [package] ``` - -Note: Paths are only inspected recursively if the Go `/...` recursive path suffix is appended to the path. diff --git a/check_no_globals_test.go b/check_no_globals_test.go deleted file mode 100644 index d1c0dd2..0000000 --- a/check_no_globals_test.go +++ /dev/null @@ -1,179 +0,0 @@ -package main - -import ( - "fmt" - "testing" -) - -func TestCheckNoGlobals(t *testing.T) { - cases := []struct { - path string - includeTests bool - wantMessages []string - }{ - { - path: "testdata/0", - wantMessages: nil, - }, - { - path: "testdata/0", - includeTests: true, - wantMessages: nil, - }, - { - path: "testdata/0/code.go", - wantMessages: nil, - }, - { - path: "testdata/1", - wantMessages: nil, - }, - { - path: "testdata/2", - wantMessages: []string{ - "testdata/2/code.go:3 myVar is a global variable", - }, - }, - { - path: "testdata/2", - includeTests: true, - wantMessages: []string{ - "testdata/2/code.go:3 myVar is a global variable", - "testdata/2/code_test.go:3 myTestVar is a global variable", - }, - }, - { - path: "testdata/3", - wantMessages: []string{ - "testdata/3/code_0.go:8 theVar is a global variable", - "testdata/3/code_1.go:3 myVar is a global variable", - }, - }, - { - path: "testdata/3/code_0.go", - wantMessages: []string{ - "testdata/3/code_0.go:8 theVar is a global variable", - }, - }, - { - path: "testdata/4", - wantMessages: []string{ - "testdata/4/code.go:3 theVar is a global variable", - }, - }, - { - path: "testdata/4/...", - wantMessages: []string{ - "testdata/4/code.go:3 theVar is a global variable", - "testdata/4/subpkg/code_1.go:3 myVar is a global variable", - }, - }, - { - path: "testdata/5", - wantMessages: []string{ - "testdata/5/code.go:3 myVar1 is a global variable", - "testdata/5/code.go:3 myVar2 is a global variable", - }, - }, - { - path: "testdata/6", - wantMessages: nil, - }, - { - path: "testdata/7", - wantMessages: []string{ - "testdata/7/code.go:8 myVar is a global variable", - // TODO: "testdata/7/code.go:13 errFakeErrorUnexported is a global variable", - // TODO: "testdata/7/code.go:14 ErrFakeErrorExported is a global variable", - "testdata/7/code.go:17 myErrVar is a global variable", - "testdata/7/code.go:18 myVarErr is a global variable", - "testdata/7/code.go:19 myVarError is a global variable", - "testdata/7/code.go:20 customErr is a global variable", - "testdata/7/code.go:29 declaredErr is a global variable", - }, - }, - { - path: "testdata/8", - wantMessages: []string{ - "testdata/8/code.go:9 myVar is a global variable", - // TODO: "testdata/8/code.go:14 errFakeErrorUnexported is a global variable", - // TODO: "testdata/8/code.go:15 ErrFakeErrorExported is a global variable", - "testdata/8/code.go:18 myErrVar is a global variable", - "testdata/8/code.go:19 myVarErr is a global variable", - "testdata/8/code.go:20 myVarError is a global variable", - "testdata/8/code.go:21 customErr is a global variable", - "testdata/8/code.go:30 declaredErr is a global variable", - }, - }, - { - path: "testdata/9", - wantMessages: []string{ - "testdata/9/code.go:3 Version is a global variable", - "testdata/9/code.go:4 version22 is a global variable", - }, - }, - { - path: ".", - wantMessages: nil, - }, - { - path: "./...", - wantMessages: []string{ - "testdata/10/code.go:10 myVar is a global variable", - "testdata/10/code.go:33 HTTPClient is a global variable", - "testdata/2/code.go:3 myVar is a global variable", - "testdata/3/code_0.go:8 theVar is a global variable", - "testdata/3/code_1.go:3 myVar is a global variable", - "testdata/4/code.go:3 theVar is a global variable", - "testdata/4/subpkg/code_1.go:3 myVar is a global variable", - "testdata/5/code.go:3 myVar1 is a global variable", - "testdata/5/code.go:3 myVar2 is a global variable", - "testdata/7/code.go:8 myVar is a global variable", - // TODO: "testdata/7/code.go:13 errFakeErrorUnexported is a global variable", - // TODO: "testdata/7/code.go:14 ErrFakeErrorExported is a global variable", - "testdata/7/code.go:17 myErrVar is a global variable", - "testdata/7/code.go:18 myVarErr is a global variable", - "testdata/7/code.go:19 myVarError is a global variable", - "testdata/7/code.go:20 customErr is a global variable", - "testdata/7/code.go:29 declaredErr is a global variable", - "testdata/8/code.go:9 myVar is a global variable", - // TODO: "testdata/8/code.go:14 errFakeErrorUnexported is a global variable", - // TODO: "testdata/8/code.go:15 ErrFakeErrorExported is a global variable", - "testdata/8/code.go:18 myErrVar is a global variable", - "testdata/8/code.go:19 myVarErr is a global variable", - "testdata/8/code.go:20 myVarError is a global variable", - "testdata/8/code.go:21 customErr is a global variable", - "testdata/8/code.go:30 declaredErr is a global variable", - "testdata/9/code.go:3 Version is a global variable", - "testdata/9/code.go:4 version22 is a global variable", - }, - }, - } - - for _, c := range cases { - caseName := fmt.Sprintf("%s include tests: %t", c.path, c.includeTests) - t.Run(caseName, func(t *testing.T) { - messages, err := checkNoGlobals(c.path, c.includeTests) - if err != nil { - t.Fatalf("got error %#v", err) - } - if !stringSlicesEqual(messages, c.wantMessages) { - t.Errorf("got %#v, want %#v", messages, c.wantMessages) - } - }) - } -} - -func stringSlicesEqual(s1, s2 []string) bool { - diff := map[string]int{} - for _, s := range s1 { - diff[s]++ - } - for _, s := range s2 { - diff[s]-- - if diff[s] == 0 { - delete(diff, s) - } - } - return len(diff) == 0 -} diff --git a/check_no_globals.go b/checknoglobals/check_no_globals.go similarity index 55% rename from check_no_globals.go rename to checknoglobals/check_no_globals.go index a7c03d2..5b6325d 100644 --- a/check_no_globals.go +++ b/checknoglobals/check_no_globals.go @@ -1,13 +1,13 @@ -package main +package checknoglobals import ( + "flag" "fmt" "go/ast" - "go/parser" "go/token" - "os" - "path/filepath" "strings" + + "golang.org/x/tools/go/analysis" ) // allowedExpression is a struct representing packages and methods that will @@ -18,6 +18,36 @@ type allowedExpression struct { SelName string } +const Doc = `check that no global variables exist + +This analyzer checks for global variables and errors on any found. + +A global variable is a variable declared in package scope and that can be read +and written to by any function within the package. Global variables can cause +side effects which are difficult to keep track of. A code in one function may +change the variables state while another unrelated chunk of code may be +effected by it.` + +// Analyzer provides an Analyzer that checks that there are no global +// variables, except for errors and variables containing regular +// expressions. +func Analyzer() *analysis.Analyzer { + return &analysis.Analyzer{ + Name: "gochecknoglobals", + Doc: Doc, + Run: checkNoGlobals, + Flags: flags(), + RunDespiteErrors: true, + } +} + +func flags() flag.FlagSet { + flags := flag.NewFlagSet("", flag.ExitOnError) + flags.Bool("t", false, "Include tests") + + return *flags +} + func isAllowed(v ast.Node) bool { switch i := v.(type) { case *ast.Ident: @@ -66,37 +96,16 @@ func looksLikeError(i *ast.Ident) bool { return strings.HasPrefix(i.Name, prefix) } -func checkNoGlobals(rootPath string, includeTests bool) ([]string, error) { - const recursiveSuffix = string(filepath.Separator) + "..." - recursive := false - if strings.HasSuffix(rootPath, recursiveSuffix) { - recursive = true - rootPath = rootPath[:len(rootPath)-len(recursiveSuffix)] - } +func checkNoGlobals(pass *analysis.Pass) (interface{}, error) { + includeTests := pass.Analyzer.Flags.Lookup("t").Value.(flag.Getter).Get().(bool) - messages := []string{} - - err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() { - if !recursive && path != rootPath { - return filepath.SkipDir - } - return nil - } - if !strings.HasSuffix(path, ".go") { - return nil + for _, file := range pass.Files { + filename := pass.Fset.Position(file.Pos()).Filename + if !strings.HasSuffix(filename, ".go") { + continue } - if !includeTests && strings.HasSuffix(path, "_test.go") { - return nil - } - - fset := token.NewFileSet() - file, err := parser.ParseFile(fset, path, nil, 0) - if err != nil { - return err + if !includeTests && strings.HasSuffix(filename, "_test.go") { + continue } for _, decl := range file.Decls { @@ -107,7 +116,6 @@ func checkNoGlobals(rootPath string, includeTests bool) ([]string, error) { if genDecl.Tok != token.VAR { continue } - filename := fset.Position(genDecl.TokPos).Filename for _, spec := range genDecl.Specs { valueSpec := spec.(*ast.ValueSpec) onlyAllowedValues := false @@ -131,14 +139,16 @@ func checkNoGlobals(rootPath string, includeTests bool) ([]string, error) { continue } - line := fset.Position(vn.Pos()).Line - message := fmt.Sprintf("%s:%d %s is a global variable", filename, line, vn.Name) - messages = append(messages, message) + message := fmt.Sprintf("%s is a global variable", vn.Name) + pass.Report(analysis.Diagnostic{ + Pos: vn.Pos(), + Category: "global", + Message: message, + }) } } } - return nil - }) + } - return messages, err + return nil, nil } diff --git a/checknoglobals/check_no_globals_test.go b/checknoglobals/check_no_globals_test.go new file mode 100644 index 0000000..87b0685 --- /dev/null +++ b/checknoglobals/check_no_globals_test.go @@ -0,0 +1,25 @@ +package checknoglobals + +import ( + "flag" + "strconv" + "testing" + + "golang.org/x/tools/go/analysis/analysistest" +) + +func TestCheckNoGlobals(t *testing.T) { + testdata := analysistest.TestData() + flags := flag.NewFlagSet("", flag.ExitOnError) + flags.Bool("t", true, "") + + analyzer := Analyzer() + analyzer.Flags = *flags + + for i := 0; i <= 10; i++ { + dir := strconv.Itoa(i) + t.Run(dir, func(t *testing.T) { + analysistest.Run(t, testdata, analyzer, dir) + }) + } +} diff --git a/testdata/0/code.go b/checknoglobals/testdata/src/0/code.go similarity index 100% rename from testdata/0/code.go rename to checknoglobals/testdata/src/0/code.go diff --git a/testdata/1/code.go b/checknoglobals/testdata/src/1/code.go similarity index 100% rename from testdata/1/code.go rename to checknoglobals/testdata/src/1/code.go diff --git a/testdata/1/code_test.go b/checknoglobals/testdata/src/1/code_test.go similarity index 100% rename from testdata/1/code_test.go rename to checknoglobals/testdata/src/1/code_test.go diff --git a/testdata/10/code.go b/checknoglobals/testdata/src/10/code.go similarity index 86% rename from testdata/10/code.go rename to checknoglobals/testdata/src/10/code.go index ded2886..ec08b68 100644 --- a/testdata/10/code.go +++ b/checknoglobals/testdata/src/10/code.go @@ -7,7 +7,7 @@ import ( ) // myVar is just a bad named global var. -var myVar = 1 +var myVar = 1 // want "myVar is a global variable" // ErrNotFound is an error and should be OK. var ErrNotFound = errors.New("this is error") @@ -30,5 +30,5 @@ var ( PrecompileFour = regexp.MustCompile(`[a-z]{1,3}`) PrecompileFive = regexp.MustCompile(`[a-z]{3,6}`) PrecompileSix = regexp.MustCompile(`[a-z]{6,9}`) - HTTPClient = http.Client{} + HTTPClient = http.Client{} // want "HTTPClient is a global variable" ) diff --git a/checknoglobals/testdata/src/2/code.go b/checknoglobals/testdata/src/2/code.go new file mode 100644 index 0000000..b77b361 --- /dev/null +++ b/checknoglobals/testdata/src/2/code.go @@ -0,0 +1,3 @@ +package code + +var myVar = 0 // want "myVar is a global variable" diff --git a/checknoglobals/testdata/src/2/code_test.go b/checknoglobals/testdata/src/2/code_test.go new file mode 100644 index 0000000..2b0de36 --- /dev/null +++ b/checknoglobals/testdata/src/2/code_test.go @@ -0,0 +1,3 @@ +package code_test + +var myTestVar = 0 // want "myTestVar is a global variable" diff --git a/testdata/3/code_0.go b/checknoglobals/testdata/src/3/code_0.go similarity index 56% rename from testdata/3/code_0.go rename to checknoglobals/testdata/src/3/code_0.go index 0a11808..023e08f 100644 --- a/testdata/3/code_0.go +++ b/checknoglobals/testdata/src/3/code_0.go @@ -5,4 +5,4 @@ func someCode() bool { return yourVar } -var theVar = true +var theVar = true // want "theVar is a global variable" diff --git a/checknoglobals/testdata/src/3/code_1.go b/checknoglobals/testdata/src/3/code_1.go new file mode 100644 index 0000000..a414b94 --- /dev/null +++ b/checknoglobals/testdata/src/3/code_1.go @@ -0,0 +1,3 @@ +package code + +var myVar = "global" // want "myVar is a global variable" diff --git a/checknoglobals/testdata/src/4/code.go b/checknoglobals/testdata/src/4/code.go new file mode 100644 index 0000000..8aea458 --- /dev/null +++ b/checknoglobals/testdata/src/4/code.go @@ -0,0 +1,3 @@ +package code + +var theVar = 2 // want "theVar is a global variable" diff --git a/testdata/4/subpkg/code_0.go b/checknoglobals/testdata/src/4/subpkg/code_0.go similarity index 100% rename from testdata/4/subpkg/code_0.go rename to checknoglobals/testdata/src/4/subpkg/code_0.go diff --git a/checknoglobals/testdata/src/4/subpkg/code_1.go b/checknoglobals/testdata/src/4/subpkg/code_1.go new file mode 100644 index 0000000..a414b94 --- /dev/null +++ b/checknoglobals/testdata/src/4/subpkg/code_1.go @@ -0,0 +1,3 @@ +package code + +var myVar = "global" // want "myVar is a global variable" diff --git a/checknoglobals/testdata/src/5/code.go b/checknoglobals/testdata/src/5/code.go new file mode 100644 index 0000000..c2f2e67 --- /dev/null +++ b/checknoglobals/testdata/src/5/code.go @@ -0,0 +1,3 @@ +package code + +var myVar1, myVar2 = 1, 2 // want "myVar1 is a global variable" "myVar2 is a global variable" diff --git a/testdata/6/code.go b/checknoglobals/testdata/src/6/code.go similarity index 100% rename from testdata/6/code.go rename to checknoglobals/testdata/src/6/code.go diff --git a/testdata/7/code.go b/checknoglobals/testdata/src/7/code.go similarity index 62% rename from testdata/7/code.go rename to checknoglobals/testdata/src/7/code.go index d30dbed..be3d623 100644 --- a/testdata/7/code.go +++ b/checknoglobals/testdata/src/7/code.go @@ -5,7 +5,7 @@ import ( ) // Those are not errors -var myVar = 1 +var myVar = 1 // want "myVar is a global variable" // Those are fake errors which are currently not detected // because they start with 'err' or 'Err' and we don't @@ -14,10 +14,10 @@ var errFakeErrorUnexported = 1 var ErrFakeErrorExported = 1 // Those errors are not named correctly -var myErrVar = errors.New("myErrVar") -var myVarErr = errors.New("myVarErr") -var myVarError = errors.New("myVarErr") -var customErr = customError{"customErr"} +var myErrVar = errors.New("myErrVar") // want "myErrVar is a global variable" +var myVarErr = errors.New("myVarErr") // want "myVarErr is a global variable" +var myVarError = errors.New("myVarErr") // want "myVarError is a global variable" +var customErr = customError{"customErr"} // want "customErr is a global variable" // Those are actual errors which should be ignored var errUnexported = errors.New("errUnexported") @@ -26,7 +26,7 @@ var errCustomUnexported = customError{"errCustomUnexported"} var ErrCustomExported = customError{"ErrCustomExported"} // Those actual errors have a declared error type -var declaredErr error = errors.New("declaredErr") +var declaredErr error = errors.New("declaredErr") // want "declaredErr is a global variable" var errDeclared error = errors.New("errDeclared") type customError struct{ e string } diff --git a/testdata/8/code.go b/checknoglobals/testdata/src/8/code.go similarity index 63% rename from testdata/8/code.go rename to checknoglobals/testdata/src/8/code.go index 8accb90..a0472c5 100644 --- a/testdata/8/code.go +++ b/checknoglobals/testdata/src/8/code.go @@ -6,7 +6,7 @@ import ( var ( // Those are not errors - myVar = 1 + myVar = 1 // want "myVar is a global variable" // Those are fake errors which are currently not detected // because they start with 'err' or 'Err' and we don't @@ -15,10 +15,10 @@ var ( ErrFakeErrorExported = 1 // Those errors are not named correctly - myErrVar = errors.New("myErrVar") - myVarErr = errors.New("myVarErr") - myVarError = errors.New("myVarErr") - customErr = customError{"customErr"} + myErrVar = errors.New("myErrVar") // want "myErrVar is a global variable" + myVarErr = errors.New("myVarErr") // want "myVarErr is a global variable" + myVarError = errors.New("myVarErr") // want "myVarError is a global variable" + customErr = customError{"customErr"} // want "customErr is a global variable" // Those are actual errors which should be ignored errUnexported = errors.New("errUnexported") @@ -27,7 +27,7 @@ var ( ErrCustomExported = customError{"ErrCustomExported"} // Those actual errors have a declared error type - declaredErr error = errors.New("declaredErr") + declaredErr error = errors.New("declaredErr") // want "declaredErr is a global variable" errDeclared error = errors.New("errDeclared") ) diff --git a/checknoglobals/testdata/src/9/code.go b/checknoglobals/testdata/src/9/code.go new file mode 100644 index 0000000..ca05cda --- /dev/null +++ b/checknoglobals/testdata/src/9/code.go @@ -0,0 +1,5 @@ +package code + +var Version string // want "Version is a global variable" +var version22 string // want "version22 is a global variable" +var version string diff --git a/go.mod b/go.mod index a8a0df8..0848399 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module 4d63.com/gochecknoglobals go 1.15 + +require golang.org/x/tools v0.0.0-20201002184944-ecd9fd270d5d diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a8e5cd3 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201002184944-ecd9fd270d5d h1:vWQvJ/Z0Lu+9/8oQ/pAYXNzbc7CMnBl+tULGVHOy3oE= +golang.org/x/tools v0.0.0-20201002184944-ecd9fd270d5d/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 57d2d53..a6009db 100644 --- a/main.go +++ b/main.go @@ -1,47 +1,16 @@ package main // import "4d63.com/gochecknoglobals" import ( - "flag" - "fmt" "os" + + "4d63.com/gochecknoglobals/checknoglobals" + "golang.org/x/tools/go/analysis/singlechecker" ) func main() { - flagPrintHelp := flag.Bool("h", false, "Print help") - flagIncludeTests := flag.Bool("t", false, "Include tests") - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: gochecknoglobals [-t] [path] [path] ...\n") - flag.PrintDefaults() - } - flag.Parse() - - if *flagPrintHelp { - flag.Usage() - return - } - - includeTests := *flagIncludeTests - - paths := flag.Args() - if len(paths) == 0 { - paths = []string{"./..."} + if len(os.Args) == 1 { + os.Args = append(os.Args, "./...") } - exitWithError := false - - for _, path := range paths { - messages, err := checkNoGlobals(path, includeTests) - for _, message := range messages { - fmt.Fprintf(os.Stdout, "%s\n", message) - exitWithError = true - } - if err != nil { - fmt.Fprintf(os.Stderr, "error: %s\n", err) - exitWithError = true - } - } - - if exitWithError { - os.Exit(1) - } + singlechecker.Main(checknoglobals.Analyzer()) } diff --git a/testdata/2/code.go b/testdata/2/code.go deleted file mode 100644 index e2adbde..0000000 --- a/testdata/2/code.go +++ /dev/null @@ -1,3 +0,0 @@ -package code - -var myVar = 0 diff --git a/testdata/2/code_test.go b/testdata/2/code_test.go deleted file mode 100644 index d5060a8..0000000 --- a/testdata/2/code_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package code_test - -var myTestVar = 0 diff --git a/testdata/3/code_1.go b/testdata/3/code_1.go deleted file mode 100644 index 9c14608..0000000 --- a/testdata/3/code_1.go +++ /dev/null @@ -1,3 +0,0 @@ -package code - -var myVar = "global" diff --git a/testdata/4/code.go b/testdata/4/code.go deleted file mode 100644 index 4be135f..0000000 --- a/testdata/4/code.go +++ /dev/null @@ -1,3 +0,0 @@ -package code - -var theVar = 2 diff --git a/testdata/4/subpkg/code_1.go b/testdata/4/subpkg/code_1.go deleted file mode 100644 index 9c14608..0000000 --- a/testdata/4/subpkg/code_1.go +++ /dev/null @@ -1,3 +0,0 @@ -package code - -var myVar = "global" diff --git a/testdata/5/code.go b/testdata/5/code.go deleted file mode 100644 index c47adf0..0000000 --- a/testdata/5/code.go +++ /dev/null @@ -1,3 +0,0 @@ -package code - -var myVar1, myVar2 = 1, 2 diff --git a/testdata/9/code.go b/testdata/9/code.go deleted file mode 100644 index 11e95e2..0000000 --- a/testdata/9/code.go +++ /dev/null @@ -1,5 +0,0 @@ -package code - -var Version string -var version22 string -var version string