Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/cmd/deadcode: Report on unused types and constants #66042

Closed
thesilentg opened this issue Feb 29, 2024 · 2 comments
Closed

x/tools/cmd/deadcode: Report on unused types and constants #66042

thesilentg opened this issue Feb 29, 2024 · 2 comments
Labels
Proposal Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@thesilentg
Copy link

Proposal Details

The cmd/deadcode tool currently reports on unreachable functions / methods, but doesn't report on unused types / constants. For example:

main.go

package main

import "fmt"

const (
	unusedConst      = "Unused"
	usedButDeadConst = "usedButDead"
)

type unused struct{}
type usedButDead struct{}

func unusedFunc2() {

}

func unusedFunc() {
	fmt.Println(usedButDead{})
	fmt.Println(usedButDeadConst)
	unusedFunc2()
}

func main() {

}

The output of deadcode . in this package is:

main.go:XX:X: unreachable func: unusedFunc
main.go:XX:X: unreachable func: unusedFunc2

where as the ideal output after this proposal is implemented would be something like:

main.go:XX:X: unreachable func: unusedFunc
main.go:XX:X: unreachable func: unusedFunc2
main.go:XX:X: unreachable type: unused
main.go:XX:X: unreachable type: usedButDead
main.go:XX:X: unreachable constant: unusedConst
main.go:XX:X: unreachable constant: usedButDeadConst

One major benefit of having deadcode report on this is that it has the ability to determine dead functions through its reachability analysis. This ensures that usages of types / constants from dead functions do not result in a type / constant being marked as live, similar to how unusedFunc2 is correctly marked as unreachable by deadcode today despite it being called from unusedFunc.

In contrast, IDEs or other static analysis tools will many times correctly determine unusedConst or unused are dead code, but are not able to determine that usedButDeadConst or usedButDead are also.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 29, 2024
@gopherbot gopherbot added this to the Unreleased milestone Feb 29, 2024
@ianlancetaylor
Copy link
Contributor

CC @golang/tools-team

@adonovan
Copy link
Member

adonovan commented Mar 1, 2024

I agree this would be a nice feature for deadcode. This is essentially the same idea as #64945, but including constants too, so let's merge the issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants