From d60ca4ae3b72c6fea6d9f487dd3a8a260028fe14 Mon Sep 17 00:00:00 2001 From: Jack You Date: Wed, 20 Oct 2021 17:57:23 +0000 Subject: [PATCH] go/analysis/passes/unusedresult: add test for typeparams This CL adds a test for unused results inside a function using generics. Update golang/go#48704 --- .../testdata/src/typeparams/typeparams.go | 29 +++++++++++++++++++ .../passes/unusedresult/unusedresult_test.go | 7 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 go/analysis/passes/unusedresult/testdata/src/typeparams/typeparams.go diff --git a/go/analysis/passes/unusedresult/testdata/src/typeparams/typeparams.go b/go/analysis/passes/unusedresult/testdata/src/typeparams/typeparams.go new file mode 100644 index 00000000000..9b5905beb9f --- /dev/null +++ b/go/analysis/passes/unusedresult/testdata/src/typeparams/typeparams.go @@ -0,0 +1,29 @@ +// Copyright 2015 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 + +package typeparams + +import ( + "bytes" + "errors" + "fmt" +) + +func _[T any]() { + fmt.Errorf("") // want "result of fmt.Errorf call not used" + _ = fmt.Errorf("") + + errors.New("") // want "result of errors.New call not used" + + err := errors.New("") + err.Error() // want `result of \(error\).Error call not used` + + var buf bytes.Buffer + buf.String() // want `result of \(bytes.Buffer\).String call not used` + + fmt.Sprint("") // want "result of fmt.Sprint call not used" + fmt.Sprintf("") // want "result of fmt.Sprintf call not used" +} diff --git a/go/analysis/passes/unusedresult/unusedresult_test.go b/go/analysis/passes/unusedresult/unusedresult_test.go index 90bf7ba4f0c..7ec09d6531d 100644 --- a/go/analysis/passes/unusedresult/unusedresult_test.go +++ b/go/analysis/passes/unusedresult/unusedresult_test.go @@ -9,9 +9,14 @@ import ( "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/unusedresult" + "golang.org/x/tools/internal/typeparams" ) func Test(t *testing.T) { testdata := analysistest.TestData() - analysistest.Run(t, testdata, unusedresult.Analyzer, "a") + tests := []string{"a"} + if typeparams.Enabled { + tests = append(tests, "typeparams") + } + analysistest.Run(t, testdata, unusedresult.Analyzer, tests...) }