Skip to content

Commit acac939

Browse files
committed
go/analysis/passes/bools: add typeparams test
testdata/src/typeparams is similar to testdata/src/a but uses type parameters. This checks analysis report's error messages correctly display type information. Change-Id: I7a8054879556903be537374d209d71a62bff45fa Reviewed-on: https://go-review.googlesource.com/c/tools/+/353629 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent ac2ed98 commit acac939

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

go/analysis/passes/bools/bools_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/bools"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, bools.Analyzer, "a")
17+
tests := []string{"a"}
18+
if typeparams.Enabled {
19+
tests = append(tests, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, bools.Analyzer, tests...)
1722
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 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 bool checker.
6+
7+
//go:build go1.18
8+
9+
package typeparams
10+
11+
type T[P interface{ ~int }] struct {
12+
a P
13+
}
14+
15+
func (t T[P]) Foo() int { return int(t.a) }
16+
17+
type FT[P any] func() P
18+
19+
func Sink[Elem any]() chan Elem {
20+
return make(chan Elem)
21+
}
22+
23+
func RedundantConditions[P interface{ int }]() {
24+
type _f[P1 any] func() P1
25+
26+
var f, g _f[P]
27+
if f() == 0 || f() == 0 { // OK f might have side effects
28+
}
29+
var t T[P]
30+
_ = t.Foo() == 2 || t.Foo() == 2 // OK Foo might have side effects
31+
if v, w := f(), g(); v == w || v == w { // want `redundant or: v == w \|\| v == w`
32+
}
33+
34+
// error messages present type params correctly.
35+
_ = t == T[P]{2} || t == T[P]{2} // want `redundant or: t == T\[P\]\{2\} \|\| t == T\[P\]\{2\}`
36+
_ = FT[P](f) == nil || FT[P](f) == nil // want `redundant or: FT\[P\]\(f\) == nil \|\| FT\[P\]\(f\) == nil`
37+
_ = (func() P)(f) == nil || (func() P)(f) == nil // want `redundant or: \(func\(\) P\)\(f\) == nil \|\| \(func\(\) P\)\(f\) == nil`
38+
39+
var tint T[int]
40+
var fint _f[int]
41+
_ = tint == T[int]{2} || tint == T[int]{2} // want `redundant or: tint == T\[int\]\{2\} \|\| tint\ == T\[int\]\{2\}`
42+
_ = FT[int](fint) == nil || FT[int](fint) == nil // want `redundant or: FT\[int\]\(fint\) == nil \|\| FT\[int\]\(fint\) == nil`
43+
_ = (func() int)(fint) == nil || (func() int)(fint) == nil // want `redundant or: \(func\(\) int\)\(fint\) == nil \|\| \(func\(\) int\)\(fint\) == nil`
44+
45+
c := Sink[P]()
46+
_ = 0 == <-c || 0 == <-c // OK subsequent receives may yield different values
47+
for i, j := <-c, <-c; i == j || i == j; i, j = <-c, <-c { // want `redundant or: i == j \|\| i == j`
48+
}
49+
50+
var i, j P
51+
_ = i == 1 || j+1 == i || i == 1 // want `redundant or: i == 1 \|\| i == 1`
52+
_ = i == 1 || f() == 1 || i == 1 // OK f may alter i as a side effect
53+
_ = f() == 1 || i == 1 || i == 1 // want `redundant or: i == 1 \|\| i == 1`
54+
}
55+
56+
func SuspectConditions[P interface{ ~int }, S interface{ ~string }]() {
57+
var i, j P
58+
_ = i == 0 || i == 1 // OK
59+
_ = i+3 != 7 || j+5 == 0 || i+3 != 9 // want `suspect or: i\+3 != 7 \|\| i\+3 != 9`
60+
61+
var s S
62+
_ = s != "one" || s != "the other" // want `suspect or: s != .one. \|\| s != .the other.`
63+
}

0 commit comments

Comments
 (0)