1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

feat: optional extension to early-return rule (#1133) (#1138)

This commit is contained in:
Miles Delahunty
2024-11-28 18:51:33 +11:00
committed by GitHub
parent 777abc9c35
commit 7e1d35d8d2
14 changed files with 311 additions and 107 deletions

View File

@@ -10,7 +10,7 @@ type IndentErrorFlowRule struct{}
// Apply applies the rule to given file.
func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
return ifelse.Apply(e, file.AST, ifelse.TargetElse, args)
return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args)
}
// Name returns the rule name.
@@ -18,28 +18,31 @@ func (*IndentErrorFlowRule) Name() string {
return "indent-error-flow"
}
// CheckIfElse evaluates the rule against an ifelse.Chain and returns a failure message if applicable.
func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) string {
func (*IndentErrorFlowRule) checkIfElse(chain ifelse.Chain, args ifelse.Args) (string, bool) {
if !chain.HasElse {
return "", false
}
if !chain.If.Deviates() {
// this rule only applies if the if-block deviates control flow
return ""
return "", false
}
if chain.HasPriorNonDeviating {
// if we de-indent the "else" block then a previous branch
// might flow into it, affecting program behaviour
return ""
return "", false
}
if !chain.If.Returns() {
// avoid overlapping with superfluous-else
return ""
return "", false
}
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) {
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls()) {
// avoid increasing variable scope
return ""
return "", false
}
return "if block ends with a return statement, so drop this else and outdent its block"
return "if block ends with a return statement, so drop this else and outdent its block", true
}