Skip to content

Commit 9863617

Browse files
committed
Improve wording and add more reliable package check
1 parent 26c541c commit 9863617

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

go/analysis/passes/initflagparse/initflagparse.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ package initflagparse
88

99
import (
1010
"go/ast"
11+
"go/types"
1112

1213
"golang.org/x/tools/go/analysis"
1314
"golang.org/x/tools/go/analysis/passes/inspect"
1415
"golang.org/x/tools/go/ast/inspector"
1516
)
1617

17-
const Doc = `check for usage of flag.Parse during modules init
18+
const Doc = `check for calls to flag.Parse within packages init
1819
19-
The initflagparse analyzer reports incorrect uses of flag.Parse during
20-
the init of the modules.`
20+
The initflagparse analyzer reports incorrect calls to flag.Parse within
21+
the init of the packages.`
2122

2223
var Analyzer = &analysis.Analyzer{
2324
Name: "initflagparse",
@@ -59,8 +60,18 @@ func checkForFlagParse(pass *analysis.Pass, x ast.Node) {
5960
return true
6061
}
6162

62-
if ok && module.Name == "flag" && fun.Sel.Name == "Parse" {
63-
pass.ReportRangef(x, "flag.Parse during package initialization")
63+
obj := pass.TypesInfo.ObjectOf(module)
64+
if obj == nil {
65+
return true
66+
}
67+
68+
pkgObj, ok := obj.(*types.PkgName)
69+
if !ok || pkgObj.Imported() == nil {
70+
return true
71+
}
72+
73+
if ok && obj != nil && pkgObj.Imported().Name() == "flag" && fun.Sel.Name == "Parse" {
74+
pass.ReportRangef(x, "flag.Parse call within package initialization")
6475
return false
6576
}
6677
return true

go/analysis/passes/initflagparse/initflagparse_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ import (
1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
1616
analysistest.Run(t, testdata, initflagparse.Analyzer, "a")
17+
analysistest.Run(t, testdata, initflagparse.Analyzer, "b")
1718
}

go/analysis/passes/initflagparse/testdata/src/a/a.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package a
77
import "flag"
88

99
func init() {
10-
flag.Parse() // want `flag.Parse usage during module initialization`
10+
flag.Parse() // want `flag.Parse call within package initialization`
1111
}
1212

1313
type Test struct{}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import x "flag"
8+
9+
func init() {
10+
x.Parse() // want `flag.Parse call within package initialization`
11+
}
12+
13+
type Test struct{}
14+
15+
func (_ *Test) init() {
16+
x.Parse()
17+
}
18+
19+
func main() {
20+
x.Parse()
21+
}

0 commit comments

Comments
 (0)