2020-05-08 20:14:21 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
2022-11-27 23:23:51 +11:00
|
|
|
"fmt"
|
2020-05-08 20:14:21 +02:00
|
|
|
|
2023-05-17 21:51:35 +10:00
|
|
|
"github.com/mgechev/revive/internal/ifelse"
|
2020-05-08 20:14:21 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-27 23:23:51 +11:00
|
|
|
// EarlyReturnRule finds opportunities to reduce nesting by inverting
|
|
|
|
|
// the condition of an "if" block.
|
2020-05-08 20:14:21 +02:00
|
|
|
type EarlyReturnRule struct{}
|
|
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2023-05-23 18:10:09 +10:00
|
|
|
func (e *EarlyReturnRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
|
2024-11-28 18:51:33 +11:00
|
|
|
return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetIf, args)
|
2020-05-08 20:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*EarlyReturnRule) Name() string {
|
2020-05-08 20:14:21 +02:00
|
|
|
return "early-return"
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 18:51:33 +11:00
|
|
|
func (*EarlyReturnRule) checkIfElse(chain ifelse.Chain, args ifelse.Args) (string, bool) {
|
|
|
|
|
if chain.HasElse {
|
|
|
|
|
if !chain.Else.BranchKind.Deviates() {
|
|
|
|
|
// this rule only applies if the else-block deviates control flow
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
} else if !args.AllowJump || !chain.AtBlockEnd || !chain.BlockEndKind.Deviates() || chain.If.IsShort() {
|
|
|
|
|
// this kind of refactor requires introducing a new indented "return", "continue" or "break" statement,
|
|
|
|
|
// so ignore unless we are able to outdent multiple statements in exchange.
|
|
|
|
|
return "", false
|
2022-11-27 23:23:51 +11:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 21:51:35 +10:00
|
|
|
if chain.HasPriorNonDeviating && !chain.If.IsEmpty() {
|
|
|
|
|
// if we de-indent this block then a previous branch
|
|
|
|
|
// might flow into it, affecting program behaviour
|
2024-11-28 18:51:33 +11:00
|
|
|
return "", false
|
2022-11-27 23:23:51 +11:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 18:51:33 +11:00
|
|
|
if chain.HasElse && chain.If.Deviates() {
|
2023-05-17 21:51:35 +10:00
|
|
|
// avoid overlapping with superfluous-else
|
2024-11-28 18:51:33 +11:00
|
|
|
return "", false
|
2022-11-27 23:23:51 +11:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 18:51:33 +11:00
|
|
|
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.If.HasDecls()) {
|
2023-05-23 18:10:09 +10:00
|
|
|
// avoid increasing variable scope
|
2024-11-28 18:51:33 +11:00
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !chain.HasElse {
|
|
|
|
|
return fmt.Sprintf("if c { ... } can be rewritten if !c { %v } ... to reduce nesting", chain.BlockEndKind), true
|
2023-05-23 18:10:09 +10:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 21:51:35 +10:00
|
|
|
if chain.If.IsEmpty() {
|
2024-11-28 18:51:33 +11:00
|
|
|
return fmt.Sprintf("if c { } else %[1]v can be simplified to if !c %[1]v", chain.Else), true
|
2022-11-27 23:23:51 +11:00
|
|
|
}
|
2024-11-28 18:51:33 +11:00
|
|
|
return fmt.Sprintf("if c { ... } else %[1]v can be simplified to if !c %[1]v ...", chain.Else), true
|
2020-05-08 20:14:21 +02:00
|
|
|
}
|