]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: simplify side effects func call logic
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 11 Jun 2018 08:34:57 +0000 (09:34 +0100)
committerRobert Griesemer <gri@golang.org>
Thu, 14 Jun 2018 21:37:12 +0000 (21:37 +0000)
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: Robert Griesemer <gri@golang.org>
src/cmd/vet/bool.go

index e8b73175aaee135738e4479923c996400926d689..1cd477f988caf09a9f7897136b323a815da0afa3 100644 (file)
@@ -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