github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/analysis/sideeffect.go (about)

     1  package analysis
     2  
     3  import (
     4  	"go/ast"
     5  	"go/token"
     6  	"go/types"
     7  )
     8  
     9  func HasSideEffect(n ast.Node, info *types.Info) bool {
    10  	v := hasSideEffectVisitor{info: info}
    11  	ast.Walk(&v, n)
    12  	return v.hasSideEffect
    13  }
    14  
    15  type hasSideEffectVisitor struct {
    16  	info          *types.Info
    17  	hasSideEffect bool
    18  }
    19  
    20  func (v *hasSideEffectVisitor) Visit(node ast.Node) (w ast.Visitor) {
    21  	if v.hasSideEffect {
    22  		return nil
    23  	}
    24  	switch n := node.(type) {
    25  	case *ast.CallExpr:
    26  		if _, isSig := v.info.TypeOf(n.Fun).(*types.Signature); isSig { // skip conversions
    27  			v.hasSideEffect = true
    28  			return nil
    29  		}
    30  	case *ast.UnaryExpr:
    31  		if n.Op == token.ARROW {
    32  			v.hasSideEffect = true
    33  			return nil
    34  		}
    35  	}
    36  	return v
    37  }