From 36e7bf96e1b84e020a1dd8ef238e3da83152c4c0 Mon Sep 17 00:00:00 2001 From: Peter Weinberger Date: Thu, 30 Sep 2021 11:12:39 -0400 Subject: [PATCH] internal/lsp/analysis/unusedparams: add tests for generics using the typeparams convention. Change-Id: I42094b75bd43937bdae099473d6241de887456b4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/353289 Trust: Peter Weinberger Run-TryBot: Peter Weinberger gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- .../testdata/src/typeparams/typeparams.go | 55 +++++++++++++++++++ .../unusedparams/unusedparams_test.go | 7 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/lsp/analysis/unusedparams/testdata/src/typeparams/typeparams.go diff --git a/internal/lsp/analysis/unusedparams/testdata/src/typeparams/typeparams.go b/internal/lsp/analysis/unusedparams/testdata/src/typeparams/typeparams.go new file mode 100644 index 0000000000..000a62f0b6 --- /dev/null +++ b/internal/lsp/analysis/unusedparams/testdata/src/typeparams/typeparams.go @@ -0,0 +1,55 @@ +// 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. + +package typeparams + +import ( + "bytes" + "fmt" + "net/http" +) + +type parent[T any] interface { + n(f T) +} + +type yuh[T any] struct { + a T +} + +func (y *yuh[int]) n(f bool) { + for i := 0; i < 10; i++ { + fmt.Println(i) + } +} + +func a[T comparable](i1 int, i2 T, i3 int) int { // want "potentially unused parameter: 'i2'" + i3 += i1 + _ = func(z int) int { // want "potentially unused parameter: 'z'" + _ = 1 + return 1 + } + return i3 +} + +func b[T any](c bytes.Buffer) { // want "potentially unused parameter: 'c'" + _ = 1 +} + +func z[T http.ResponseWriter](h T, _ *http.Request) { // want "potentially unused parameter: 'h'" + fmt.Println("Before") +} + +func l(h http.Handler) http.Handler { + return http.HandlerFunc(z[http.ResponseWriter]) +} + +func mult(a, b int) int { // want "potentially unused parameter: 'b'" + a += 1 + return a +} + +func y[T any](a T) { + panic("yo") +} diff --git a/internal/lsp/analysis/unusedparams/unusedparams_test.go b/internal/lsp/analysis/unusedparams/unusedparams_test.go index 907f71c8d6..646a3dc653 100644 --- a/internal/lsp/analysis/unusedparams/unusedparams_test.go +++ b/internal/lsp/analysis/unusedparams/unusedparams_test.go @@ -9,9 +9,14 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/internal/lsp/analysis/unusedparams" + "golang.org/x/tools/internal/typeparams" ) func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.Run(t, testdata, unusedparams.Analyzer, "a") + tests := []string{"a"} + if typeparams.Enabled { + tests = append(tests, "typeparams") + } + analysistest.Run(t, testdata, unusedparams.Analyzer, tests...) }