Skip to content

Commit 26727b8

Browse files
mvdangriesemer
authored andcommitted
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í <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent d31cad7 commit 26727b8

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/cmd/vet/bool.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package main
99
import (
1010
"go/ast"
1111
"go/token"
12-
"go/types"
1312
)
1413

1514
func init() {
@@ -142,28 +141,22 @@ func hasSideEffects(f *File, e ast.Expr) bool {
142141
ast.Inspect(e, func(node ast.Node) bool {
143142
switch n := node.(type) {
144143
case *ast.CallExpr:
145-
// Don't call Type.Underlying(), since its lack
146-
// lets us see the NamedFuncType(x) type
147-
// conversion as a *types.Named.
148144
typVal := f.pkg.types[n.Fun]
149-
_, isSig := typVal.Type.(*types.Signature)
150145
switch {
151-
case typVal.IsValue() && isSig:
152-
// If we have a value of unnamed signature type,
153-
// this CallExpr is a non-builtin func call and
154-
// not a type conversion. Conservatively assume
155-
// that all function and method calls have side
156-
// effects for now.
146+
case typVal.IsType():
147+
// Type conversion, which is safe.
148+
case typVal.IsBuiltin():
149+
// Builtin func, conservatively assumed to not
150+
// be safe for now.
157151
safe = false
158152
return false
159-
case typVal.IsBuiltin():
160-
// For now, conservatively assume that all
161-
// built-in functions have side effects.
153+
default:
154+
// A non-builtin func or method call.
155+
// Conservatively assume that all of them have
156+
// side effects for now.
162157
safe = false
163158
return false
164159
}
165-
// It's a type conversion, which cannot
166-
// have side effects.
167160
case *ast.UnaryExpr:
168161
if n.Op == token.ARROW {
169162
safe = false

0 commit comments

Comments
 (0)