Example #1
0
func (v *ShortError) tempVar(stem string, scope *ast.Scope) string {
	for ; v.tmpvar < 10*1000; v.tmpvar++ {
		name := fmt.Sprint(stem, v.tmpvar)
		if scopes.Lookup(scope, name) == nil {
			v.tmpvar++
			return name
		}
	}
	panic(">100,000 temporary variables used. Either the code is crazy, or I am.")
}
Example #2
0
func (w WarnShadow) VisitStmt(scope *ast.Scope, stmt ast.Stmt) scopes.Visitor {
	if stmt, ok := stmt.(*ast.DeclStmt); ok {
		if decl, ok := stmt.Decl.(*ast.GenDecl); ok {
			for _, spec := range decl.Specs {
				if spec, ok := spec.(*ast.ValueSpec); ok {
					for _, name := range spec.Names {
						if scopes.Lookup(scope.Outer, name.Name) != nil {
							fmt.Print(w.Position(name.Pos()).Line, ": Warning, shadowed ", name, "\n")
						}
					}
				}
			}
		}
	}
	return w
}
Example #3
0
func (v *Unused) VisitExpr(scope *ast.Scope, expr ast.Expr) scopes.Visitor {
	switch expr := expr.(type) {
	case *ast.Ident:
		if v.Irrelevant[expr] {
			return v
		}
		if def := scopes.Lookup(scope, expr.Name); def != nil {
			v.Used[def] = true
		} else {
			v.UsedImports[expr.Name] = true
		}
	case *ast.SelectorExpr:
		v.Irrelevant[expr.Sel] = true
	case *ast.KeyValueExpr:
		// if we get a := struct {Count int} {Count: 1}, disregard Count
		if id, ok := expr.Key.(*ast.Ident); ok {
			v.Irrelevant[id] = true
		}
	}
	return v
}
Example #4
0
func (v *AutoImporter) VisitExpr(scope *ast.Scope, expr ast.Expr) scopes.Visitor {
	switch expr := expr.(type) {
	case *ast.Ident:
		if v.Irrelevant[expr] {
			return v
		}
		if importname, ok := imports.RevStdlib[expr.Name]; ok && len(importname) == 1 &&
			!v.m[expr.Name] && scopes.Lookup(scope, expr.Name) == nil {
			v.m[expr.Name] = true // don't add it again
			v.Patches = append(v.Patches, patch.Insert(v.pkg, "; import "+importname[0]))
		}
	case *ast.SelectorExpr:
		v.Irrelevant[expr.Sel] = true
	case *ast.KeyValueExpr:
		// if we get a := struct {Count int} {Count: 1}, disregard Count
		if id, ok := expr.Key.(*ast.Ident); ok {
			v.Irrelevant[id] = true
		}
	}
	return v
}