Skip to content

SA9010: Conversion of uncomparable type to error (proposal)#1696

Open
klauspost wants to merge 1 commit intodominikh:masterfrom
klauspost:sa9010-uncomparable-error
Open

SA9010: Conversion of uncomparable type to error (proposal)#1696
klauspost wants to merge 1 commit intodominikh:masterfrom
klauspost:sa9010-uncomparable-error

Conversation

@klauspost
Copy link

@klauspost klauspost commented Jan 29, 2026

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.

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) where c := MyError{} to work around the compiler error.

Disclaimer: AI Assisted. Human Reviewed.

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.
@klauspost klauspost changed the title SA9010: Conversion of uncomparable type to error SA9010: Conversion of uncomparable type to error (proposal) Jan 29, 2026
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 this pull request may close these issues.

1 participant