Skip to content

Commit 6f5fd9b

Browse files
committed
internal/lsp: add parameterized slice test for simplifyslice analysis
Add a test for a parameterized slice to make sure that the simplify slice analysis still finds the simplification. Change-Id: I20d5f064bcae60c752f0dee53472dd5db0b18a89 Reviewed-on: https://go-review.googlesource.com/c/tools/+/352089 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2189684 commit 6f5fd9b

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

internal/lsp/analysis/simplifyslice/simplifyslice_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/internal/lsp/analysis/simplifyslice"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, "a")
17+
tests := []string{"a"}
18+
if typeparams.Enabled {
19+
tests = append(tests, "typeparams")
20+
}
21+
analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, tests...)
1722
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
//go:build go1.18
6+
// +build go1.18
7+
8+
package testdata
9+
10+
type List[E any] []E
11+
12+
// TODO(suzmue): add a test for generic slice expressions when https://github.com/golang/go/issues/48618 is closed.
13+
// type S interface{ ~[]int }
14+
15+
var (
16+
a [10]byte
17+
b [20]float32
18+
p List[int]
19+
20+
_ = p[0:]
21+
_ = p[1:10]
22+
_ = p[2:len(p)] // want "unneeded: len\\(p\\)"
23+
_ = p[3:(len(p))]
24+
_ = p[len(a) : len(p)-1]
25+
_ = p[0:len(b)]
26+
_ = p[2:len(p):len(p)]
27+
28+
_ = p[:]
29+
_ = p[:10]
30+
_ = p[:len(p)] // want "unneeded: len\\(p\\)"
31+
_ = p[:(len(p))]
32+
_ = p[:len(p)-1]
33+
_ = p[:len(b)]
34+
_ = p[:len(p):len(p)]
35+
)
36+
37+
func foo[E any](a List[E]) {
38+
_ = a[0:len(a)] // want "unneeded: len\\(a\\)"
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
//go:build go1.18
6+
// +build go1.18
7+
8+
package testdata
9+
10+
type List[E any] []E
11+
12+
// TODO(suzmue): add a test for generic slice expressions when https://github.com/golang/go/issues/48618 is closed.
13+
// type S interface{ ~[]int }
14+
15+
var (
16+
a [10]byte
17+
b [20]float32
18+
p List[int]
19+
20+
_ = p[0:]
21+
_ = p[1:10]
22+
_ = p[2:] // want "unneeded: len\\(p\\)"
23+
_ = p[3:(len(p))]
24+
_ = p[len(a) : len(p)-1]
25+
_ = p[0:len(b)]
26+
_ = p[2:len(p):len(p)]
27+
28+
_ = p[:]
29+
_ = p[:10]
30+
_ = p[:] // want "unneeded: len\\(p\\)"
31+
_ = p[:(len(p))]
32+
_ = p[:len(p)-1]
33+
_ = p[:len(b)]
34+
_ = p[:len(p):len(p)]
35+
)
36+
37+
func foo[E any](a List[E]) {
38+
_ = a[0:] // want "unneeded: len\\(a\\)"
39+
}

0 commit comments

Comments
 (0)