]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: check that C receivers are cgo imports
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 13 Jun 2017 15:22:06 +0000 (16:22 +0100)
committerRob Pike <r@golang.org>
Wed, 9 Aug 2017 02:23:51 +0000 (02:23 +0000)
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: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/vet/cgo.go
src/cmd/vet/testdata/cgo/cgo2.go
src/cmd/vet/testdata/cgo/cgo4.go [new file with mode: 0644]

index 984911c489f9d70c8c71e371ccbc6c1ac452146f..76364ff6ed8bec5cf26c143828f9cc1717780ff8 100644 (file)
@@ -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
        }
 
index 276aea961937dc6d0f5ce3af0105650b14bcfbaf..4f27116893138361a13a404e7cada585f4e13426 100644 (file)
@@ -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})
diff --git a/src/cmd/vet/testdata/cgo/cgo4.go b/src/cmd/vet/testdata/cgo/cgo4.go
new file mode 100644 (file)
index 0000000..67b5450
--- /dev/null
@@ -0,0 +1,15 @@
+// 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})