SA9010: Conversion of uncomparable type to error (proposal)#1696
Open
klauspost wants to merge 1 commit intodominikh:masterfrom
Open
SA9010: Conversion of uncomparable type to error (proposal)#1696klauspost wants to merge 1 commit intodominikh:masterfrom
klauspost wants to merge 1 commit intodominikh:masterfrom
Conversation
Converting a value of an uncomparable type to the error interface
can lead to runtime panics when error values are compared. Types containing
slices, maps, or function fields are not comparable.
For example:
type MyError struct { Details []string }
func (e MyError) Error() string { return "" }
a, b := error(MyError{}), error(MyError{})
fmt.Println(a == b) // panic: comparing uncomparable type
To avoid this, either use a pointer receiver or wrap the error.
Disclaimer: AI Assisted. Human Reviewed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Converting a value of an uncomparable type to the error interface can lead to runtime panics when error values are compared. Types containing slices, maps, or function fields are not comparable.
For example:
To avoid this, either use a pointer receiver or wrap the error.
This seemed to belong in the "Dubious code constructs that have a high probability of being wrong" section.
It is a foot-gun waiting to blow up. You can argue that you shouldn't be comparing these in the first place, but you can end up with these from different sources, and you may have people trying to
a == error(c)wherec := MyError{}to work around the compiler error.Disclaimer: AI Assisted. Human Reviewed.