Skip to content

SA4000: ignore T{} == T{} comparable assertions #1670

@mvdan

Description

@mvdan
env HOME=${WORK}/.home

exec go version
exec staticcheck --version

exec go build f.go
exec staticcheck f.go

-- f.go --
package p

type T struct {
	Foo int
}

// Ensure T is comparable.
var _ = T{} == T{}
> exec go version
[stdout]
go version go1.26-devel_8282b72d62 2025-08-06 14:34:39 -0700 linux/amd64
> exec staticcheck --version
[stdout]
staticcheck (devel, v0.7.0-0.dev.0.20250708155414-ba737e5a0f56)
> exec go build f.go
> exec staticcheck f.go
[stdout]
f.go:8:9: identical expressions on the left and right side of the '==' operator (SA4000)
[exit status 1]

Can we add an exception so that var _ = T{} == T{} does not trigger "identical expressions on the left and right" errors?

There are other ways to assert that a type is comparable at build time, like using a map (var _ map[T]struct{}) or instantiating a generic func with a comparable constraint, but they both seem more convoluted than simply comparing an empty T to itself.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions