Skip to content

Commit 2ad105d

Browse files
authored
fix: do not panic on nil object (#21)
1 parent 27a84af commit 2ad105d

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

musttag.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func run(pass *analysis.Pass, funcs map[string]Func) (any, error) {
160160
initialPos := token.NoPos
161161
switch arg := arg.(type) {
162162
case *ast.Ident: // e.g. json.Marshal(foo)
163+
if arg.Obj == nil {
164+
return // e.g. json.Marshal(nil)
165+
}
163166
initialPos = arg.Obj.Pos()
164167
case *ast.CompositeLit: // e.g. json.Marshal(struct{}{})
165168
initialPos = arg.Pos()

testdata/src/tests/tests.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,8 @@ func nonStructArgument() {
468468
custom.Marshal(0)
469469
custom.Unmarshal(nil, &[]int{})
470470
}
471+
472+
// test for panic with nil object issue: https://github.com/junk1tm/musttag/issues/20
473+
func nilObject() {
474+
json.Marshal(nil)
475+
}

0 commit comments

Comments
 (0)