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

map[string]struct{} unused variable #298

Closed
jhwz opened this issue Sep 23, 2021 · 0 comments · Fixed by #339
Closed

map[string]struct{} unused variable #298

jhwz opened this issue Sep 23, 2021 · 0 comments · Fixed by #339

Comments

@jhwz
Copy link

jhwz commented Sep 23, 2021

If there's a map[string]struct{} field in a struct, the generated code will not compile

Eg:

type Example struct {
	M map[string]struct{}
}

produces

// EncodeMsg implements msgp.Encodable
func (z *Example) EncodeMsg(en *msgp.Writer) (err error) {
	// map header, size 1
	// write "M"
	err = en.Append(0x81, 0xa1, 0x4d)
	if err != nil {
		return
	}
	err = en.WriteMapHeader(uint32(len(z.M)))
	if err != nil {
		err = msgp.WrapError(err, "M")
		return
	}
	for za0001, za0002 := range z.M {
		err = en.WriteString(za0001)
		if err != nil {
			err = msgp.WrapError(err, "M")
			return
		}
		// map header, size 0
		err = en.Append(0x80)
		if err != nil {
			return
		}
	}
	return
}

where za0002 is unused and causes the code to not compile

klauspost added a commit to klauspost/msgp that referenced this issue Feb 10, 2024
Empty structs used as map values would generate invalid code.

Fixes tinylib#298
klauspost added a commit that referenced this issue Feb 10, 2024
Empty structs used as map values would generate invalid code.

Fixes #298
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant