Last active
September 10, 2018 08:35
-
-
Save detailyang/3c8fc54e1a84f8e547b3a5583845e5dc to your computer and use it in GitHub Desktop.
copernicus find error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"go/ast" | |
"go/parser" | |
"go/token" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func checkerr(filename string) { | |
fset := token.NewFileSet() | |
f, err := parser.ParseFile(fset, filename, nil, 0) | |
if err != nil { | |
panic(err) | |
} | |
ast.Inspect(f, func(n ast.Node) bool { | |
switch ifs := n.(type) { | |
case *ast.IfStmt: | |
for j := 0; j < len(ifs.Body.List); j++ { | |
stmt := ifs.Body.List[j] | |
es, ok := stmt.(*ast.ExprStmt) | |
if !ok { | |
continue | |
} | |
ce, ok := es.X.(*ast.CallExpr) | |
if !ok { | |
continue | |
} | |
se, ok := ce.Fun.(*ast.SelectorExpr) | |
if !ok { | |
continue | |
} | |
if ident, ok := se.X.(*ast.Ident); ok { | |
if ident.Name == "log" && se.Sel.Name == "Error" { | |
if j+1 >= len(ifs.Body.List) { | |
fmt.Println(fset.Position(stmt.Pos())) | |
break | |
} | |
stmt = ifs.Body.List[len(ifs.Body.List)-1] | |
switch n := stmt.(type) { | |
case *ast.ExprStmt: | |
if ce, ok := n.X.(*ast.CallExpr); !ok { | |
fmt.Println(fset.Position(stmt.Pos())) | |
} else { | |
if ident, ok := ce.Fun.(*ast.Ident); ok { | |
if ident.Name == "panic" { | |
break | |
} | |
} | |
} | |
case *ast.BranchStmt: | |
case *ast.ReturnStmt: | |
default: | |
fmt.Println(fset.Position(stmt.Pos())) | |
} | |
break | |
} | |
} | |
} | |
} | |
return true | |
}) | |
} | |
func visit(path string, f os.FileInfo, err error) error { | |
if err != nil { | |
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) | |
return err | |
} | |
if !strings.HasSuffix(path, "go") || strings.Contains(path, "vendor") { | |
return nil | |
} | |
if f.IsDir() { | |
return nil | |
} | |
checkerr(path) | |
return nil | |
} | |
func main() { | |
flag.Parse() | |
root := flag.Arg(0) | |
filepath.Walk(root, visit) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment