Skip to content

Commit 5cae67a

Browse files
authored
Handle args (#41)
* add new testcase Signed-off-by: sivchari <[email protected]> * handle arguments Signed-off-by: sivchari <[email protected]> --------- Signed-off-by: sivchari <[email protected]>
1 parent 6833cd3 commit 5cae67a

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

tenv.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName st
9292
if !ok {
9393
return false
9494
}
95+
checkArgs(pass, callExpr.Args, funcName, argName)
9596
fun, ok := callExpr.Fun.(*ast.SelectorExpr)
9697
if !ok {
9798
return false
@@ -110,6 +111,30 @@ func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName st
110111
return true
111112
}
112113

114+
func checkArgs(pass *analysis.Pass, args []ast.Expr, funcName, argName string) {
115+
for _, arg := range args {
116+
callExpr, ok := arg.(*ast.CallExpr)
117+
if !ok {
118+
continue
119+
}
120+
fun, ok := callExpr.Fun.(*ast.SelectorExpr)
121+
if !ok {
122+
continue
123+
}
124+
x, ok := fun.X.(*ast.Ident)
125+
if !ok {
126+
continue
127+
}
128+
targetName := x.Name + "." + fun.Sel.Name
129+
if targetName == "os.Setenv" {
130+
if argName == "" {
131+
argName = "testing"
132+
}
133+
pass.Reportf(arg.Pos(), "os.Setenv() can be replaced by `%s.Setenv()` in %s", argName, funcName)
134+
}
135+
}
136+
}
137+
113138
func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string) bool {
114139
assignStmt, ok := stmt.Init.(*ast.AssignStmt)
115140
if !ok {

testdata/src/a/a_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"testing"
7+
8+
"github.com/stretchr/testify/require"
79
)
810

911
var (
@@ -95,3 +97,7 @@ func TestLoop(t *testing.T) {
9597
}
9698
}
9799
}
100+
101+
func TestUsingArg(t *testing.T) {
102+
require.NoError(t, os.Setenv("a", "b")) // want "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestUsingArg"
103+
}

testdata/src/a/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module a
22

33
go 1.22.3
4+
5+
require github.com/stretchr/testify v1.9.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

testdata/src/a/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)