From a3eb352eedfc310776f419d87dc382624564532c Mon Sep 17 00:00:00 2001 From: "Iskander (Alex) Sharipov" Date: Mon, 10 Dec 2018 12:04:46 +0300 Subject: [PATCH] astwalk: use typep package instead of lintutil (#60) Remove code duplication. We already have IsTypeExpr inside typep package. Signed-off-by: Iskander Sharipov --- astwalk/type_expr_walker.go | 8 ++++---- internal/lintutil/lintutil.go | 37 ----------------------------------- 2 files changed, 4 insertions(+), 41 deletions(-) delete mode 100644 internal/lintutil/lintutil.go diff --git a/astwalk/type_expr_walker.go b/astwalk/type_expr_walker.go index b80ad5b..24c1500 100644 --- a/astwalk/type_expr_walker.go +++ b/astwalk/type_expr_walker.go @@ -5,8 +5,8 @@ import ( "go/token" "go/types" - "github.com/go-lintpack/lintpack/internal/lintutil" "github.com/go-toolsmith/astp" + "github.com/go-toolsmith/typep" ) type typeExprWalker struct { @@ -49,7 +49,7 @@ func (w *typeExprWalker) visit(x ast.Expr) bool { func (w *typeExprWalker) walk(x ast.Node) bool { switch x := x.(type) { case *ast.ParenExpr: - if lintutil.IsTypeExpr(w.info, x.X) { + if typep.IsTypeExpr(w.info, x.X) { return w.visit(x) } return true @@ -63,7 +63,7 @@ func (w *typeExprWalker) walk(x ast.Node) bool { // Like with conversions, method expressions are another special. return w.inspectInner(x.X) case *ast.StarExpr: - if lintutil.IsTypeExpr(w.info, x.X) { + if typep.IsTypeExpr(w.info, x.X) { return w.visit(x) } return true @@ -95,7 +95,7 @@ func (w *typeExprWalker) walk(x ast.Node) bool { func (w *typeExprWalker) inspectInner(x ast.Expr) bool { parens, ok := x.(*ast.ParenExpr) - if ok && lintutil.IsTypeExpr(w.info, parens.X) && astp.IsStarExpr(parens.X) { + if ok && typep.IsTypeExpr(w.info, parens.X) && astp.IsStarExpr(parens.X) { ast.Inspect(parens.X, w.walk) return false } diff --git a/internal/lintutil/lintutil.go b/internal/lintutil/lintutil.go deleted file mode 100644 index 333543b..0000000 --- a/internal/lintutil/lintutil.go +++ /dev/null @@ -1,37 +0,0 @@ -package lintutil - -import ( - "go/ast" - "go/types" -) - -// TODO: this package is a way to reuse code between lint and astwalk. -// Would be good to find it a better name. - -// IsTypeExpr reports whether x represents type expression. -// -// Type expression does not evaluate to any run time value, -// but rather describes type that is used inside Go expression. -// For example, (*T)(v) is a CallExpr that "calls" (*T). -// (*T) is a type expression that tells Go compiler type v should be converted to. -func IsTypeExpr(info *types.Info, x ast.Expr) bool { - switch x := x.(type) { - case *ast.StarExpr: - return IsTypeExpr(info, x.X) - case *ast.ParenExpr: - return IsTypeExpr(info, x.X) - case *ast.SelectorExpr: - return IsTypeExpr(info, x.Sel) - case *ast.Ident: - // Identifier may be a type expression if object - // it reffers to is a type name. - _, ok := info.ObjectOf(x).(*types.TypeName) - return ok - - case *ast.FuncType, *ast.StructType, *ast.InterfaceType, *ast.ArrayType, *ast.MapType: - return true - - default: - return false - } -}