Skip to content

Commit

Permalink
go/analysis/passes/unusedresult: add test for typeparams
Browse files Browse the repository at this point in the history
This CL adds a test for unused results inside a function using generics.

Update golang/go#48704
  • Loading branch information
Jack You authored and jmesyou committed Oct 20, 2021
1 parent 9b675d0 commit d60ca4a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 6 additions & 1 deletion go/analysis/passes/unusedresult/unusedresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

0 comments on commit d60ca4a

Please sign in to comment.