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

don't falsely report Test methods in main package #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions deadcode/deadcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"golang.org/x/tools/go/loader"
)
Expand Down Expand Up @@ -59,9 +60,8 @@ func (ctx *Context) Load(args ...string) {
}
}

// error formats the error to standard error, adding program
// identification and a newline
func (ctx *Context) errorf(pos token.Pos, format string, args ...interface{}) {
// pos resolves a compact position encoding into a verbose one
func (ctx *Context) pos(pos token.Pos) token.Position {
if ctx.cwd == "" {
ctx.cwd, _ = os.Getwd()
}
Expand All @@ -70,6 +70,13 @@ func (ctx *Context) errorf(pos token.Pos, format string, args ...interface{}) {
if err == nil {
p.Filename = f
}
return p
}

// error formats the error to standard error, adding program
// identification and a newline
func (ctx *Context) errorf(pos token.Pos, format string, args ...interface{}) {
p := ctx.pos(pos)
fmt.Fprintf(os.Stderr, p.String()+": "+format+"\n", args...)
exitCode = 2
}
Expand All @@ -81,14 +88,14 @@ func (ctx *Context) Process() []types.Object {
}
var allUnused []types.Object
for _, pkg := range prog.Imported {
unused := doPackage(prog, pkg)
unused := ctx.doPackage(prog, pkg)
allUnused = append(allUnused, unused...)
}
sort.Sort(objects(allUnused))
return allUnused
}

func doPackage(prog *loader.Program, pkg *loader.PackageInfo) []types.Object {
func (ctx *Context) doPackage(prog *loader.Program, pkg *loader.PackageInfo) []types.Object {
used := make(map[types.Object]bool)
for _, file := range pkg.Files {
ast.Inspect(file, func(n ast.Node) bool {
Expand All @@ -111,7 +118,10 @@ func doPackage(prog *loader.Program, pkg *loader.PackageInfo) []types.Object {
continue
}
obj := global.Lookup(name)
if !used[obj] && (pkg.Pkg.Name() == "main" || !ast.IsExported(name)) {
_, isSig := obj.Type().(*types.Signature)
pos := ctx.pos(obj.Pos())
isTestMethod := isSig && strings.HasPrefix(obj.Name(), "Test") && strings.HasSuffix(pos.Filename, "_test.go")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, need to check Example and Benchmark prefixes

if !used[obj] && ((pkg.Pkg.Name() == "main" && !isTestMethod) || !ast.IsExported(name)) {
unused = append(unused, obj)
}
}
Expand Down