Skip to content

Commit 1c6db19

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. Updates golang/go#48704
1 parent c8ad2e1 commit 1c6db19

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
s := userdefs.SingleTypeParam[int]{X: 1}
35+
s.String() // want `result of \(typeparams/userdefs.SingleTypeParam\[int\]\).String call not used`
36+
_ = s.String()
37+
38+
m := userdefs.MultiTypeParam[int, string]{X: 1, Y: "one"}
39+
m.String() // want `result of \(typeparams/userdefs.MultiTypeParam\[int, string\]\).String call not used`
40+
_ = m.String()
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 SingleTypeParam[T any] struct {
14+
X T
15+
}
16+
17+
func (_ *SingleTypeParam[T]) String() string {
18+
return "SingleTypeParam"
19+
}
20+
21+
type MultiTypeParam[T any, U any] struct {
22+
X T
23+
Y U
24+
}
25+
26+
func (_ *MultiTypeParam[T, U]) String() string {
27+
return "MultiTypeParam"
28+
}

go/analysis/passes/unusedresult/unusedresult.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/tools/go/analysis/passes/inspect"
1818
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
1919
"golang.org/x/tools/go/ast/inspector"
20+
"golang.org/x/tools/internal/typeparams"
2021
)
2122

2223
// TODO(adonovan): make this analysis modular: export a mustUseResult
@@ -70,6 +71,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
7071
return // a conversion, not a call
7172
}
7273

74+
index := typeparams.GetIndexExprData(fun)
75+
if index != nil {
76+
fun = index.X // If this is generic function or method call, skip the instantiation arguments
77+
}
78+
7379
selector, ok := fun.(*ast.SelectorExpr)
7480
if !ok {
7581
return // neither a method call nor a qualified ident

go/analysis/passes/unusedresult/unusedresult_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ 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+
funcs := "typeparams/userdefs.MustUse,errors.New,fmt.Errorf,fmt.Sprintf,fmt.Sprint"
18+
unusedresult.Analyzer.Flags.Set("funcs", funcs)
19+
tests := []string{"a"}
20+
if typeparams.Enabled {
21+
tests = append(tests, "typeparams")
22+
}
23+
analysistest.Run(t, testdata, unusedresult.Analyzer, tests...)
1724
}

0 commit comments

Comments
 (0)