mirror of
https://github.com/mgechev/revive.git
synced 2025-03-03 14:52:54 +02:00
* refactor: extract shared code for linting if-else chains The rules "early-return", "indent-error-flow" and "superfluous-else" have a similar structure. This moves the common logic for classifying if-else chains to a common package. A few side benefits: - "early-return" now handles os.Exit/log.Panicf/etc - "superfluous-else" now handles (builtin) panic - "superfluous-else" and "indent-error-flow" now handle if/else chains with 2+ "if" branches * internal/ifelse: style fixes, renames, spelling
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package ifelse
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/token"
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
)
|
|
|
|
// Rule is an interface for linters operating on if-else chains
|
|
type Rule interface {
|
|
CheckIfElse(chain Chain) (failMsg string)
|
|
}
|
|
|
|
// 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.
|
|
func Apply(rule Rule, node ast.Node, target Target) []lint.Failure {
|
|
v := &visitor{rule: rule, target: target}
|
|
ast.Walk(v, node)
|
|
return v.failures
|
|
}
|
|
|
|
type visitor struct {
|
|
failures []lint.Failure
|
|
target Target
|
|
rule Rule
|
|
}
|
|
|
|
func (v *visitor) Visit(node ast.Node) ast.Visitor {
|
|
ifStmt, ok := node.(*ast.IfStmt)
|
|
if !ok {
|
|
return v
|
|
}
|
|
|
|
v.visitChain(ifStmt, Chain{})
|
|
return nil
|
|
}
|
|
|
|
func (v *visitor) visitChain(ifStmt *ast.IfStmt, chain Chain) {
|
|
// look for other if-else chains nested inside this if { } block
|
|
ast.Walk(v, ifStmt.Body)
|
|
|
|
if ifStmt.Else == nil {
|
|
// no else branch
|
|
return
|
|
}
|
|
|
|
if as, ok := ifStmt.Init.(*ast.AssignStmt); ok && as.Tok == token.DEFINE {
|
|
chain.HasInitializer = true
|
|
}
|
|
chain.If = BlockBranch(ifStmt.Body)
|
|
|
|
switch elseBlock := ifStmt.Else.(type) {
|
|
case *ast.IfStmt:
|
|
if !chain.If.Deviates() {
|
|
chain.HasPriorNonDeviating = true
|
|
}
|
|
v.visitChain(elseBlock, chain)
|
|
case *ast.BlockStmt:
|
|
// look for other if-else chains nested inside this else { } block
|
|
ast.Walk(v, elseBlock)
|
|
chain.Else = BlockBranch(elseBlock)
|
|
if failMsg := v.rule.CheckIfElse(chain); failMsg != "" {
|
|
if chain.HasInitializer {
|
|
// if statement has a := initializer, so we might need to move the assignment
|
|
// onto its own line in case the body references it
|
|
failMsg += " (move short variable declaration to its own line if necessary)"
|
|
}
|
|
v.failures = append(v.failures, lint.Failure{
|
|
Confidence: 1,
|
|
Node: v.target.node(ifStmt),
|
|
Failure: failMsg,
|
|
})
|
|
}
|
|
default:
|
|
panic("invalid node type for else")
|
|
}
|
|
}
|