| 1 |
// Copyright 2025 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 typesinternal_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"honnef.co/go/tools/internal/xtools-internal/typesinternal" |
| 15 |
) |
| 16 |
|
| 17 |
func TestNoEffects(t *testing.T) { |
| 18 |
src := `package p |
| 19 |
|
| 20 |
type T int |
| 21 |
|
| 22 |
type G[P any] int |
| 23 |
|
| 24 |
func _() { |
| 25 |
var x int |
| 26 |
_ = T(x) |
| 27 |
_ = G[int](x) |
| 28 |
} |
| 29 |
` |
| 30 |
fset := token.NewFileSet() |
| 31 |
file, err := parser.ParseFile(fset, "p.go", src, 0) |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
var conf types.Config |
| 36 |
info := &types.Info{ |
| 37 |
Types: make(map[ast.Expr]types.TypeAndValue), |
| 38 |
} |
| 39 |
_, err = conf.Check("", fset, []*ast.File{file}, info) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
ast.Inspect(file, func(node ast.Node) bool { |
| 44 |
call, ok := node.(*ast.CallExpr) |
| 45 |
if !ok { |
| 46 |
return true |
| 47 |
} |
| 48 |
if !typesinternal.NoEffects(info, call) { |
| 49 |
t.Errorf("NoEffects(%s) = false", types.ExprString(call)) |
| 50 |
} |
| 51 |
return true |
| 52 |
}) |
| 53 |
} |