From 26727b84d92db09843175a945b93ad46ff7d0a53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Mon, 11 Jun 2018 09:34:57 +0100 Subject: [PATCH] cmd/vet: simplify side effects func call logic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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í Reviewed-by: Robert Griesemer --- src/cmd/vet/bool.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/cmd/vet/bool.go b/src/cmd/vet/bool.go index e8b73175aa..1cd477f988 100644 --- a/src/cmd/vet/bool.go +++ b/src/cmd/vet/bool.go @@ -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 -- 2.50.0