Skip to content

Commit

Permalink
go/analysis/passes/unsafeptr: add tests using generics
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
findleyr committed Nov 15, 2021
1 parent 49ce184 commit 4392381
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 21 additions & 0 deletions go/analysis/passes/unsafeptr/testdata/src/typeparams/typeparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

package typeparams

import "unsafe"

func _[IntPtr ~uintptr, RealPtr *T, AnyPtr uintptr | *T, T any]() {
var (
i IntPtr
r RealPtr
a AnyPtr
)
_ = unsafe.Pointer(i) // incorrect, but not detected
_ = unsafe.Pointer(i + i) // incorrect, but not detected
_ = unsafe.Pointer(1 + i) // incorrect, but not detected
_ = unsafe.Pointer(uintptr(i)) // want "possible misuse of unsafe.Pointer"
_ = unsafe.Pointer(r)
_ = unsafe.Pointer(a) // possibly incorrect, but not detected
}
7 changes: 6 additions & 1 deletion go/analysis/passes/unsafeptr/unsafeptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (

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

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, unsafeptr.Analyzer, "a")
pkgs := []string{"a"}
if typeparams.Enabled {
pkgs = append(pkgs, "typeparams")
}
analysistest.Run(t, testdata, unsafeptr.Analyzer, pkgs...)
}

0 comments on commit 4392381

Please sign in to comment.