Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not panic on nil object #21

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
tmzane marked this conversation as resolved.
Show resolved Hide resolved
json.Marshal(nil)
}