Skip to content

Commit 903c757

Browse files
go/analysis/passes/httpresponse: add typeparams test
testdata/src/typeparams is similar to testdata/src/a but uses type parameters. For golang/go#48704 Change-Id: I91b101bda6e1da5b2de6830896a4b13508f31322 Reviewed-on: https://go-review.googlesource.com/c/tools/+/358696 Trust: Guodong Li <[email protected]> Run-TryBot: Guodong Li <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent f916b54 commit 903c757

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

go/analysis/passes/httpresponse/httpresponse_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
package httpresponse_test
66

77
import (
8-
"testing"
9-
108
"golang.org/x/tools/go/analysis/analysistest"
119
"golang.org/x/tools/go/analysis/passes/httpresponse"
10+
"golang.org/x/tools/internal/typeparams"
11+
"testing"
1212
)
1313

1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, httpresponse.Analyzer, "a")
16+
tests := []string{"a"}
17+
if true || typeparams.Enabled {
18+
tests = append(tests, "typeparams")
19+
}
20+
analysistest.Run(t, testdata, httpresponse.Analyzer, tests...)
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2021 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+
// This file contains tests for the httpresponse checker.
6+
7+
//go:build go1.18
8+
9+
package typeparams
10+
11+
import (
12+
"log"
13+
"net/http"
14+
)
15+
16+
func badHTTPGet[T any](url string) {
17+
res, err := http.Get(url)
18+
defer res.Body.Close() // want "using res before checking for errors"
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
}
23+
24+
func mkClient[T any]() *T {
25+
return nil
26+
}
27+
28+
func badClientHTTPGet() {
29+
client := mkClient[http.Client]()
30+
res, _ := client.Get("")
31+
defer res.Body.Close() // want "using res before checking for errors"
32+
}
33+
34+
// User-defined type embedded "http.Client"
35+
type S[P any] struct {
36+
http.Client
37+
}
38+
39+
func unmatchedClientTypeName(client S[string]) {
40+
res, _ := client.Get("")
41+
defer res.Body.Close() // the name of client's type doesn't match "*http.Client"
42+
}
43+
44+
// User-defined Client type
45+
type C[P any] interface {
46+
Get(url string) (resp *P, err error)
47+
}
48+
49+
func userDefinedClientType(client C[http.Response]) {
50+
resp, _ := client.Get("http://foo.com")
51+
defer resp.Body.Close() // "client" is not of type "*http.Client"
52+
}

0 commit comments

Comments
 (0)