Skip to content

Commit

Permalink
go/analysis/passes/httpresponse: fix ReceiverNamed usage
Browse files Browse the repository at this point in the history
Fixes a bug where ReceiverNamed returns a nil *types.Named if the type is
not a Named type.

Updates related documentation in httpmux.

Fixes golang/go#66259

Change-Id: I512feeb11473f2278edf4d9ef3312319d9dd1edf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/571057
Run-TryBot: Tim King <taking@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
timothy-king committed Mar 20, 2024
1 parent d0f7dce commit c3544e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 7 additions & 4 deletions go/analysis/passes/httpmux/httpmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,29 @@ func isServeMuxRegisterCall(pass *analysis.Pass, call *ast.CallExpr) bool {
if !isMethodNamed(fn, "net/http", "Handle", "HandleFunc") {
return false
}
isPtr, named := typesinternal.ReceiverNamed(fn.Type().(*types.Signature).Recv())
recv := fn.Type().(*types.Signature).Recv() // isMethodNamed() -> non-nil
isPtr, named := typesinternal.ReceiverNamed(recv)
return isPtr && analysisutil.IsNamedType(named, "net/http", "ServeMux")
}

// isMethodNamed reports when a function f is a method,
// in a package with the path pkgPath and the name of f is in names.
func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool {
if f == nil {
return false
}
if f.Pkg() == nil || f.Pkg().Path() != pkgPath {
return false
return false // not at pkgPath
}
if f.Type().(*types.Signature).Recv() == nil {
return false
return false // not a method
}
for _, n := range names {
if f.Name() == n {
return true
}
}
return false
return false // not in names
}

// stringConstantExpr returns expression's string constant value.
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/httpresponse/httpresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool {
return false // the function called does not return two values.
}
isPtr, named := typesinternal.ReceiverNamed(res.At(0))
if !isPtr || !analysisutil.IsNamedType(named, "net/http", "Response") {
if !isPtr || named == nil || !analysisutil.IsNamedType(named, "net/http", "Response") {
return false // the first return type is not *http.Response.
}

Expand Down
9 changes: 9 additions & 0 deletions go/analysis/passes/httpresponse/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ func badUnwrapResp() {
log.Fatal(err)
}
}

type i66259 struct{}

func (_ *i66259) Foo() (*int, int) { return nil, 1 }

func issue66259() {
var f *i66259
f.Foo()
}

0 comments on commit c3544e2

Please sign in to comment.