Skip to content

Commit

Permalink
go/analysis/passes/nilfunc: add typeparams test
Browse files Browse the repository at this point in the history
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 <guodongli@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Guodong Li <guodongli@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
guodongli-google committed Oct 30, 2021
1 parent a2be0cd commit a6c6f4b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions go/analysis/passes/nilfunc/nilfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/internal/typeparams"
)

const Doc = `check for useless comparisons between functions and nil
Expand Down Expand Up @@ -59,6 +60,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
obj = pass.TypesInfo.Uses[v]
case *ast.SelectorExpr:
obj = pass.TypesInfo.Uses[v.Sel]
case *ast.IndexExpr, *typeparams.IndexListExpr:
// Check generic functions such as "f[T1,T2]".
if id, ok := typeparams.GetIndexExprData(v).X.(*ast.Ident); ok {
obj = pass.TypesInfo.Uses[id]
}
default:
return
}
Expand Down
10 changes: 7 additions & 3 deletions go/analysis/passes/nilfunc/nilfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package nilfunc_test

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/nilfunc"
"golang.org/x/tools/internal/typeparams"
"testing"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, nilfunc.Analyzer, "a")
tests := []string{"a"}
if typeparams.Enabled {
tests = append(tests, "typeparams")
}
analysistest.Run(t, testdata, nilfunc.Analyzer, tests...)
}
52 changes: 52 additions & 0 deletions go/analysis/passes/nilfunc/testdata/src/typeparams/typeparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file contains tests for the lostcancel checker.

//go:build go1.18

package typeparams

func f[P any]() {}

func g[P1 any, P2 any](x P1) {}

var f1 = f[int]

type T1[P any] struct {
f func() P
}

type T2[P1 any, P2 any] struct {
g func(P1) P2
}

func Comparison[P any](f2 func()T1[P]) {
var t1 T1[P]
var t2 T2[P, int]
var fn func()
if fn == nil || f1 == nil || f2 == nil || t1.f == nil || t2.g == nil {
// no error; these func vars or fields may be nil
}
if f[P] == nil { // want "comparison of function f == nil is always false"
panic("can't happen")
}
if f[int] == nil { // want "comparison of function f == nil is always false"
panic("can't happen")
}
if g[P, int] == nil { // want "comparison of function g == nil is always false"
panic("can't happen")
}
}

func Index[P any](a [](func()P)) {
if a[1] == nil {
// no error
}
var t1 []T1[P]
var t2 [][]T2[P, P]
if t1[1].f == nil || t2[0][1].g == nil {
// no error
}
}

0 comments on commit a6c6f4b

Please sign in to comment.