File tree Expand file tree Collapse file tree 3 files changed +65
-3
lines changed
go/analysis/passes/nilfunc Expand file tree Collapse file tree 3 files changed +65
-3
lines changed Original file line number Diff line number Diff 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
1920const 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 }
Original file line number Diff line number Diff line change 55package nilfunc_test
66
77import (
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
1414func 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments