Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var NoFallthroughRule = rule.Rule{ Name: "no-fallthrough", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { opts := parseOptions(options) return rule.RuleListeners{ ast.KindSwitchStatement: func(node *ast.Node) { switchStmt := node.AsSwitchStatement() if switchStmt == nil || switchStmt.CaseBlock == nil { return } caseBlock := switchStmt.CaseBlock.AsCaseBlock() if caseBlock == nil || caseBlock.Clauses == nil { return } clauses := caseBlock.Clauses.Nodes sourceText := ctx.SourceFile.Text() for i := range len(clauses) - 1 { currentClause := clauses[i].AsCaseOrDefaultClause() if currentClause == nil { continue } statements := currentClause.Statements if statements == nil || len(statements.Nodes) == 0 { continue } if opts.allowEmptyCase && isCaseBodyEmpty(statements.Nodes) { continue } isFallthrough := currentClause.FallthroughFlowNode != nil if isFallthrough && hasASTTerminal(statements.Nodes) { isFallthrough = false } nextClause := clauses[i+1] lastStmt := statements.Nodes[len(statements.Nodes)-1] commentStart := lastStmt.End() nextKeywordPos := scanner.GetRangeOfTokenAtPosition(ctx.SourceFile, nextClause.Pos()).Pos() hasComment := hasFallthroughComment(sourceText, commentStart, nextKeywordPos, opts.commentPattern) if !isFallthrough { if opts.reportUnusedFallthroughComment && hasComment { ctx.ReportNode(nextClause, rule.RuleMessage{ Id: "unusedFallthroughComment", Description: "Found a comment that would permit fallthrough, but case cannot fall through.", }) } continue } if hasComment { continue } var msgID string var description string if nextClause.Kind == ast.KindDefaultClause { msgID = "default" description = "Expected a 'break' statement before 'default'." } else { msgID = "case" description = "Expected a 'break' statement before 'case'." } ctx.ReportNode(nextClause, rule.RuleMessage{ Id: msgID, Description: description, }) } }, } }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.