Skip to content

Commit

Permalink
cleanup code (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: sivchari <[email protected]>
  • Loading branch information
sivchari authored Oct 28, 2024
1 parent b69ab3c commit 1ea85c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions tenv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tenv

import (
"fmt"
"go/ast"
"go/token"
"go/types"
Expand Down Expand Up @@ -73,40 +72,33 @@ func checkStmts(pass *analysis.Pass, stmts []ast.Stmt, funcName, argName string)
for _, stmt := range stmts {
switch stmt := stmt.(type) {
case *ast.ExprStmt:
if !checkExprStmt(pass, stmt, funcName, argName) {
continue
}
checkExprStmt(pass, stmt, funcName, argName)
case *ast.IfStmt:
if !checkIfStmt(pass, stmt, funcName, argName) {
continue
}
checkIfStmt(pass, stmt, funcName, argName)
case *ast.AssignStmt:
if !checkAssignStmt(pass, stmt, funcName, argName) {
continue
}
checkAssignStmt(pass, stmt, funcName, argName)
case *ast.ForStmt:
checkForStmt(pass, stmt, funcName, argName)
}
}
}

func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName string) bool {
func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName string) {
callExpr, ok := stmt.X.(*ast.CallExpr)
if !ok {
return false
return
}
checkArgs(pass, callExpr.Args, funcName, argName)
ident, ok := callExpr.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := callExpr.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
return false
}

func checkArgs(pass *analysis.Pass, args []ast.Expr, funcName, argName string) {
Expand All @@ -128,47 +120,45 @@ func checkArgs(pass *analysis.Pass, args []ast.Expr, funcName, argName string) {
}
}

func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string) bool {
func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string) {
assignStmt, ok := stmt.Init.(*ast.AssignStmt)
if !ok {
return false
return
}
rhs, ok := assignStmt.Rhs[0].(*ast.CallExpr)
if !ok {
return false
return
}
ident, ok := rhs.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := rhs.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
return false
}

func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, funcName, argName string) bool {
func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, funcName, argName string) {
rhs, ok := stmt.Rhs[0].(*ast.CallExpr)
if !ok {
return false
return
}
ident, ok := rhs.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := rhs.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
return checkObj(pass, obj, stmt.Pos(), funcName, argName)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
return false
}

func checkObj(pass *analysis.Pass, obj types.Object, pos token.Pos, funcName, argName string) bool {
func checkObj(pass *analysis.Pass, obj types.Object, pos token.Pos, funcName, argName string) {
// For built-in objects, obj.Pkg() returns nil.
var pkgPrefix string
if pkg := obj.Pkg(); pkg != nil {
Expand All @@ -180,10 +170,8 @@ func checkObj(pass *analysis.Pass, obj types.Object, pos token.Pos, funcName, ar
if argName == "" {
argName = "testing"
}
fmt.Println(argName, funcName)
pass.Reportf(pos, "os.Setenv() can be replaced by `%s.Setenv()` in %s", argName, funcName)
}
return true
}

func checkForStmt(pass *analysis.Pass, stmt *ast.ForStmt, funcName, argName string) {
Expand Down
Empty file removed testdata/src/regression/go.sum
Empty file.

0 comments on commit 1ea85c7

Please sign in to comment.