@@ -42,18 +42,21 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
42
42
}
43
43
44
44
ce , ok := n .(* ast.CallExpr )
45
- if ok && e . omitSafe {
45
+ if ok {
46
46
fn , _ := typeutil .Callee (pass .TypesInfo , ce ).(* types.Func )
47
47
if fn == nil {
48
48
return true
49
49
}
50
50
51
51
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
55
58
}
56
- return true
59
+ return false
57
60
}
58
61
59
62
as , ok := n .(* ast.AssignStmt )
@@ -73,9 +76,9 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
73
76
74
77
switch fn .FullName () {
75
78
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 ]) )
77
80
case "(*encoding/json.Encoder).Encode" :
78
- e .handleJSONMarshal (pass , n , fn .FullName (), 0 )
81
+ e .handleJSONMarshal (pass , ce , fn .FullName (), blankIdentifier ( as . Lhs [ 0 ]) )
79
82
default :
80
83
return true
81
84
}
@@ -86,22 +89,21 @@ func (e *errchkjson) run(pass *analysis.Pass) (interface{}, error) {
86
89
return nil , nil
87
90
}
88
91
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 {
95
94
if errIdent .Name == "_" {
96
- blankIdentifier = true
95
+ return true
97
96
}
98
97
}
98
+ return false
99
+ }
99
100
101
+ func (e * errchkjson ) handleJSONMarshal (pass * analysis.Pass , ce * ast.CallExpr , fnName string , blankIdentifier bool ) {
100
102
t := pass .TypesInfo .TypeOf (ce .Args [0 ])
101
103
if t == nil {
102
104
// Not sure, if this is at all possible
103
105
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 )
105
107
}
106
108
return
107
109
}
@@ -113,19 +115,19 @@ func (e *errchkjson) handleJSONMarshal(pass *analysis.Pass, n ast.Node, fnName s
113
115
err := jsonSafe (t )
114
116
if err != nil {
115
117
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 )
117
119
return
118
120
}
119
121
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 )
121
123
}
122
124
}
123
125
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 )
125
127
}
126
128
// Report an error, if err for json.Marshal is not checked and safe types are omitted
127
129
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 )
129
131
}
130
132
}
131
133
0 commit comments