Skip to content

Commit

Permalink
internal/lsp: fix fillstruct for structs with unsafe.Pointer
Browse files Browse the repository at this point in the history
Provide a default value for unsafe.Pointer in fillstruct.

Fixes golang/go#52640

Change-Id: I10a1878fbf53b082f83f44e0ba2788ead14439d6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/403535
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
suzmue committed May 5, 2022
1 parent 0ebacc1 commit 30fbd19
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/lsp/analysis/fillstruct/fillstruct.go
Expand Up @@ -368,6 +368,8 @@ func populateValue(fset *token.FileSet, f *ast.File, pkg *types.Package, typ typ
return &ast.Ident{Name: "false"}
case u.Info()&types.IsString != 0:
return &ast.BasicLit{Kind: token.STRING, Value: `""`}
case u.Kind() == types.UnsafePointer:
return ast.NewIdent("nil")
default:
panic("unknown basic type")
}
Expand Down
7 changes: 7 additions & 0 deletions internal/lsp/analysis/fillstruct/testdata/src/a/a.go
Expand Up @@ -8,6 +8,7 @@ import (
data "b"
"go/ast"
"go/token"
"unsafe"
)

type emptyStruct struct{}
Expand Down Expand Up @@ -104,3 +105,9 @@ var _ = []ast.BasicLit{

var _ = []ast.BasicLit{{}, // want ""
}

type unsafeStruct struct {
foo unsafe.Pointer
}

var _ = unsafeStruct{} // want ""
12 changes: 12 additions & 0 deletions internal/lsp/testdata/fillstruct/fill_struct_unsafe.go
@@ -0,0 +1,12 @@
package fillstruct

import "unsafe"

type unsafeStruct struct {
x int
p unsafe.Pointer
}

func fill() {
_ := unsafeStruct{} //@suggestedfix("}", "refactor.rewrite")
}
17 changes: 17 additions & 0 deletions internal/lsp/testdata/fillstruct/fill_struct_unsafe.go.golden
@@ -0,0 +1,17 @@
-- suggestedfix_fill_struct_unsafe_11_20 --
package fillstruct

import "unsafe"

type unsafeStruct struct {
x int
p unsafe.Pointer
}

func fill() {
_ := unsafeStruct{
x: 0,
p: nil,
} //@suggestedfix("}", "refactor.rewrite")
}

2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary.txt.golden
Expand Up @@ -13,7 +13,7 @@ FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
SemanticTokenCount = 3
SuggestedFixCount = 61
SuggestedFixCount = 62
FunctionExtractionCount = 25
MethodExtractionCount = 6
DefinitionsCount = 95
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/testdata/summary_go1.18.txt.golden
Expand Up @@ -13,7 +13,7 @@ FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
SemanticTokenCount = 3
SuggestedFixCount = 62
SuggestedFixCount = 63
FunctionExtractionCount = 25
MethodExtractionCount = 6
DefinitionsCount = 108
Expand Down

0 comments on commit 30fbd19

Please sign in to comment.