File: sideeffect.go

package info (click to toggle)
golang-github-gopherjs-gopherjs 0.0~git20170927.0.4152256-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,468 kB
  • sloc: cpp: 91; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package analysis

import (
	"go/ast"
	"go/token"
	"go/types"
)

func HasSideEffect(n ast.Node, info *types.Info) bool {
	v := hasSideEffectVisitor{info: info}
	ast.Walk(&v, n)
	return v.hasSideEffect
}

type hasSideEffectVisitor struct {
	info          *types.Info
	hasSideEffect bool
}

func (v *hasSideEffectVisitor) Visit(node ast.Node) (w ast.Visitor) {
	if v.hasSideEffect {
		return nil
	}
	switch n := node.(type) {
	case *ast.CallExpr:
		if _, isSig := v.info.TypeOf(n.Fun).(*types.Signature); isSig { // skip conversions
			v.hasSideEffect = true
			return nil
		}
	case *ast.UnaryExpr:
		if n.Op == token.ARROW {
			v.hasSideEffect = true
			return nil
		}
	}
	return v
}