File tree 3 files changed +65
-3
lines changed
go/analysis/passes/nilfunc
3 files changed +65
-3
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import (
14
14
"golang.org/x/tools/go/analysis"
15
15
"golang.org/x/tools/go/analysis/passes/inspect"
16
16
"golang.org/x/tools/go/ast/inspector"
17
+ "golang.org/x/tools/internal/typeparams"
17
18
)
18
19
19
20
const Doc = `check for useless comparisons between functions and nil
@@ -59,6 +60,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
59
60
obj = pass .TypesInfo .Uses [v ]
60
61
case * ast.SelectorExpr :
61
62
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
+ }
62
68
default :
63
69
return
64
70
}
Original file line number Diff line number Diff line change 5
5
package nilfunc_test
6
6
7
7
import (
8
- "testing"
9
-
10
8
"golang.org/x/tools/go/analysis/analysistest"
11
9
"golang.org/x/tools/go/analysis/passes/nilfunc"
10
+ "golang.org/x/tools/internal/typeparams"
11
+ "testing"
12
12
)
13
13
14
14
func Test (t * testing.T ) {
15
15
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 ... )
17
21
}
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