Skip to content

Commit d8ea6ba

Browse files
committed
update linter
1 parent 1f85886 commit d8ea6ba

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

tenv.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,22 @@ func run(pass *analysis.Pass) (interface{}, error) {
4040
inspect.Preorder(nodeFilter, func(n ast.Node) {
4141
switch n := n.(type) {
4242
case *ast.File:
43-
if strings.HasSuffix(pass.Fset.File(n.Pos()).Name(), "_test.go") {
44-
for _, decl := range n.Decls {
45-
funcDecl, ok := decl.(*ast.FuncDecl)
46-
if !ok {
47-
continue
48-
}
49-
checkFunc(pass, funcDecl)
43+
for _, decl := range n.Decls {
44+
45+
funcDecl, ok := decl.(*ast.FuncDecl)
46+
if !ok {
47+
continue
5048
}
49+
checkFunc(pass, funcDecl, pass.Fset.File(n.Pos()).Name())
5150
}
5251
}
5352
})
5453

5554
return nil, nil
5655
}
5756

58-
func checkFunc(pass *analysis.Pass, n *ast.FuncDecl) {
59-
argName, ok := targetRunner(n)
57+
func checkFunc(pass *analysis.Pass, n *ast.FuncDecl, fileName string) {
58+
argName, ok := targetRunner(n, fileName)
6059
if ok {
6160
for _, stmt := range n.Body.List {
6261
switch stmt := stmt.(type) {
@@ -150,7 +149,7 @@ func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, n *ast.FuncDecl,
150149
return true
151150
}
152151

153-
func targetRunner(funcDecl *ast.FuncDecl) (string, bool) {
152+
func targetRunner(funcDecl *ast.FuncDecl, fileName string) (string, bool) {
154153
params := funcDecl.Type.Params.List
155154
for _, p := range params {
156155
switch typ := p.Type.(type) {
@@ -166,7 +165,7 @@ func targetRunner(funcDecl *ast.FuncDecl) (string, bool) {
166165
}
167166
}
168167
}
169-
if aflag {
168+
if aflag && strings.HasSuffix(fileName, "_test.go") {
170169
return "", true
171170
}
172171
return "", false

testdata/src/a/a.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package a
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
var (
9+
ee = os.Setenv("a", "b") // never seen
10+
)
11+
12+
func setup() {
13+
os.Setenv("a", "b") // never seen
14+
err := os.Setenv("a", "b") // never seen
15+
if err != nil {
16+
_ = err
17+
}
18+
os.Setenv("a", "b") // never seen
19+
}
20+
21+
func F(t *testing.T) {
22+
setup()
23+
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in F"
24+
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in F"
25+
_ = err
26+
if err := os.Setenv("a", "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in F"
27+
_ = err
28+
}
29+
}
30+
31+
func BF(b *testing.B) {
32+
TBF(b)
33+
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BF"
34+
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BF"
35+
_ = err
36+
if err := os.Setenv("a", "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BF"
37+
_ = err
38+
}
39+
}
40+
41+
func TBF(tb testing.TB) {
42+
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TBF"
43+
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TBF"
44+
_ = err
45+
if err := os.Setenv("a", "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TBF"
46+
_ = err
47+
}
48+
}

testdata/src/a/a_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ var (
99
e = os.Setenv("a", "b") // never seen
1010
)
1111

12-
func setup() {
13-
os.Setenv("a", "b") // if -all = true, want "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in setup"
14-
err := os.Setenv("a", "b") // if -all = true, want "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in setup"
12+
func testsetup() {
13+
os.Setenv("a", "b") // if -all = true, want "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in testsetup"
14+
err := os.Setenv("a", "b") // if -all = true, want "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in testsetup"
1515
if err != nil {
1616
_ = err
1717
}
1818
os.Setenv("a", "b") // if -all = true, "func setup is not using testing.Setenv"
1919
}
2020

2121
func TestF(t *testing.T) {
22-
setup()
22+
testsetup()
2323
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF"
2424
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF"
2525
_ = err
@@ -29,7 +29,7 @@ func TestF(t *testing.T) {
2929
}
3030

3131
func BenchmarkF(b *testing.B) {
32-
testTB(b)
32+
TB(b)
3333
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF"
3434
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF"
3535
_ = err
@@ -38,6 +38,11 @@ func BenchmarkF(b *testing.B) {
3838
}
3939
}
4040

41-
func testTB(tb testing.TB) {
42-
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB"
41+
func TB(tb testing.TB) {
42+
os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TB"
43+
err := os.Setenv("a", "b") // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TB"
44+
_ = err
45+
if err := os.Setenv("a", "b"); err != nil { // want "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in TB"
46+
_ = err
47+
}
4348
}

0 commit comments

Comments
 (0)