Commit 26727b84 authored by Daniel Martí's avatar Daniel Martí Committed by Robert Griesemer

cmd/vet: simplify side effects func call logic

Instead of first looking for values of unnamed signature type, first
treat the types and builtins. All the remaining cases will be what we're
after.

Change-Id: I328e22ae0be1cccaeb45ed4ddaa360233d447e7e
Reviewed-on: https://go-review.googlesource.com/117835
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent d31cad7c
......@@ -9,7 +9,6 @@ package main
import (
"go/ast"
"go/token"
"go/types"
)
func init() {
......@@ -142,28 +141,22 @@ func hasSideEffects(f *File, e ast.Expr) bool {
ast.Inspect(e, func(node ast.Node) bool {
switch n := node.(type) {
case *ast.CallExpr:
// Don't call Type.Underlying(), since its lack
// lets us see the NamedFuncType(x) type
// conversion as a *types.Named.
typVal := f.pkg.types[n.Fun]
_, isSig := typVal.Type.(*types.Signature)
switch {
case typVal.IsValue() && isSig:
// If we have a value of unnamed signature type,
// this CallExpr is a non-builtin func call and
// not a type conversion. Conservatively assume
// that all function and method calls have side
// effects for now.
case typVal.IsType():
// Type conversion, which is safe.
case typVal.IsBuiltin():
// Builtin func, conservatively assumed to not
// be safe for now.
safe = false
return false
case typVal.IsBuiltin():
// For now, conservatively assume that all
// built-in functions have side effects.
default:
// A non-builtin func or method call.
// Conservatively assume that all of them have
// side effects for now.
safe = false
return false
}
// It's a type conversion, which cannot
// have side effects.
case *ast.UnaryExpr:
if n.Op == token.ARROW {
safe = false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment