forked from rsc/grind
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunusedlabel.go
32 lines (28 loc) · 882 Bytes
/
unusedlabel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/ast"
"github.com/jackspirou/grind/block"
"github.com/jackspirou/grind/grinder"
)
func DeleteUnusedLabels(ctxt *grinder.Context, pkg *grinder.Package) {
grinder.GrindFuncDecls(ctxt, pkg, func(ctxt *grinder.Context, pkg *grinder.Package, edit *grinder.EditBuffer, fn *ast.FuncDecl) {
if fn.Body == nil {
return
}
blocks := block.Build(pkg.FileSet, fn.Body)
ast.Inspect(fn.Body, func(x ast.Node) bool {
switch x := x.(type) {
case *ast.LabeledStmt:
if len(blocks.Goto[x.Label.Name])+len(blocks.Break[x.Label.Name])+len(blocks.Continue[x.Label.Name]) == 0 {
edit.DeleteLine(x.Pos(), x.Colon+1)
}
case ast.Expr:
return false
}
return true
})
})
}