Skip to content

Commit cc97728

Browse files
authored
Merge pull request #2 from breml/fix-pure-call-expr
Fix check for pure CallExpr
2 parents 385b50b + b132814 commit cc97728

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

errchkjson.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
4242
}
4343

4444
ce, ok := n.(*ast.CallExpr)
45-
if ok && e.omitSafe {
45+
if ok {
4646
fn, _ := typeutil.Callee(pass.TypesInfo, ce).(*types.Func)
4747
if fn == nil {
4848
return true
4949
}
5050

5151
switch fn.FullName() {
52-
case "encoding/json.Marshal", "encoding/json.MarshalIndent", "(*encoding/json.Encoder).Encode":
53-
pass.Reportf(n.Pos(), "Error return value of `%s` is not checked", fn.FullName())
54-
return false
52+
case "encoding/json.Marshal", "encoding/json.MarshalIndent":
53+
e.handleJSONMarshal(pass, ce, fn.FullName(), true)
54+
case "(*encoding/json.Encoder).Encode":
55+
e.handleJSONMarshal(pass, ce, fn.FullName(), true)
56+
default:
57+
return true
5558
}
56-
return true
59+
return false
5760
}
5861

5962
as, ok := n.(*ast.AssignStmt)
@@ -73,9 +76,9 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
7376

7477
switch fn.FullName() {
7578
case "encoding/json.Marshal", "encoding/json.MarshalIndent":
76-
e.handleJSONMarshal(pass, n, fn.FullName(), 1)
79+
e.handleJSONMarshal(pass, ce, fn.FullName(), blankIdentifier(as.Lhs[1]))
7780
case "(*encoding/json.Encoder).Encode":
78-
e.handleJSONMarshal(pass, n, fn.FullName(), 0)
81+
e.handleJSONMarshal(pass, ce, fn.FullName(), blankIdentifier(as.Lhs[0]))
7982
default:
8083
return true
8184
}
@@ -86,22 +89,21 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
8689
return nil, nil
8790
}
8891

89-
func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, n ast.Node, fnName string, errPos int) {
90-
as := n.(*ast.AssignStmt)
91-
ce := as.Rhs[0].(*ast.CallExpr)
92-
93-
var blankIdentifier bool
94-
if errIdent, ok := as.Lhs[errPos].(*ast.Ident); ok {
92+
func blankIdentifier(n ast.Expr) bool {
93+
if errIdent, ok := n.(*ast.Ident); ok {
9594
if errIdent.Name == "_" {
96-
blankIdentifier = true
95+
return true
9796
}
9897
}
98+
return false
99+
}
99100

101+
func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, ce *ast.CallExpr, fnName string, blankIdentifier bool) {
100102
t := pass.TypesInfo.TypeOf(ce.Args[0])
101103
if t == nil {
102104
// Not sure, if this is at all possible
103105
if blankIdentifier {
104-
pass.Reportf(n.Pos(), "Type of argument to `%s` could not be evaluated and error return value is not checked", fnName)
106+
pass.Reportf(ce.Pos(), "Type of argument to `%s` could not be evaluated and error return value is not checked", fnName)
105107
}
106108
return
107109
}
@@ -113,19 +115,19 @@ func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, n ast.Node, fnName s
113115
err := jsonSafe(t)
114116
if err != nil {
115117
if _, ok := err.(unsupported); ok {
116-
pass.Reportf(n.Pos(), "`%s` for %v", fnName, err)
118+
pass.Reportf(ce.Pos(), "`%s` for %v", fnName, err)
117119
return
118120
}
119121
if blankIdentifier {
120-
pass.Reportf(n.Pos(), "Error return value of `%s` is not checked: %v", fnName, err)
122+
pass.Reportf(ce.Pos(), "Error return value of `%s` is not checked: %v", fnName, err)
121123
}
122124
}
123125
if err == nil && !blankIdentifier && !e.omitSafe {
124-
pass.Reportf(n.Pos(), "Error return value of `%s` is checked but passed argument is safe", fnName)
126+
pass.Reportf(ce.Pos(), "Error return value of `%s` is checked but passed argument is safe", fnName)
125127
}
126128
// Report an error, if err for json.Marshal is not checked and safe types are omitted
127129
if err == nil && blankIdentifier && e.omitSafe {
128-
pass.Reportf(n.Pos(), "Error return value of `%s` is not checked", fnName)
130+
pass.Reportf(ce.Pos(), "Error return value of `%s` is not checked", fnName)
129131
}
130132
}
131133

testdata/src/nosafe/a.go

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ func JSONMarshalUnsafeTypes() {
391391
var err error
392392

393393
var f32 float32
394+
json.Marshal(f32) // want "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
394395
_, _ = json.Marshal(f32) // want "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
395396
_, err = json.Marshal(f32) // err is checked
396397
_ = err
@@ -514,6 +515,7 @@ func JSONMarshalInvalidTypes() {
514515
var err error
515516

516517
var c64 complex64
518+
json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
517519
_, _ = json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
518520
_, err = json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
519521
_ = err

testdata/src/standard/a.go

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ func JSONMarshalUnsafeTypes() {
391391
var err error
392392

393393
var f32 float32
394+
json.Marshal(f32) // want "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
394395
_, _ = json.Marshal(f32) // want "Error return value of `encoding/json.Marshal` is not checked: unsafe type `float32` found"
395396
_, err = json.Marshal(f32) // err is checked
396397
_ = err
@@ -514,6 +515,7 @@ func JSONMarshalInvalidTypes() {
514515
var err error
515516

516517
var c64 complex64
518+
json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
517519
_, _ = json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
518520
_, err = json.Marshal(c64) // want "`encoding/json.Marshal` for unsupported type `complex64` found"
519521
_ = err

0 commit comments

Comments
 (0)