Documentation ¶
Overview ¶
Package ifelse provides helpers for analysing the control flow in if-else chains, presently used by the following rules: - early-return - indent-error-flow - superfluous-else
Index ¶
Constants ¶
const PreserveScope = "preserveScope"
PreserveScope is a configuration argument that prevents suggestions that would enlarge variable scope
Variables ¶
var DeviatingFuncs = map[Call]BranchKind{ {"os", "Exit"}: Exit, {"log", "Fatal"}: Exit, {"log", "Fatalf"}: Exit, {"log", "Fatalln"}: Exit, {"", "panic"}: Panic, {"log", "Panic"}: Panic, {"log", "Panicf"}: Panic, {"log", "Panicln"}: Panic, }
DeviatingFuncs lists known control flow deviating function calls.
Functions ¶
func Apply ¶
Apply evaluates the given Rule on if-else chains found within the given AST, and returns the failures.
Note that in if-else chain with multiple "if" blocks, only the *last* one is checked, that is to say, given:
if foo { ... } else if bar { ... } else { ... }
Only the block following "bar" is linted. This is because the rules that use this function do not presently have anything to say about earlier blocks in the chain.
Types ¶
type Args ¶
type Args struct {
PreserveScope bool
}
Args contains arguments common to the early-return, indent-error-flow and superfluous-else rules (currently just preserveScope)
type Branch ¶
type Branch struct { BranchKind Call // The function called at the end for kind Panic or Exit. HasDecls bool // The branch has one or more declarations (at the top level block) }
Branch contains information about a branch within an if-else chain.
func BlockBranch ¶
BlockBranch gets the Branch of an ast.BlockStmt.
func (Branch) LongString ¶
LongString returns a longer form string representation
type BranchKind ¶
type BranchKind int
BranchKind is a classifier for if-else branches. It says whether the branch is empty, and whether the branch ends with a statement that deviates control flow.
const ( // Empty branches do nothing Empty BranchKind = iota // Return branches return from the current function Return // Continue branches continue a surrounding "for" loop Continue // Break branches break a surrounding "for" loop Break // Goto branches conclude with a "goto" statement Goto // Panic branches panic the current function Panic // Exit branches end the program Exit // Regular branches do not fit any category above Regular )
func (BranchKind) Branch ¶
func (k BranchKind) Branch() Branch
Branch returns a Branch with the given kind
func (BranchKind) Deviates ¶
func (k BranchKind) Deviates() bool
Deviates tests if the control does not flow to the first statement following the if-else chain.
func (BranchKind) LongString ¶
func (k BranchKind) LongString() string
LongString returns a longer form string representation
func (BranchKind) Returns ¶
func (k BranchKind) Returns() bool
Returns tests if the branch returns from the current function
func (BranchKind) String ¶
func (k BranchKind) String() string
String returns a brief string representation
type Call ¶
type Call struct { Pkg string // The package qualifier of the function, if not built-in. Name string // The function name. }
Call contains the name of a function that deviates control flow.
type Chain ¶
type Chain struct { If Branch // what happens at the end of the "if" block Else Branch // what happens at the end of the "else" block HasInitializer bool // is there an "if"-initializer somewhere in the chain? HasPriorNonDeviating bool // is there a prior "if" block that does NOT deviate control flow? AtBlockEnd bool // whether the chain is placed at the end of the surrounding block }
Chain contains information about an if-else chain.