Skip to content

Skipping error checks for json.Encoder.Encode or json.MarshalIndent is not safe #14

@scop

Description

@scop
$ cat t.go
package main

import (
	"encoding/json"
	"os"
)

type Foo struct {
	X string
}

func main() {
	err := json.NewEncoder(os.Stdout).Encode(Foo{"bar"})
	if err != nil {
		panic("meh")
	}
}
$ ./errchkjson t.go
t.go:13:9: Error return value of `(*encoding/json.Encoder).Encode` is checked but passed argument is safe
$ ./errchkjson -V
errchkjson version 0.2.3
commit: 3aae21891fb6867e0ae247403f8eb67c0573c5d5
built at: 2022-02-08T16:17:38Z
module version: v0.2.3, checksum: h1:97eGTmR/w0paL2SwfRPI1jaAZHaH/fXnxWTw2eEIqE0=
goos: linux
goarch: amd64

json.Encoder.Encode can return an error due to a number of reasons unrelated to the argument's general JSON marshal safety:

The same applies to json.MarshalIndent:

I think errchkjson should not encourage skipping error checks for these functions at all.

(Haven't thought through if there are other messages in errchkjson that should be adjusted due to these considerations, but I suspect there might be some.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions