-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <guodongli@google.com> Run-TryBot: Guodong Li <guodongli@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
- Loading branch information
1 parent
f916b54
commit 903c757
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
go/analysis/passes/httpresponse/testdata/src/typeparams/typeparams.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 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. | ||
|
||
// This file contains tests for the httpresponse checker. | ||
|
||
//go:build go1.18 | ||
|
||
package typeparams | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func badHTTPGet[T any](url string) { | ||
res, err := http.Get(url) | ||
defer res.Body.Close() // want "using res before checking for errors" | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func mkClient[T any]() *T { | ||
return nil | ||
} | ||
|
||
func badClientHTTPGet() { | ||
client := mkClient[http.Client]() | ||
res, _ := client.Get("") | ||
defer res.Body.Close() // want "using res before checking for errors" | ||
} | ||
|
||
// User-defined type embedded "http.Client" | ||
type S[P any] struct { | ||
http.Client | ||
} | ||
|
||
func unmatchedClientTypeName(client S[string]) { | ||
res, _ := client.Get("") | ||
defer res.Body.Close() // the name of client's type doesn't match "*http.Client" | ||
} | ||
|
||
// User-defined Client type | ||
type C[P any] interface { | ||
Get(url string) (resp *P, err error) | ||
} | ||
|
||
func userDefinedClientType(client C[http.Response]) { | ||
resp, _ := client.Get("http://foo.com") | ||
defer resp.Body.Close() // "client" is not of type "*http.Client" | ||
} |