Skip to content

go/analysis/passes/initflagparse: add check for avoid flag.Parse at init #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions go/analysis/passes/initflagparse/initflagparse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package initflagparse defines an Analyzer that checks for invalid
// usages of flag.Parse during package initialization
package initflagparse

import (
"go/ast"
"go/types"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const Doc = `check for calls to flag.Parse within packages init

The initflagparse analyzer reports incorrect calls to flag.Parse within
the init of the packages.`

var Analyzer = &analysis.Analyzer{
Name: "initflagparse",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
x := n.(*ast.FuncDecl)
if x.Recv == nil && x.Name.Name == "init" {
checkForFlagParse(pass, x)
}
})
return nil, nil
}

// checkForFlagParse check the code inside a node and fail if it finds a
// flag.Parse call
func checkForFlagParse(pass *analysis.Pass, x ast.Node) {
ast.Inspect(x, func(n ast.Node) bool {
x, ok := n.(*ast.CallExpr)
if !ok {
return true
}
fun, ok := x.Fun.(*ast.SelectorExpr)
if !ok {
return true
}

module, ok := fun.X.(*ast.Ident)
if !ok {
return true
}

obj := pass.TypesInfo.ObjectOf(module)
if obj == nil {
return true
}

pkgObj, ok := obj.(*types.PkgName)
if !ok || pkgObj.Imported() == nil {
return true
}

if ok && obj != nil && pkgObj.Imported().Name() == "flag" && fun.Sel.Name == "Parse" {
pass.ReportRangef(x, "flag.Parse call within package initialization")
return false
}
return true
})
}
18 changes: 18 additions & 0 deletions go/analysis/passes/initflagparse/initflagparse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package initflagparse_test

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/initflagparse"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, initflagparse.Analyzer, "a")
analysistest.Run(t, testdata, initflagparse.Analyzer, "b")
}
21 changes: 21 additions & 0 deletions go/analysis/passes/initflagparse/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package a

import "flag"

func init() {
flag.Parse() // want `flag.Parse call within package initialization`
}

type Test struct{}

func (_ *Test) init() {
flag.Parse()
}

func main() {
flag.Parse()
}
21 changes: 21 additions & 0 deletions go/analysis/passes/initflagparse/testdata/src/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package b

import x "flag"

func init() {
x.Parse() // want `flag.Parse call within package initialization`
}

type Test struct{}

func (_ *Test) init() {
x.Parse()
}

func main() {
x.Parse()
}
2 changes: 2 additions & 0 deletions go/analysis/unitchecker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"golang.org/x/tools/go/analysis/passes/copylock"
"golang.org/x/tools/go/analysis/passes/errorsas"
"golang.org/x/tools/go/analysis/passes/httpresponse"
"golang.org/x/tools/go/analysis/passes/initflagparse"
"golang.org/x/tools/go/analysis/passes/loopclosure"
"golang.org/x/tools/go/analysis/passes/lostcancel"
"golang.org/x/tools/go/analysis/passes/nilfunc"
Expand Down Expand Up @@ -62,5 +63,6 @@ func main() {
unreachable.Analyzer,
unsafeptr.Analyzer,
unusedresult.Analyzer,
initflagparse.Analyzer,
)
}