Skip to content

x/tools/gopls/internal/analysis/unusedfunc: spurious warnings for unused enum constants #76924

@jscissr

Description

@jscissr

gopls version

golang.org/x/tools/gopls v0.21.0
golang.org/x/tools/[email protected] h1:k8RlBm3ES+GVe+fbTSkzwKgarmNwN+6aDalb0T0xfag=
github.com/BurntSushi/[email protected] h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/fatih/[email protected] h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/[email protected] h1:dDSgAjoOMp8da3egfz0t2S+t8RGOpEmEXZubcGuc0Bg=
github.com/fatih/[email protected] h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fsnotify/[email protected] h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/google/[email protected] h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/[email protected] h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q=
github.com/modelcontextprotocol/[email protected] h1:jdsBtGzBLY287WKSIjYovOXAqtJkP+HtFQFKrZd4a6c=
github.com/yosida95/uritemplate/[email protected] h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
golang.org/x/exp/[email protected] h1:HDjDiATsGqvuqvkDvgJjD1IgPrVekcSXVVE21JwvzGE=
golang.org/x/[email protected] h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
golang.org/x/[email protected] h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
golang.org/x/[email protected] h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/[email protected] h1:E2/AqCUMZGgd73TQkxUMcMla25GB9i/5HOdLr+uH7Vo=
golang.org/x/[email protected] h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
golang.org/x/[email protected] h1:NIdx9X+Z8lIV89t3Bs/bb4D/KTtHP4KYdUIFMiGlo6Y=
golang.org/x/[email protected] h1:Ju8QsuyhX3Hk8ma3CesTbO8vfJD9EvUBgHvkxHBzj0I=
honnef.co/go/[email protected] h1:5SXjd4ET5dYijLaf0O3aOenC0Z4ZafIWSpjUzsQaNho=
mvdan.cc/[email protected] h1:nZUCeC2ViFaerTcYKstMmfysj6uhQrA2vJe+2vwGU6k=
mvdan.cc/xurls/[email protected] h1:3NTZpeTxYVWNSokW3MKeyVkz/j7uYXYiMtXRUfmjbgI=
go: go1.25.4

go env

-

What did you do?

When implementing an existing protocol, there are often enums which I implement in Go as a group of constants. The numbers may not be consecutive, so I cannot use iota. I want to add all constants defined by the protocol, even if I don't initially use all of them.

For example:

// Command codes
const (
	cmdGetPDRRepositoryInfo      = 0x50
	cmdGetPDR                    = 0x51
	cmdFindPDR                   = 0x52
	cmdRunInitAgent              = 0x58
	cmdGetPDRRepositorySignature = 0x53
)

What did you see happen?

gopls warns about unused constants in the enum.

What did you expect to see?

According to comments in #50764, every warning by gopls should be actionable. In this case, even though some of the constants are indeed unused, I don't want to remove them because I may need them later, and the enum definition would be incomplete.

My suggestion is to add add a heuristic which detects a group of constants, where each has the same type, as an enum and suppress warnings if some of them are unused.

To reduce false negatives, the analysis could require that at least one of the constants in the group is used, and otherwise warn. The same could be done for iota enums: Currently, it doesn't warn for these even if all constants are unused.

Editor and settings

No response

Logs

No response

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions