diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go
index 1a8829d..6bbfb79 100644
--- a/gopls/internal/cache/analysis.go
+++ b/gopls/internal/cache/analysis.go
@@ -572,7 +572,7 @@
const cacheKind = "analysis"
if data, err := filecache.Get(cacheKind, key, filecache.Bytes); err == nil {
// cache hit
- analyzeSummaryCodec.Decode(data, &summary)
+ summary = analyzeSummaryCodec.Decode(data)
} else if err != filecache.ErrNotFound {
return nil, bug.Errorf("internal error reading shared cache: %v", err)
} else {
diff --git a/gopls/internal/cache/errors.go b/gopls/internal/cache/errors.go
index 26f30c8..f7e9813 100644
--- a/gopls/internal/cache/errors.go
+++ b/gopls/internal/cache/errors.go
@@ -205,8 +205,7 @@
// decodeDiagnostics decodes the given gob-encoded diagnostics.
func decodeDiagnostics(data []byte) []*Diagnostic {
- var gobDiags []gobDiagnostic
- diagnosticsCodec.Decode(data, &gobDiags)
+ gobDiags := diagnosticsCodec.Decode(data)
var srcDiags []*Diagnostic
for _, gobDiag := range gobDiags {
var srcFixes []SuggestedFix
diff --git a/gopls/internal/cache/methodsets/methodsets.go b/gopls/internal/cache/methodsets/methodsets.go
index feb1b16..b47f4a1 100644
--- a/gopls/internal/cache/methodsets/methodsets.go
+++ b/gopls/internal/cache/methodsets/methodsets.go
@@ -67,9 +67,7 @@
// Decode decodes the given gob-encoded data as an Index.
func Decode(pkgpath metadata.PackagePath, data []byte) *Index {
- var pkg gobPackage
- packageCodec.Decode(data, &pkg)
- return &Index{pkg: pkg, PkgPath: pkgpath}
+ return &Index{pkg: packageCodec.Decode(data), PkgPath: pkgpath}
}
// Encode encodes the receiver as gob-encoded data.
diff --git a/gopls/internal/cache/symbols/symbols.go b/gopls/internal/cache/symbols/symbols.go
index 4af0240..df4f978 100644
--- a/gopls/internal/cache/symbols/symbols.go
+++ b/gopls/internal/cache/symbols/symbols.go
@@ -41,8 +41,7 @@
// Decode decodes data from [Package.Encode].
func Decode(data []byte) *Package {
- var pkg Package
- codec.Decode(data, &pkg)
+ pkg := codec.Decode(data)
return &pkg
}
diff --git a/gopls/internal/cache/testfuncs/tests.go b/gopls/internal/cache/testfuncs/tests.go
index e0e3ce1..d9d6ffe 100644
--- a/gopls/internal/cache/testfuncs/tests.go
+++ b/gopls/internal/cache/testfuncs/tests.go
@@ -25,9 +25,7 @@
// Decode decodes the given gob-encoded data as an Index.
func Decode(data []byte) *Index {
- var pkg gobPackage
- packageCodec.Decode(data, &pkg)
- return &Index{pkg}
+ return &Index{packageCodec.Decode(data)}
}
// Encode encodes the receiver as gob-encoded data.
diff --git a/gopls/internal/cache/typerefs/refs.go b/gopls/internal/cache/typerefs/refs.go
index 9b2f6a5..3b9763e 100644
--- a/gopls/internal/cache/typerefs/refs.go
+++ b/gopls/internal/cache/typerefs/refs.go
@@ -801,8 +801,7 @@
}
func decode(pkgIndex *PackageIndex, data []byte) []Class {
- var payload gobClasses
- classesCodec.Decode(data, &payload)
+ payload := classesCodec.Decode(data)
classes := make([]Class, len(payload.Classes))
for i, gobClass := range payload.Classes {
diff --git a/gopls/internal/cache/xrefs/xrefs.go b/gopls/internal/cache/xrefs/xrefs.go
index ae9fc1b..56a9ef0 100644
--- a/gopls/internal/cache/xrefs/xrefs.go
+++ b/gopls/internal/cache/xrefs/xrefs.go
@@ -148,9 +148,7 @@
// Decode decodes a serialized cross-reference index.
// It is suitable for use as a filecache.Get decoder.
func Decode(data []byte) *Index {
- var packages []*gobPackage
- packageCodec.Decode(data, &packages)
- return &Index{packages: packages}
+ return &Index{packages: packageCodec.Decode(data)}
}
// Lookup searches the index for references to any object in the target set,
diff --git a/gopls/internal/filewatcher/poll_watcher.go b/gopls/internal/filewatcher/poll_watcher.go
index 780667b..a48b8c3 100644
--- a/gopls/internal/filewatcher/poll_watcher.go
+++ b/gopls/internal/filewatcher/poll_watcher.go
@@ -348,11 +348,7 @@
func (w *pollWatcher) loadState(root string) (fileState, error) {
key := cacheKey(root)
- state, err := filecache.Get(filewatcherKind, key, func(data []byte) fileState {
- var state fileState
- codec.Decode(data, &state)
- return state
- })
+ state, err := filecache.Get(filewatcherKind, key, codec.Decode)
if err != nil && err != filecache.ErrNotFound {
bug.Reportf("internal error reading shared cache: %v", err)
}
diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go
index 420dede..02956b8 100644
--- a/gopls/internal/settings/settings.go
+++ b/gopls/internal/settings/settings.go
@@ -1100,10 +1100,7 @@
var codec = frob.CodecFor[*Options]()
func (o *Options) Clone() *Options {
- data := codec.Encode(o)
- var clone *Options
- codec.Decode(data, &clone)
- return clone
+ return codec.Decode(codec.Encode(o))
}
// validateDirectoryFilter validates if the filter string
diff --git a/gopls/internal/util/frob/frob.go b/gopls/internal/util/frob/frob.go
index 6ef157b..357198c 100644
--- a/gopls/internal/util/frob/frob.go
+++ b/gopls/internal/util/frob/frob.go
@@ -48,8 +48,14 @@
return Codec[T]{frobFor(reflect.TypeFor[T]())}
}
-func (codec Codec[T]) Encode(v T) []byte { return codec.frob.Encode(v) }
-func (codec Codec[T]) Decode(data []byte, ptr *T) { codec.frob.Decode(data, ptr) }
+func (codec Codec[T]) Encode(v T) []byte { return codec.frob.Encode(v) }
+
+// Decode decodes data and returns the result. It is suitable for use
+// as a filecache.Get decoder.
+func (codec Codec[T]) Decode(data []byte) (v T) {
+ codec.frob.Decode(data, &v)
+ return
+}
var (
frobsMu sync.Mutex
diff --git a/gopls/internal/util/frob/frob_test.go b/gopls/internal/util/frob/frob_test.go
index 5765c96..be01385 100644
--- a/gopls/internal/util/frob/frob_test.go
+++ b/gopls/internal/util/frob/frob_test.go
@@ -34,8 +34,7 @@
E: []byte("hello"),
F: []string{s1, s2},
}
- var y Basics
- codec.Decode(codec.Encode(x), &y)
+ y := codec.Decode(codec.Encode(x))
if !reflect.DeepEqual(x, y) {
t.Fatalf("bad roundtrip: got %#v, want %#v", y, x)
}
@@ -79,8 +78,7 @@
C64: complex(math.MaxFloat32, math.MaxFloat32),
C128: complex(math.MaxFloat64, math.MaxFloat64),
}
- var max2 Ints
- codec.Decode(codec.Encode(max1), &max2)
+ max2 := codec.Decode(codec.Encode(max1))
if !reflect.DeepEqual(max1, max2) {
t.Fatalf("max: bad roundtrip: got %#v, want %#v", max2, max1)
}
@@ -97,8 +95,7 @@
C64: complex(-math.MaxFloat32, -math.MaxFloat32),
C128: complex(-math.MaxFloat64, -math.MaxFloat64),
}
- var min2 Ints
- codec.Decode(codec.Encode(min1), &min2)
+ min2 := codec.Decode(codec.Encode(min1))
if !reflect.DeepEqual(min1, min2) {
t.Fatalf("min: bad roundtrip: got %#v, want %#v", min2, min1)
}
@@ -111,8 +108,7 @@
I32: -1,
I64: -1,
}
- var neg2 Ints
- codec.Decode(codec.Encode(neg1), &neg2)
+ neg2 := codec.Decode(codec.Encode(neg1))
if !reflect.DeepEqual(neg1, neg2) {
t.Fatalf("neg: bad roundtrip: got %#v, want %#v", neg2, neg1)
}