1 | // Copyright 2021 The Go Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE file. |
4 | |
5 | package analysisutil_test |
6 | |
7 | import ( |
8 | "go/ast" |
9 | "go/parser" |
10 | "go/token" |
11 | "go/types" |
12 | "testing" |
13 | |
14 | "golang.org/x/tools/go/analysis/passes/internal/analysisutil" |
15 | "golang.org/x/tools/internal/typeparams" |
16 | ) |
17 | |
18 | func TestHasSideEffects(t *testing.T) { |
19 | if !typeparams.Enabled { |
20 | t.Skip("type parameters are not enabled") |
21 | } |
22 | src := `package p |
23 | |
24 | type T int |
25 | |
26 | type G[P any] int |
27 | |
28 | func _() { |
29 | var x int |
30 | _ = T(x) |
31 | _ = G[int](x) |
32 | } |
33 | ` |
34 | fset := token.NewFileSet() |
35 | file, err := parser.ParseFile(fset, "p.go", src, 0) |
36 | if err != nil { |
37 | t.Fatal(err) |
38 | } |
39 | var conf types.Config |
40 | info := &types.Info{ |
41 | Types: make(map[ast.Expr]types.TypeAndValue), |
42 | } |
43 | _, err = conf.Check("", fset, []*ast.File{file}, info) |
44 | if err != nil { |
45 | t.Fatal(err) |
46 | } |
47 | ast.Inspect(file, func(node ast.Node) bool { |
48 | call, ok := node.(*ast.CallExpr) |
49 | if !ok { |
50 | return true |
51 | } |
52 | if got := analysisutil.HasSideEffects(info, call); got != false { |
53 | t.Errorf("HasSideEffects(%s) = true, want false", types.ExprString(call)) |
54 | } |
55 | return true |
56 | }) |
57 | } |
58 |
Members