Skip to content

Commit fd40685

Browse files
lint fixes (#151)
* json: use reflect.PointerTo (avoid deprecated naming) * json: use maps.Copy * lint: remove unused consts/fields * exclude stdlib copied tests from linting * json: partial lint fixes
1 parent 7b9ca4e commit fd40685

File tree

8 files changed

+25
-31
lines changed

8 files changed

+25
-31
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
runs-on: ubuntu-latest
4141

4242
steps:
43-
- name: Steup Go
43+
- name: Setup Go
4444
uses: actions/setup-go@v2
4545
with:
4646
go-version: "1.24"

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: "2"
2+
linters:
3+
exclusions:
4+
rules:
5+
# Tests copied from the stdlib are not meant to be linted.
6+
- path: 'golang_(.+_)?test\.go'
7+
source: "^" # regex

benchmarks/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ benchstat := ${GOPATH}/bin/benchstat
7474
all:
7575

7676
$(benchstat):
77-
go install golang.org/x/perf/cmd/benchstat
77+
go install golang.org/x/perf/cmd/benchstat@latest
7878

7979
$(benchmark.cmd.dir)/message.pb.go: $(benchmark.cmd.dir)/message.proto
8080
@protoc -I. \

json/codec.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding"
55
"encoding/json"
66
"fmt"
7+
"maps"
78
"math/big"
89
"reflect"
910
"sort"
@@ -73,12 +74,9 @@ func cacheLoad() map[unsafe.Pointer]codec {
7374

7475
func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) {
7576
newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1)
77+
maps.Copy(newCodecs, oldCodecs)
7678
newCodecs[typeid(typ)] = cod
7779

78-
for t, c := range oldCodecs {
79-
newCodecs[t] = c
80-
}
81-
8280
cache.Store(&newCodecs)
8381
}
8482

@@ -205,7 +203,7 @@ func constructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr b
205203
c = constructUnsupportedTypeCodec(t)
206204
}
207205

208-
p := reflect.PtrTo(t)
206+
p := reflect.PointerTo(t)
209207

210208
if canAddr {
211209
switch {
@@ -291,7 +289,7 @@ func constructSliceCodec(t reflect.Type, seen map[reflect.Type]*structType) code
291289
// Go 1.7+ behavior: slices of byte types (and aliases) may override the
292290
// default encoding and decoding behaviors by implementing marshaler and
293291
// unmarshaler interfaces.
294-
p := reflect.PtrTo(e)
292+
p := reflect.PointerTo(e)
295293
c := codec{}
296294

297295
switch {
@@ -391,7 +389,7 @@ func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec
391389
kc := codec{}
392390
vc := constructCodec(v, seen, false)
393391

394-
if k.Implements(textMarshalerType) || reflect.PtrTo(k).Implements(textUnmarshalerType) {
392+
if k.Implements(textMarshalerType) || reflect.PointerTo(k).Implements(textUnmarshalerType) {
395393
kc.encode = constructTextMarshalerEncodeFunc(k, false)
396394
kc.decode = constructTextUnmarshalerDecodeFunc(k, true)
397395

@@ -972,7 +970,6 @@ type structType struct {
972970
ficaseIndex map[string]*structField
973971
keyset []byte
974972
typ reflect.Type
975-
inlined bool
976973
}
977974

978975
type structField struct {
@@ -1095,10 +1092,10 @@ var (
10951092
timeType = reflect.TypeOf(time.Time{})
10961093
rawMessageType = reflect.TypeOf(RawMessage(nil))
10971094

1098-
numberPtrType = reflect.PtrTo(numberType)
1099-
durationPtrType = reflect.PtrTo(durationType)
1100-
timePtrType = reflect.PtrTo(timeType)
1101-
rawMessagePtrType = reflect.PtrTo(rawMessageType)
1095+
numberPtrType = reflect.PointerTo(numberType)
1096+
durationPtrType = reflect.PointerTo(durationType)
1097+
timePtrType = reflect.PointerTo(timeType)
1098+
rawMessagePtrType = reflect.PointerTo(rawMessageType)
11021099

11031100
sliceInterfaceType = reflect.TypeOf(([]any)(nil))
11041101
sliceStringType = reflect.TypeOf(([]any)(nil))

json/decode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ func (d decoder) decodeMaybeEmptyInterface(b []byte, p unsafe.Pointer, t reflect
14101410
return d.decodeUnmarshalTypeError(b, p, t)
14111411
}
14121412

1413-
func (d decoder) decodeUnmarshalTypeError(b []byte, p unsafe.Pointer, t reflect.Type) ([]byte, error) {
1413+
func (d decoder) decodeUnmarshalTypeError(b []byte, _ unsafe.Pointer, t reflect.Type) ([]byte, error) {
14141414
v, b, _, err := d.parseValue(b)
14151415
if err != nil {
14161416
return b, err
@@ -1500,7 +1500,7 @@ func (d decoder) decodeTextUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Typ
15001500
value = "array"
15011501
}
15021502

1503-
return b, &UnmarshalTypeError{Value: value, Type: reflect.PtrTo(t)}
1503+
return b, &UnmarshalTypeError{Value: value, Type: reflect.PointerTo(t)}
15041504
}
15051505

15061506
func (d decoder) prependField(key, field string) string {

json/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
type Delim = json.Delim
1616

1717
// InvalidUTF8Error is documented at https://golang.org/pkg/encoding/json/#InvalidUTF8Error
18-
type InvalidUTF8Error = json.InvalidUTF8Error
18+
type InvalidUTF8Error = json.InvalidUTF8Error //nolint:staticcheck // compat.
1919

2020
// InvalidUnmarshalError is documented at https://golang.org/pkg/encoding/json/#InvalidUnmarshalError
2121
type InvalidUnmarshalError = json.InvalidUnmarshalError
@@ -39,7 +39,7 @@ type SyntaxError = json.SyntaxError
3939
type Token = json.Token
4040

4141
// UnmarshalFieldError is documented at https://golang.org/pkg/encoding/json/#UnmarshalFieldError
42-
type UnmarshalFieldError = json.UnmarshalFieldError
42+
type UnmarshalFieldError = json.UnmarshalFieldError //nolint:staticcheck // compat.
4343

4444
// UnmarshalTypeError is documented at https://golang.org/pkg/encoding/json/#UnmarshalTypeError
4545
type UnmarshalTypeError = json.UnmarshalTypeError

json/json_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ var testValues = [...]any{
240240
A string `json:"name"`
241241
B string `json:"-"`
242242
C string `json:",omitempty"`
243-
D map[string]any `json:",string"`
243+
D map[string]any `json:",string"` //nolint:staticcheck // intentional
244244
e string
245245
}{A: "Luke", D: map[string]any{"answer": float64(42)}},
246246
struct{ point }{point{1, 2}},
@@ -880,12 +880,11 @@ func TestDecodeLines(t *testing.T) {
880880
t.Run(test.desc, func(t *testing.T) {
881881
d := NewDecoder(test.reader)
882882
var count int
883-
var err error
884883
for {
885884
var o obj
886-
err = d.Decode(&o)
885+
err := d.Decode(&o)
887886
if err != nil {
888-
if err == io.EOF {
887+
if errors.Is(err, io.EOF) {
889888
break
890889
}
891890

@@ -904,10 +903,6 @@ func TestDecodeLines(t *testing.T) {
904903
count++
905904
}
906905

907-
if err != nil && err != io.EOF {
908-
t.Error(err)
909-
}
910-
911906
if count != test.expectCount {
912907
t.Errorf("expected %d objects, got %d", test.expectCount, count)
913908
}

json/parse.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ const (
2121
cr = '\r'
2222
)
2323

24-
const (
25-
escape = '\\'
26-
quote = '"'
27-
)
28-
2924
func internalParseFlags(b []byte) (flags ParseFlags) {
3025
// Don't consider surrounding whitespace
3126
b = skipSpaces(b)

0 commit comments

Comments
 (0)