func (v *BreakAndNextCheck) Visit(s *SemanticAnalyzer, n ast.Node) { switch n := n.(type) { case *ast.NextStat, *ast.BreakStat: if v.nestedLoopCount[v.functions[len(v.functions)-1]] == 0 { s.Err(n, "%s must be in a loop", util.CapitalizeFirst(n.NodeName())) } case *ast.LoopStat: v.nestedLoopCount[v.functions[len(v.functions)-1]]++ case *ast.FunctionDecl: v.functions = append(v.functions, n.Function) case *ast.LambdaExpr: v.functions = append(v.functions, n.Function) } }
func (v *UnusedCheck) Visit(s *SemanticAnalyzer, n ast.Node) { switch n := n.(type) { case *ast.VariableDecl: if !n.IsPublic() { v.encountered = append(v.encountered, n.Variable) v.encounteredDecl = append(v.encounteredDecl, n) } case *ast.DestructVarDecl: if !n.IsPublic() { for idx, vari := range n.Variables { if !n.ShouldDiscard[idx] { v.encountered = append(v.encountered, vari) v.encounteredDecl = append(v.encounteredDecl, n) } } } case *ast.FunctionDecl: if !n.IsPublic() { v.encountered = append(v.encountered, n.Function) v.encounteredDecl = append(v.encounteredDecl, n) } } switch n := n.(type) { case *ast.FunctionAccessExpr: v.uses[n.Function]++ case *ast.VariableAccessExpr: v.uses[n.Variable]++ } }
func (v *UnreachableCheck) PostVisit(s *SemanticAnalyzer, n ast.Node) { switch n := n.(type) { case *ast.Block: for i, c := range n.Nodes { if i < len(n.Nodes)-1 && IsNodeTerminating(c) { s.Err(n.Nodes[i+1], "Unreachable code") } } if len(n.Nodes) > 0 { n.IsTerminating = IsNodeTerminating(n.Nodes[len(n.Nodes)-1]) } case *ast.FunctionDecl: v.visitFunction(s, n, n.Function) case *ast.LambdaExpr: v.visitFunction(s, n, n.Function) } }
func (v *MiscCheck) Visit(s *SemanticAnalyzer, n ast.Node) { switch n.(type) { case *ast.FunctionDecl, *ast.LambdaExpr: v.InFunction++ } if v.InFunction <= 0 { switch n.(type) { case *ast.ReturnStat: s.Err(n, "%s must be in function", util.CapitalizeFirst(n.NodeName())) } } else { switch n.(type) { case *ast.TypeDecl: s.Err(n, "%s must not be in function", util.CapitalizeFirst(n.NodeName())) case *ast.FunctionDecl: if v.InFunction > 1 { s.Err(n, "%s must not be in function", util.CapitalizeFirst(n.NodeName())) } } } }