ifelse

package
v1.3.2-comdiv-1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 4 Imported by: 0

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

View Source
const PreserveScope = "preserveScope"

PreserveScope is a configuration argument that prevents suggestions that would enlarge variable scope

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(rule Rule, node ast.Node, target Target, args lint.Arguments) []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
}

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

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) 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
	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.

type Rule

type Rule interface {
	CheckIfElse(chain Chain, args Args) (failMsg string)
}

Rule is an interface for linters operating on if-else chains

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