golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.3k stars 17.47k forks source link

x/tools/gopls: report unnecessarily exported symbols #67573

Open adonovan opened 3 months ago

adonovan commented 3 months ago

After a code reorganization, many symbols may continue to be exported even though they are no longer referenced from another package. Just as it is helpful to report dead code within an application, it would be useful to report symbols that could safely be unexported.

Gopls has all the information required to do this; we should expose it on request in the UI somehow.

(Or this could be a feature of cmd/deadcode. But cmd/deadcode could be a feature of gopls, too.)

gopherbot commented 3 months ago

Change https://go.dev/cl/586781 mentions this issue: gopls/internal/golang: unexport several functions

adonovan commented 3 months ago

We could easily add this as a code action "Show unnecessarily exported symbols" whose associated command should indicate one of two things (that don't currently exist in LSP, but should):

@findleyr had a neat idea: produce the report as a web page (which we could easily do today), with a list of checkboxes, one per function. The user could select a subset of them then hit an "Apply changes" button, which would cause the server to send one or more ApplyEdit downcalls to do the renamings.

ian-h-chamberlain commented 2 months ago

:+1: to this feature.

As a companion feature, a code action to easily toggle exported-ness of a single symbol (or struct member) would also be helpful! That would make it easier to fix one-off instances as you see them, without needing to explicitly run the analysis for the entire package/workspace. I imagine the necessary pieces of functionality would be very similar to those needed for a report like this.