Skip to content

Commit 385b50b

Browse files
authored
Merge pull request #1 from breml/fix-typo
Fix typos
2 parents ec0bee2 + 948106f commit 385b50b

File tree

4 files changed

+143
-139
lines changed

4 files changed

+143
-139
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ This is ok, as long as the struct is not altered in such a way, that could poten
4747
to `json.Marshal` returning an error.
4848

4949
`errchkjson` allows you to lint your code such that the above error returned from `json.Marshal`
50-
can be omitted while still staying save, because as soon as an unsafe type is added to the
50+
can be omitted while still staying safe, because as soon as an unsafe type is added to the
5151
response type, the linter will warn you.
5252

5353
## Installation

errchkjson.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package errchkjson defines an Analyzer that finds places, where it is
2-
// save to omit checking the error returned from json.Marshal.
2+
// safe to omit checking the error returned from json.Marshal.
33
package errchkjson
44

55
import (
@@ -123,7 +123,7 @@ func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, n ast.Node, fnName s
123123
if err == nil && !blankIdentifier && !e.omitSafe {
124124
pass.Reportf(n.Pos(), "Error return value of `%s` is checked but passed argument is safe", fnName)
125125
}
126-
// Report an error, if err for json.Marshal is not checked and save types are omitted
126+
// Report an error, if err for json.Marshal is not checked and safe types are omitted
127127
if err == nil && blankIdentifier && e.omitSafe {
128128
pass.Reportf(n.Pos(), "Error return value of `%s` is not checked", fnName)
129129
}

testdata/src/nosafe/a.go

+70-68
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func (mt marshalText) MarshalText() ([]byte, error) {
1616

1717
var _ encoding.TextMarshaler = marshalText(struct{}{})
1818

19-
// JSONMarshalSaveTypesWithNoSafe contains a multitude of test cases to marshal different combinations of types to JSON,
20-
// that are save, that is, they will never return an error, if these types are marshaled to JSON.
21-
func JSONMarshalSaveTypesWithNoSafe() {
19+
// JSONMarshalSafeTypesWithNoSafe contains a multitude of test cases to marshal different combinations of types to JSON,
20+
// that are safe, that is, they will never return an error, if these types are marshaled to JSON.
21+
func JSONMarshalSafeTypesWithNoSafe() {
2222
var err error
2323

2424
_, _ = json.Marshal(nil) // want "Error return value of `encoding/json.Marshal` is not checked"
@@ -249,37 +249,39 @@ func JSONMarshalSaveTypesWithNoSafe() {
249249
_ = err
250250
}
251251

252-
type structKey struct{ id int }
253-
type ExportedUnsafeAndInvalidStruct struct { // unsafe unexported but ommited
254-
F64 float64
255-
F64Ptr *float64
256-
F64Slice []float64
257-
F64Array [10]float64
258-
MapStrF64 map[string]float64
259-
MapEIStr map[interface{}]string
260-
Number json.Number
261-
NumberPtr *json.Number
262-
NumberSlice []json.Number
263-
MapNumberStr map[json.Number]string
264-
Ei interface{}
265-
Stringer fmt.Stringer
266-
Mt marshalText
267-
MapMarshalTextString map[marshalText]string
268-
269-
C128 complex128
270-
C128Ptr *complex128
271-
C128Slice []complex128
272-
C128Array [10]complex128
273-
MapBoolStr map[bool]string
274-
MapF64Str map[float64]string
275-
F func()
276-
Ch chan struct{}
277-
UnsafePtr unsafe.Pointer
278-
MapStructStr map[structKey]string
279-
}
252+
type (
253+
structKey struct{ id int }
254+
ExportedUnsafeAndInvalidStruct struct { // unsafe unexported but omitted
255+
F64 float64
256+
F64Ptr *float64
257+
F64Slice []float64
258+
F64Array [10]float64
259+
MapStrF64 map[string]float64
260+
MapEIStr map[interface{}]string
261+
Number json.Number
262+
NumberPtr *json.Number
263+
NumberSlice []json.Number
264+
MapNumberStr map[json.Number]string
265+
Ei interface{}
266+
Stringer fmt.Stringer
267+
Mt marshalText
268+
MapMarshalTextString map[marshalText]string
269+
270+
C128 complex128
271+
C128Ptr *complex128
272+
C128Slice []complex128
273+
C128Array [10]complex128
274+
MapBoolStr map[bool]string
275+
MapF64Str map[float64]string
276+
F func()
277+
Ch chan struct{}
278+
UnsafePtr unsafe.Pointer
279+
MapStructStr map[structKey]string
280+
}
281+
)
280282

281-
// JSONMarshalSaveStructWithUnexportedFields contains a struct with unexported, unsafe fields.
282-
func JSONMarshalSaveStructWithUnexportedFieldsWithNoSafe() {
283+
// JSONMarshalSafeStructWithUnexportedFieldsWithNoSafe contains a struct with unexported, unsafe fields.
284+
func JSONMarshalSafeStructWithUnexportedFieldsWithNoSafe() {
283285
var err error
284286

285287
var unexportedInStruct struct {
@@ -343,49 +345,49 @@ func JSONMarshalSaveStructWithUnexportedFieldsWithNoSafe() {
343345
_ = err
344346
}
345347

346-
// JSONMarshalSaveStructWithOmittedFields contains a struct with omitted, unsafe fields.
347-
func JSONMarshalSaveStructWithOmittedFieldsWithNoSafe() {
348+
// JSONMarshalSafeStructWithOmittedFieldsWithNoSafe contains a struct with omitted, unsafe fields.
349+
func JSONMarshalSafeStructWithOmittedFieldsWithNoSafe() {
348350
var err error
349351

350-
var ommitInStruct struct {
352+
var omitInStruct struct {
351353
Bool bool // safe exported
352354

353-
F64 float64 `json:"-"` // unsafe exported but ommited
354-
F64Ptr *float64 `json:"-"` // unsafe exported but ommited
355-
F64Slice []float64 `json:"-"` // unsafe exported but ommited
356-
F64Array [10]float64 `json:"-"` // unsafe exported but ommited
357-
MapStrF64 map[string]float64 `json:"-"` // unsafe exported but ommited
358-
MapEIStr map[interface{}]string `json:"-"` // unsafe exported but ommited
359-
Number json.Number `json:"-"` // unsafe exported but ommited
360-
NumberPtr *json.Number `json:"-"` // unsafe exported but ommited
361-
NumberSlice []json.Number `json:"-"` // unsafe exported but ommited
362-
MapNumberStr map[json.Number]string `json:"-"` // unsafe exported but ommited
363-
Ei interface{} `json:"-"` // unsafe exported but ommited
364-
Stringer fmt.Stringer `json:"-"` // unsafe exported but ommited
365-
Mt marshalText `json:"-"` // unsafe exported but ommited
366-
MapMarshalTextString map[marshalText]string `json:"-"` // unsafe exported but ommited
367-
ExportedStruct ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but ommited
368-
ExportedStructPtr *ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but ommited
369-
370-
C128 complex128 `json:"-"` // invalid exported but ommited
371-
C128Slice []complex128 `json:"-"` // invalid exported but ommited
372-
C128Array [10]complex128 `json:"-"` // invalid exported but ommited
373-
MapBoolStr map[bool]string `json:"-"` // invalid exported but ommited
374-
MapF64Str map[float64]string `json:"-"` // invalid exported but ommited
375-
F func() `json:"-"` // invalid exported but ommited
376-
Ch chan struct{} `json:"-"` // invalid exported but ommited
377-
UnsafePtr unsafe.Pointer `json:"-"` // invalid exported but ommited
378-
MapStructStr map[structKey]string `json:"-"` // invalid exported but ommited
355+
F64 float64 `json:"-"` // unsafe exported but omitted
356+
F64Ptr *float64 `json:"-"` // unsafe exported but omitted
357+
F64Slice []float64 `json:"-"` // unsafe exported but omitted
358+
F64Array [10]float64 `json:"-"` // unsafe exported but omitted
359+
MapStrF64 map[string]float64 `json:"-"` // unsafe exported but omitted
360+
MapEIStr map[interface{}]string `json:"-"` // unsafe exported but omitted
361+
Number json.Number `json:"-"` // unsafe exported but omitted
362+
NumberPtr *json.Number `json:"-"` // unsafe exported but omitted
363+
NumberSlice []json.Number `json:"-"` // unsafe exported but omitted
364+
MapNumberStr map[json.Number]string `json:"-"` // unsafe exported but omitted
365+
Ei interface{} `json:"-"` // unsafe exported but omitted
366+
Stringer fmt.Stringer `json:"-"` // unsafe exported but omitted
367+
Mt marshalText `json:"-"` // unsafe exported but omitted
368+
MapMarshalTextString map[marshalText]string `json:"-"` // unsafe exported but omitted
369+
ExportedStruct ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
370+
ExportedStructPtr *ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
371+
372+
C128 complex128 `json:"-"` // invalid exported but omitted
373+
C128Slice []complex128 `json:"-"` // invalid exported but omitted
374+
C128Array [10]complex128 `json:"-"` // invalid exported but omitted
375+
MapBoolStr map[bool]string `json:"-"` // invalid exported but omitted
376+
MapF64Str map[float64]string `json:"-"` // invalid exported but omitted
377+
F func() `json:"-"` // invalid exported but omitted
378+
Ch chan struct{} `json:"-"` // invalid exported but omitted
379+
UnsafePtr unsafe.Pointer `json:"-"` // invalid exported but omitted
380+
MapStructStr map[structKey]string `json:"-"` // invalid exported but omitted
379381
}
380-
_ = ommitInStruct.MapStructStr[structKey{1}]
381-
_, _ = json.Marshal(ommitInStruct) // want "Error return value of `encoding/json.Marshal` is not checked"
382-
_, err = json.Marshal(ommitInStruct) // struct containing unsafe but omitted, exported fields is safe, but omit-safe is set
382+
_ = omitInStruct.MapStructStr[structKey{1}]
383+
_, _ = json.Marshal(omitInStruct) // want "Error return value of `encoding/json.Marshal` is not checked"
384+
_, err = json.Marshal(omitInStruct) // struct containing unsafe but omitted, exported fields is safe, but omit-safe is set
383385
_ = err
384386
}
385387

386-
// JSONMarshalUnsaveTypes contains a multitude of test cases to marshal different combinations of types to JSON,
388+
// JSONMarshalUnsafeTypes contains a multitude of test cases to marshal different combinations of types to JSON,
387389
// that can potentially lead to json.Marshal returning an error.
388-
func JSONMarshalUnsaveTypes() {
390+
func JSONMarshalUnsafeTypes() {
389391
var err error
390392

391393
var f32 float32
@@ -585,7 +587,7 @@ func JSONMarshalInvalidTypes() {
585587
_, err = json.Marshal(mapStructStr) // want "`encoding/json.Marshal` for unsupported type `nosafe.structKey` as map key found"
586588
_ = err
587589

588-
var f = func() {}
590+
f := func() {}
589591
_, _ = json.Marshal(f) // want "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
590592
_, err = json.Marshal(f) // want "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
591593
_ = err

testdata/src/standard/a.go

+70-68
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func (mt marshalText) MarshalText() ([]byte, error) {
1616

1717
var _ encoding.TextMarshaler = marshalText(struct{}{})
1818

19-
// JSONMarshalSaveTypes contains a multitude of test cases to marshal different combinations of types to JSON,
20-
// that are save, that is, they will never return an error, if these types are marshaled to JSON.
21-
func JSONMarshalSaveTypes() {
19+
// JSONMarshalSafeTypes contains a multitude of test cases to marshal different combinations of types to JSON,
20+
// that are safe, that is, they will never return an error, if these types are marshaled to JSON.
21+
func JSONMarshalSafeTypes() {
2222
var err error
2323

2424
_, _ = json.Marshal(nil) // nil is safe
@@ -249,37 +249,39 @@ func JSONMarshalSaveTypes() {
249249
_ = err
250250
}
251251

252-
type structKey struct{ id int }
253-
type ExportedUnsafeAndInvalidStruct struct { // unsafe unexported but ommited
254-
F64 float64
255-
F64Ptr *float64
256-
F64Slice []float64
257-
F64Array [10]float64
258-
MapStrF64 map[string]float64
259-
MapEIStr map[interface{}]string
260-
Number json.Number
261-
NumberPtr *json.Number
262-
NumberSlice []json.Number
263-
MapNumberStr map[json.Number]string
264-
Ei interface{}
265-
Stringer fmt.Stringer
266-
Mt marshalText
267-
MapMarshalTextString map[marshalText]string
268-
269-
C128 complex128
270-
C128Ptr *complex128
271-
C128Slice []complex128
272-
C128Array [10]complex128
273-
MapBoolStr map[bool]string
274-
MapF64Str map[float64]string
275-
F func()
276-
Ch chan struct{}
277-
UnsafePtr unsafe.Pointer
278-
MapStructStr map[structKey]string
279-
}
252+
type (
253+
structKey struct{ id int }
254+
ExportedUnsafeAndInvalidStruct struct { // unsafe unexported but omitted
255+
F64 float64
256+
F64Ptr *float64
257+
F64Slice []float64
258+
F64Array [10]float64
259+
MapStrF64 map[string]float64
260+
MapEIStr map[interface{}]string
261+
Number json.Number
262+
NumberPtr *json.Number
263+
NumberSlice []json.Number
264+
MapNumberStr map[json.Number]string
265+
Ei interface{}
266+
Stringer fmt.Stringer
267+
Mt marshalText
268+
MapMarshalTextString map[marshalText]string
269+
270+
C128 complex128
271+
C128Ptr *complex128
272+
C128Slice []complex128
273+
C128Array [10]complex128
274+
MapBoolStr map[bool]string
275+
MapF64Str map[float64]string
276+
F func()
277+
Ch chan struct{}
278+
UnsafePtr unsafe.Pointer
279+
MapStructStr map[structKey]string
280+
}
281+
)
280282

281-
// JSONMarshalSaveStructWithUnexportedFields contains a struct with unexported, unsafe fields.
282-
func JSONMarshalSaveStructWithUnexportedFields() {
283+
// JSONMarshalSafeStructWithUnexportedFields contains a struct with unexported, unsafe fields.
284+
func JSONMarshalSafeStructWithUnexportedFields() {
283285
var err error
284286

285287
var unexportedInStruct struct {
@@ -343,49 +345,49 @@ func JSONMarshalSaveStructWithUnexportedFields() {
343345
_ = err
344346
}
345347

346-
// JSONMarshalSaveStructWithOmittedFields contains a struct with omitted, unsafe fields.
347-
func JSONMarshalSaveStructWithOmittedFields() {
348+
// JSONMarshalSafeStructWithOmittedFields contains a struct with omitted, unsafe fields.
349+
func JSONMarshalSafeStructWithOmittedFields() {
348350
var err error
349351

350-
var ommitInStruct struct {
352+
var omitInStruct struct {
351353
Bool bool // safe exported
352354

353-
F64 float64 `json:"-"` // unsafe exported but ommited
354-
F64Ptr *float64 `json:"-"` // unsafe exported but ommited
355-
F64Slice []float64 `json:"-"` // unsafe exported but ommited
356-
F64Array [10]float64 `json:"-"` // unsafe exported but ommited
357-
MapStrF64 map[string]float64 `json:"-"` // unsafe exported but ommited
358-
MapEIStr map[interface{}]string `json:"-"` // unsafe exported but ommited
359-
Number json.Number `json:"-"` // unsafe exported but ommited
360-
NumberPtr *json.Number `json:"-"` // unsafe exported but ommited
361-
NumberSlice []json.Number `json:"-"` // unsafe exported but ommited
362-
MapNumberStr map[json.Number]string `json:"-"` // unsafe exported but ommited
363-
Ei interface{} `json:"-"` // unsafe exported but ommited
364-
Stringer fmt.Stringer `json:"-"` // unsafe exported but ommited
365-
Mt marshalText `json:"-"` // unsafe exported but ommited
366-
MapMarshalTextString map[marshalText]string `json:"-"` // unsafe exported but ommited
367-
ExportedStruct ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but ommited
368-
ExportedStructPtr *ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but ommited
369-
370-
C128 complex128 `json:"-"` // invalid exported but ommited
371-
C128Slice []complex128 `json:"-"` // invalid exported but ommited
372-
C128Array [10]complex128 `json:"-"` // invalid exported but ommited
373-
MapBoolStr map[bool]string `json:"-"` // invalid exported but ommited
374-
MapF64Str map[float64]string `json:"-"` // invalid exported but ommited
375-
F func() `json:"-"` // invalid exported but ommited
376-
Ch chan struct{} `json:"-"` // invalid exported but ommited
377-
UnsafePtr unsafe.Pointer `json:"-"` // invalid exported but ommited
378-
MapStructStr map[structKey]string `json:"-"` // invalid exported but ommited
355+
F64 float64 `json:"-"` // unsafe exported but omitted
356+
F64Ptr *float64 `json:"-"` // unsafe exported but omitted
357+
F64Slice []float64 `json:"-"` // unsafe exported but omitted
358+
F64Array [10]float64 `json:"-"` // unsafe exported but omitted
359+
MapStrF64 map[string]float64 `json:"-"` // unsafe exported but omitted
360+
MapEIStr map[interface{}]string `json:"-"` // unsafe exported but omitted
361+
Number json.Number `json:"-"` // unsafe exported but omitted
362+
NumberPtr *json.Number `json:"-"` // unsafe exported but omitted
363+
NumberSlice []json.Number `json:"-"` // unsafe exported but omitted
364+
MapNumberStr map[json.Number]string `json:"-"` // unsafe exported but omitted
365+
Ei interface{} `json:"-"` // unsafe exported but omitted
366+
Stringer fmt.Stringer `json:"-"` // unsafe exported but omitted
367+
Mt marshalText `json:"-"` // unsafe exported but omitted
368+
MapMarshalTextString map[marshalText]string `json:"-"` // unsafe exported but omitted
369+
ExportedStruct ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
370+
ExportedStructPtr *ExportedUnsafeAndInvalidStruct `json:"-"` // unsafe exported but omitted
371+
372+
C128 complex128 `json:"-"` // invalid exported but omitted
373+
C128Slice []complex128 `json:"-"` // invalid exported but omitted
374+
C128Array [10]complex128 `json:"-"` // invalid exported but omitted
375+
MapBoolStr map[bool]string `json:"-"` // invalid exported but omitted
376+
MapF64Str map[float64]string `json:"-"` // invalid exported but omitted
377+
F func() `json:"-"` // invalid exported but omitted
378+
Ch chan struct{} `json:"-"` // invalid exported but omitted
379+
UnsafePtr unsafe.Pointer `json:"-"` // invalid exported but omitted
380+
MapStructStr map[structKey]string `json:"-"` // invalid exported but omitted
379381
}
380-
_ = ommitInStruct.MapStructStr[structKey{1}]
381-
_, _ = json.Marshal(ommitInStruct) // struct containing unsafe but omitted, exported fields is safe
382-
_, err = json.Marshal(ommitInStruct) // want "Error return value of `encoding/json.Marshal` is checked but passed argument is safe"
382+
_ = omitInStruct.MapStructStr[structKey{1}]
383+
_, _ = json.Marshal(omitInStruct) // struct containing unsafe but omitted, exported fields is safe
384+
_, err = json.Marshal(omitInStruct) // want "Error return value of `encoding/json.Marshal` is checked but passed argument is safe"
383385
_ = err
384386
}
385387

386-
// JSONMarshalUnsaveTypes contains a multitude of test cases to marshal different combinations of types to JSON,
388+
// JSONMarshalUnsafeTypes contains a multitude of test cases to marshal different combinations of types to JSON,
387389
// that can potentially lead to json.Marshal returning an error.
388-
func JSONMarshalUnsaveTypes() {
390+
func JSONMarshalUnsafeTypes() {
389391
var err error
390392

391393
var f32 float32
@@ -585,7 +587,7 @@ func JSONMarshalInvalidTypes() {
585587
_, err = json.Marshal(mapStructStr) // want "`encoding/json.Marshal` for unsupported type `standard.structKey` as map key found"
586588
_ = err
587589

588-
var f = func() {}
590+
f := func() {}
589591
_, _ = json.Marshal(f) // want "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
590592
_, err = json.Marshal(f) // want "`encoding/json.Marshal` for unsupported type `func\\(\\)` found"
591593
_ = err

0 commit comments

Comments
 (0)