Skip to content

Commit b7b4d46

Browse files
author
Jack You
committed
go/analysis/passes/unusedresult: update and add test for typeparams
This CL implements support for setting unused generic funcs via flags. A test for unused results with generics is added. Update golang/go#48704
1 parent 9b675d0 commit b7b4d46

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"typeparams/userdefs"
14+
)
15+
16+
func _[T any]() {
17+
fmt.Errorf("") // want "result of fmt.Errorf call not used"
18+
_ = fmt.Errorf("")
19+
20+
errors.New("") // want "result of errors.New call not used"
21+
22+
err := errors.New("")
23+
err.Error() // want `result of \(error\).Error call not used`
24+
25+
var buf bytes.Buffer
26+
buf.String() // want `result of \(bytes.Buffer\).String call not used`
27+
28+
fmt.Sprint("") // want "result of fmt.Sprint call not used"
29+
fmt.Sprintf("") // want "result of fmt.Sprintf call not used"
30+
31+
userdefs.MustUse[int](1) // want "result of typeparams/userdefs.MustUse call not used"
32+
_ = userdefs.MustUse[int](2)
33+
34+
st := userdefs.ST[int]{X: 1}
35+
st.String() // want `result of \(typeparams/userdefs.ST\[int\]\).String call not used`
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 userdefs
8+
9+
func MustUse[T interface{ ~int }](v T) T {
10+
return v + 1
11+
}
12+
13+
type ST[T interface{ ~int }] struct {
14+
X T
15+
}
16+
17+
func (st *ST[T]) String() string {
18+
return "st"
19+
}

go/analysis/passes/unusedresult/unusedresult.go

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
7070
return // a conversion, not a call
7171
}
7272

73+
index, ok := fun.(*ast.IndexExpr)
74+
if ok {
75+
if _, ok := pass.TypesInfo.TypeOf(index).(*types.Signature); ok {
76+
fun = index.X // get selector of generic function or method call
77+
}
78+
}
79+
7380
selector, ok := fun.(*ast.SelectorExpr)
7481
if !ok {
7582
return // neither a method call nor a qualified ident

go/analysis/passes/unusedresult/unusedresult_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ 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+
unusedresult.Analyzer.Flags.Set("funcs", "typeparams/userdefs.MustUse,errors.New,fmt.Errorf,fmt.Sprintf,fmt.Sprint,sort.Reverse,context.WithValue,context.WithCancel,context.WithDeadline,context.WithTimeout")
20+
tests = append(tests, "typeparams")
21+
}
22+
analysistest.Run(t, testdata, unusedresult.Analyzer, tests...)
1723
}

0 commit comments

Comments
 (0)