From 6f5fd9bf4117ee41c12da6febf7c785791b64491 Mon Sep 17 00:00:00 2001 From: Suzy Mueller Date: Fri, 24 Sep 2021 10:43:52 -0600 Subject: [PATCH] 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 Run-TryBot: Suzy Mueller gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- .../simplifyslice/simplifyslice_test.go | 7 +++- .../testdata/src/typeparams/typeparams.go | 39 +++++++++++++++++++ .../src/typeparams/typeparams.go.golden | 39 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go create mode 100644 internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go.golden diff --git a/internal/lsp/analysis/simplifyslice/simplifyslice_test.go b/internal/lsp/analysis/simplifyslice/simplifyslice_test.go index 91db76ae02..cff6267c67 100644 --- a/internal/lsp/analysis/simplifyslice/simplifyslice_test.go +++ b/internal/lsp/analysis/simplifyslice/simplifyslice_test.go @@ -9,9 +9,14 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/internal/lsp/analysis/simplifyslice" + "golang.org/x/tools/internal/typeparams" ) func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, "a") + tests := []string{"a"} + if typeparams.Enabled { + tests = append(tests, "typeparams") + } + analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, tests...) } diff --git a/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go b/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go new file mode 100644 index 0000000000..69db3100a9 --- /dev/null +++ b/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go @@ -0,0 +1,39 @@ +// 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. +// +//go:build go1.18 +// +build go1.18 + +package testdata + +type List[E any] []E + +// TODO(suzmue): add a test for generic slice expressions when https://github.com/golang/go/issues/48618 is closed. +// type S interface{ ~[]int } + +var ( + a [10]byte + b [20]float32 + p List[int] + + _ = p[0:] + _ = p[1:10] + _ = p[2:len(p)] // want "unneeded: len\\(p\\)" + _ = p[3:(len(p))] + _ = p[len(a) : len(p)-1] + _ = p[0:len(b)] + _ = p[2:len(p):len(p)] + + _ = p[:] + _ = p[:10] + _ = p[:len(p)] // want "unneeded: len\\(p\\)" + _ = p[:(len(p))] + _ = p[:len(p)-1] + _ = p[:len(b)] + _ = p[:len(p):len(p)] +) + +func foo[E any](a List[E]) { + _ = a[0:len(a)] // want "unneeded: len\\(a\\)" +} diff --git a/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go.golden b/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go.golden new file mode 100644 index 0000000000..99ca9e4474 --- /dev/null +++ b/internal/lsp/analysis/simplifyslice/testdata/src/typeparams/typeparams.go.golden @@ -0,0 +1,39 @@ +// 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. +// +//go:build go1.18 +// +build go1.18 + +package testdata + +type List[E any] []E + +// TODO(suzmue): add a test for generic slice expressions when https://github.com/golang/go/issues/48618 is closed. +// type S interface{ ~[]int } + +var ( + a [10]byte + b [20]float32 + p List[int] + + _ = p[0:] + _ = p[1:10] + _ = p[2:] // want "unneeded: len\\(p\\)" + _ = p[3:(len(p))] + _ = p[len(a) : len(p)-1] + _ = p[0:len(b)] + _ = p[2:len(p):len(p)] + + _ = p[:] + _ = p[:10] + _ = p[:] // want "unneeded: len\\(p\\)" + _ = p[:(len(p))] + _ = p[:len(p)-1] + _ = p[:len(b)] + _ = p[:len(p):len(p)] +) + +func foo[E any](a List[E]) { + _ = a[0:] // want "unneeded: len\\(a\\)" +}