ifelse

package
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ifelse provides helpers for analyzing the control flow in if-else chains, presently used by the following rules: - early-return - indent-error-flow - superfluous-else

Index

Constants

This section is empty.

Variables

View Source
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

func Apply(check CheckFunc, node ast.Node, target Target, args Args) []lint.Failure

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
	AllowJump     bool
}

Args contains arguments common to the early-return, indent-error-flow, and superfluous-else rules.

type Branch

type Branch struct {
	BranchKind
	Call // The function called at the end for kind Panic or Exit.
	// contains filtered or unexported fields
}

Branch contains information about a branch within an if-else chain.

func BlockBranch

func BlockBranch(block *ast.BlockStmt) Branch

BlockBranch gets the Branch of an ast.BlockStmt.

func StmtBranch

func StmtBranch(stmt ast.Stmt) Branch

StmtBranch gets the Branch of an ast.Stmt.

func (Branch) HasDecls

func (b Branch) HasDecls() bool

HasDecls returns whether the branch has any top-level declarations.

func (Branch) IsShort added in v1.6.0

func (b Branch) IsShort() bool

IsShort returns whether the branch is empty or consists of a single statement.

func (Branch) LongString

func (b Branch) LongString() string

LongString returns a longer form string representation.

func (Branch) String

func (b Branch) String() string

String returns a brief 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) IsEmpty

func (k BranchKind) IsEmpty() bool

IsEmpty tests if the branch is empty.

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.

func ExprCall

func ExprCall(expr *ast.ExprStmt) (Call, bool)

ExprCall gets the Call of an ExprStmt, if any.

func (Call) String

func (f Call) String() string

String returns the function name with package qualifier (if any).

type Chain

type Chain struct {
	If                   Branch     // what happens at the end of the "if" block
	HasElse              bool       // is there an "else" 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
	BlockEndKind         BranchKind // control flow at end of surrounding block (e.g. "return" for function body)
}

Chain contains information about an if-else chain.

type CheckFunc added in v1.6.0

type CheckFunc func(Chain) (string, bool)

CheckFunc evaluates a rule against the given if-else chain and returns a message describing the proposed refactor, along with a indicator of whether such a refactor could be found.

type Target

type Target int

Target decides what line/column should be indicated by the rule in question.

const (
	// TargetIf means the text refers to the "if".
	TargetIf Target = iota

	// TargetElse means the text refers to the "else".
	TargetElse
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL