Skip to content

Commit 4392381

Browse files
committed
go/analysis/passes/unsafeptr: add tests using generics
Unlike with some other analyzers, it did not seem worthwhile to consider a type parameter's type set when looking for incorrect conversions to unsafe.Pointer. There's probably no reason to have a type parameter with uintptr structural type. Add some sanity-check tests for the behavior of this analyzer with respect to generic code. Updates golang/go#48704 Change-Id: Ibc3180c6eba9c2c88ea2220b1c84cd27971a6700 Reviewed-on: https://go-review.googlesource.com/c/tools/+/360174 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 49ce184 commit 4392381

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package typeparams
6+
7+
import "unsafe"
8+
9+
func _[IntPtr ~uintptr, RealPtr *T, AnyPtr uintptr | *T, T any]() {
10+
var (
11+
i IntPtr
12+
r RealPtr
13+
a AnyPtr
14+
)
15+
_ = unsafe.Pointer(i) // incorrect, but not detected
16+
_ = unsafe.Pointer(i + i) // incorrect, but not detected
17+
_ = unsafe.Pointer(1 + i) // incorrect, but not detected
18+
_ = unsafe.Pointer(uintptr(i)) // want "possible misuse of unsafe.Pointer"
19+
_ = unsafe.Pointer(r)
20+
_ = unsafe.Pointer(a) // possibly incorrect, but not detected
21+
}

go/analysis/passes/unsafeptr/unsafeptr_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/unsafeptr"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, unsafeptr.Analyzer, "a")
17+
pkgs := []string{"a"}
18+
if typeparams.Enabled {
19+
pkgs = append(pkgs, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, unsafeptr.Analyzer, pkgs...)
1722
}

0 commit comments

Comments
 (0)