Example #1
0
func Example() {
	file, fset := parse(`package main
		func init() {
			i := 1
			if true {
				var i = 2
			}
		}`)
	scopes.WalkFile(WarnShadow{fset}, file)
	// Output: 5: Warning, shadowed i
}
Example #2
0
// TODO(elazar): more complex tests:
//   1. What should happen when I `import . "foo"`, and use `var foo` from other package?
func TestSimpleUnused(t *testing.T) {
	for i, c := range UnusedSimple {
		if *ncase != i && *ncase > 0 {
			continue
		}
		file, _ := parse(c.body, t)
		unused := []string{}
		scopes.WalkFile(NewUnused(unusedNames(func(name string) {
			unused = append(unused, name)
		})), file)
		if fmt.Sprint(unused) != fmt.Sprint(c.expUnused) {
			t.Errorf("Case #%d:\n%s\n Expected unused %v got %v", i, c.body, c.expUnused, unused)
		}
	}
}
Example #3
0
func main() {
	if len(os.Args) == 1 {
		usage()
		return
	}
	f := func(p *patch.PatchableFile) patch.Patches {
		patches := &visitors.PatchUnused{patch.Patches{}}
		autoimport := visitors.NewAutoImporter(p.File)
		scopes.WalkFile(visitors.NewMultiVisitor(visitors.NewUnused(patches), autoimport), p.File)
		return append(patches.Patches, autoimport.Patches...)
	}
	if err := instrument.InstrumentCmd(f, os.Args...); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
}