Skip to content

Commit

Permalink
go/analysis/passes/httpresponse: add typeparams test
Browse files Browse the repository at this point in the history
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
guodongli-google committed Oct 26, 2021
1 parent f916b54 commit 903c757
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
10 changes: 7 additions & 3 deletions go/analysis/passes/httpresponse/httpresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package httpresponse_test

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/httpresponse"
"golang.org/x/tools/internal/typeparams"
"testing"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, httpresponse.Analyzer, "a")
tests := []string{"a"}
if true || typeparams.Enabled {
tests = append(tests, "typeparams")
}
analysistest.Run(t, testdata, httpresponse.Analyzer, tests...)
}
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"
}

0 comments on commit 903c757

Please sign in to comment.