Skip to content

Commit ed4c84e

Browse files
committed
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
1 parent 9b675d0 commit ed4c84e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2015 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+
7+
package typeparams
8+
9+
import (
10+
"bytes"
11+
"errors"
12+
"fmt"
13+
)
14+
15+
func _[T any]() {
16+
fmt.Errorf("") // want "result of fmt.Errorf call not used"
17+
_ = fmt.Errorf("")
18+
19+
errors.New("") // want "result of errors.New call not used"
20+
21+
err := errors.New("")
22+
err.Error() // want `result of \(error\).Error call not used`
23+
24+
var buf bytes.Buffer
25+
buf.String() // want `result of \(bytes.Buffer\).String call not used`
26+
27+
fmt.Sprint("") // want "result of fmt.Sprint call not used"
28+
fmt.Sprintf("") // want "result of fmt.Sprintf call not used"
29+
}

go/analysis/passes/unusedresult/unusedresult_test.go

+6-1
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/unusedresult"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, unusedresult.Analyzer, "a")
17+
tests := []string{"a"}
18+
if typeparams.Enabled {
19+
tests = append(tests, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, unusedresult.Analyzer, tests...)
1722
}

0 commit comments

Comments
 (0)