Commit bef0055a authored by Daniel Martí's avatar Daniel Martí Committed by Rob Pike

cmd/vet: check that C receivers are cgo imports

Otherwise, vet might have false positives when "C" is a variable and
we're just using a method on it. Or when an import was renamed to "C".

Add test files for both of these cases.

Fixes #20655.

Change-Id: I55fb93119444a67fcf7891ad92653678cbd4670e
Reviewed-on: https://go-review.googlesource.com/45551
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent 561b147e
......@@ -34,7 +34,12 @@ func checkCgoCall(f *File, node ast.Node) {
return
}
id, ok := sel.X.(*ast.Ident)
if !ok || id.Name != "C" {
if !ok {
return
}
pkgname, ok := f.pkg.uses[id].(*types.PkgName)
if !ok || pkgname.Imported().Path() != "C" {
return
}
......
......@@ -7,3 +7,6 @@
package testdata
var _ = C.f(*p(**p))
// Passing a pointer (via the slice), but C isn't cgo.
var _ = C.f([]int{3})
// Copyright 2017 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.
// Test the cgo checker on a file that doesn't use cgo, but has an
// import named "C".
package testdata
import C "fmt"
var _ = C.Println(*p(**p))
// Passing a pointer (via a slice), but C is fmt, not cgo.
var _ = C.Println([]int{3})
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment