You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46Lines changed: 46 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,52 @@
4
4
5
5
Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omited.
6
6
7
+
Consider this [http.Handler](https://pkg.go.dev/net/http#Handler):
8
+
9
+
```Go
10
+
funcJSONHelloWorld(whttp.ResponseWriter, r *http.Request) {
11
+
response:=struct {
12
+
Message string
13
+
Code int
14
+
}{
15
+
Message: "Hello World",
16
+
Code: 200,
17
+
}
18
+
19
+
body, err:= json.Marshal(response)
20
+
if err != nil {
21
+
panic(err) // unreachable, because json encoding of a struct with just a string and an int will never return an error.
22
+
}
23
+
24
+
w.Write(body)
25
+
}
26
+
```
27
+
28
+
Because the `panic` is not possible to happen, one might refactor the code like this:
29
+
30
+
```Go
31
+
funcJSONHelloWorld(whttp.ResponseWriter, r *http.Request) {
32
+
response:=struct {
33
+
Message string
34
+
Code int
35
+
}{
36
+
Message: "Hello World",
37
+
Code: 200,
38
+
}
39
+
40
+
body, _:= json.Marshal(response)
41
+
42
+
w.Write(body)
43
+
}
44
+
```
45
+
46
+
This is ok, as long as the struct is not altered in such a way, that could potentially lead
47
+
to `json.Marshal` returning an error.
48
+
49
+
`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
51
+
response type, the linter will warn you.
52
+
7
53
## Installation
8
54
9
55
Download `errchkjson` from the [releases](https://github.com/breml/errchkjson/releases) or get the latest version from source with:
Copy file name to clipboardExpand all lines: errchkjson.go
+23-14Lines changed: 23 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
package errchkjson
4
4
5
5
import (
6
+
"flag"
6
7
"fmt"
7
8
"go/ast"
8
9
"go/token"
@@ -13,27 +14,35 @@ import (
13
14
"golang.org/x/tools/go/types/typeutil"
14
15
)
15
16
16
-
varAnalyzer=&analysis.Analyzer{
17
-
Name: "errchkjson",
18
-
Doc: "Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omited.",
19
-
Run: run,
17
+
typeerrchkjsonstruct {
18
+
omitSafebool// -omit-safe flag
20
19
}
21
20
22
-
varomitSafebool// -omit-safe flag
21
+
// NewAnalyzer returns a new errchkjson analyzer.
22
+
funcNewAnalyzer() *analysis.Analyzer {
23
+
errchkjson:=&errchkjson{}
23
24
24
-
funcinit() {
25
-
Analyzer.Flags.BoolVar(&omitSafe, "omit-safe", false, "if omit-safe is true, checking of safe returns is omitted")
25
+
a:=&analysis.Analyzer{
26
+
Name: "errchkjson",
27
+
Doc: "Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omitted.",
28
+
Run: errchkjson.run,
29
+
}
30
+
31
+
a.Flags.Init("errchkjson", flag.ExitOnError)
32
+
a.Flags.BoolVar(&errchkjson.omitSafe, "omit-safe", false, "if omit-safe is true, checking of safe returns is omitted")
0 commit comments