Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
cmd/nargs/nargs
/nargs
nargs.exe
34 changes: 22 additions & 12 deletions nargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Flags struct {

type unusedVisitor struct {
fileSet *token.FileSet
currentFile *token.File
resultsSet map[string]struct{}
includeNamedReturns bool
includeReceivers bool
Expand Down Expand Up @@ -106,9 +107,11 @@ func (v *unusedVisitor) Visit(node ast.Node) ast.Visitor {
}
stmtList = v.handleFuncDecl(paramMap, funcDecl, stmtList)
file = v.fileSet.File(funcDecl.Pos())
v.currentFile = file

case *ast.File:
file = v.fileSet.File(topLevelType.Pos())
v.currentFile = file
if topLevelType.Decls != nil {
stmtList = v.handleDecls(paramMap, topLevelType.Decls, stmtList)
}
Expand All @@ -125,17 +128,24 @@ func (v *unusedVisitor) Visit(node ast.Node) ast.Visitor {
v.handleStmts(paramMap, stmtList)

for paramName, used := range paramMap {
if !used {
if file != nil {
if funcDecl != nil && funcDecl.Name != nil {
// TODO print parameter vs parameter(s)?
// TODO differentiation of used parameter vs. receiver?
resStr := fmt.Sprintf("%v:%v %v contains unused parameter %v\n", file.Name(), file.Position(funcDecl.Pos()).Line, funcDecl.Name.Name, paramName)
v.resultsSet[resStr] = struct{}{}
v.errsFound = true
}
}
if used {
continue
}
if file == nil {
continue
}
if funcDecl == nil {
continue
}
if funcDecl.Name == nil {
continue
}

// TODO print parameter vs parameter(s)?
// TODO differentiation of used parameter vs. receiver?
resStr := fmt.Sprintf("%v:%v %v contains unused parameter %v\n", file.Name(), file.Position(funcDecl.Pos()).Line, funcDecl.Name.Name, paramName)
v.resultsSet[resStr] = struct{}{}
v.errsFound = true
}

return v
Expand Down Expand Up @@ -237,7 +247,7 @@ func (v *unusedVisitor) handleStmts(paramMap map[string]bool, stmtList []ast.Stm
// no-op

default:
log.Printf("ERROR: unknown stmt type %T\n", s)
log.Printf("ERROR: unknown stmt type %T in file %v\n", s, v.currentFile.Name())
}

stmtList = stmtList[1:]
Expand Down Expand Up @@ -356,7 +366,7 @@ func (v *unusedVisitor) handleExprs(paramMap map[string]bool, exprList []ast.Exp
// no op

default:
log.Printf("ERROR: unknown expr type %T\n", e)
log.Printf("ERROR: unknown expr type %T in file %v\n", e, v.currentFile.Name())
}
exprList = exprList[1:]
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func variableCapturedByClosure(r int) {
feedTokens(5)
}

//TODO - functions as keys
// TODO - functions as keys
// var usedAsGlobalInterfaceMapValue = map[string]interface{}{
// "someFunc": func(i int, s string) {
// if i == 0 {
Expand Down
Loading