Skip to content

Commit

Permalink
fix: do not panic on nil object (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm authored Feb 11, 2023
1 parent 27a84af commit 2ad105d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func run(pass *analysis.Pass, funcs map[string]Func) (any, error) {
initialPos := token.NoPos
switch arg := arg.(type) {
case *ast.Ident: // e.g. json.Marshal(foo)
if arg.Obj == nil {
return // e.g. json.Marshal(nil)
}
initialPos = arg.Obj.Pos()
case *ast.CompositeLit: // e.g. json.Marshal(struct{}{})
initialPos = arg.Pos()
Expand Down
5 changes: 5 additions & 0 deletions testdata/src/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,8 @@ func nonStructArgument() {
custom.Marshal(0)
custom.Unmarshal(nil, &[]int{})
}

// test for panic with nil object issue: https://github.com/junk1tm/musttag/issues/20
func nilObject() {
json.Marshal(nil)
}

0 comments on commit 2ad105d

Please sign in to comment.