Skip to content

Commit a6c6f4b

Browse files
go/analysis/passes/nilfunc: add typeparams test
testdata/src/typeparams is similar to testdata/src/a but uses type parameters. For golang/go#48704 Change-Id: I2d49190b606d6cdcfe79af5f29bba1117b07b94a Reviewed-on: https://go-review.googlesource.com/c/tools/+/359894 Run-TryBot: Guodong Li <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Guodong Li <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent a2be0cd commit a6c6f4b

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

go/analysis/passes/nilfunc/nilfunc.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/tools/go/analysis"
1515
"golang.org/x/tools/go/analysis/passes/inspect"
1616
"golang.org/x/tools/go/ast/inspector"
17+
"golang.org/x/tools/internal/typeparams"
1718
)
1819

1920
const Doc = `check for useless comparisons between functions and nil
@@ -59,6 +60,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
5960
obj = pass.TypesInfo.Uses[v]
6061
case *ast.SelectorExpr:
6162
obj = pass.TypesInfo.Uses[v.Sel]
63+
case *ast.IndexExpr, *typeparams.IndexListExpr:
64+
// Check generic functions such as "f[T1,T2]".
65+
if id, ok := typeparams.GetIndexExprData(v).X.(*ast.Ident); ok {
66+
obj = pass.TypesInfo.Uses[id]
67+
}
6268
default:
6369
return
6470
}

go/analysis/passes/nilfunc/nilfunc_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
package nilfunc_test
66

77
import (
8-
"testing"
9-
108
"golang.org/x/tools/go/analysis/analysistest"
119
"golang.org/x/tools/go/analysis/passes/nilfunc"
10+
"golang.org/x/tools/internal/typeparams"
11+
"testing"
1212
)
1313

1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, nilfunc.Analyzer, "a")
16+
tests := []string{"a"}
17+
if typeparams.Enabled {
18+
tests = append(tests, "typeparams")
19+
}
20+
analysistest.Run(t, testdata, nilfunc.Analyzer, tests...)
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// This file contains tests for the lostcancel checker.
6+
7+
//go:build go1.18
8+
9+
package typeparams
10+
11+
func f[P any]() {}
12+
13+
func g[P1 any, P2 any](x P1) {}
14+
15+
var f1 = f[int]
16+
17+
type T1[P any] struct {
18+
f func() P
19+
}
20+
21+
type T2[P1 any, P2 any] struct {
22+
g func(P1) P2
23+
}
24+
25+
func Comparison[P any](f2 func()T1[P]) {
26+
var t1 T1[P]
27+
var t2 T2[P, int]
28+
var fn func()
29+
if fn == nil || f1 == nil || f2 == nil || t1.f == nil || t2.g == nil {
30+
// no error; these func vars or fields may be nil
31+
}
32+
if f[P] == nil { // want "comparison of function f == nil is always false"
33+
panic("can't happen")
34+
}
35+
if f[int] == nil { // want "comparison of function f == nil is always false"
36+
panic("can't happen")
37+
}
38+
if g[P, int] == nil { // want "comparison of function g == nil is always false"
39+
panic("can't happen")
40+
}
41+
}
42+
43+
func Index[P any](a [](func()P)) {
44+
if a[1] == nil {
45+
// no error
46+
}
47+
var t1 []T1[P]
48+
var t2 [][]T2[P, P]
49+
if t1[1].f == nil || t2[0][1].g == nil {
50+
// no error
51+
}
52+
}

0 commit comments

Comments
 (0)