1
0
mirror of https://github.com/mgechev/revive.git synced 2024-12-12 10:44:59 +02:00
revive/rule/indent_error_flow.go

49 lines
1.3 KiB
Go
Raw Permalink Normal View History

2018-01-22 04:04:41 +02:00
package rule
2017-08-28 01:57:16 +02:00
import (
"github.com/mgechev/revive/internal/ifelse"
2018-01-25 01:44:03 +02:00
"github.com/mgechev/revive/lint"
2017-08-28 01:57:16 +02:00
)
2024-12-01 17:44:41 +02:00
// IndentErrorFlowRule prevents redundant else statements.
2018-05-27 01:14:36 +02:00
type IndentErrorFlowRule struct{}
2017-08-28 01:57:16 +02:00
// Apply applies the rule to given file.
func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args)
2017-08-28 01:57:16 +02:00
}
2017-11-20 03:18:16 +02:00
// Name returns the rule name.
2022-04-10 11:55:13 +02:00
func (*IndentErrorFlowRule) Name() string {
2018-05-27 01:14:36 +02:00
return "indent-error-flow"
}
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 "", false
2017-11-20 05:38:28 +02:00
}
if chain.HasPriorNonDeviating {
// if we de-indent the "else" block then a previous branch
// might flow into it, affecting program behaviour
return "", false
2017-11-20 05:38:28 +02:00
}
if !chain.If.Returns() {
// avoid overlapping with superfluous-else
return "", false
2017-08-28 01:57:16 +02:00
}
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls()) {
// avoid increasing variable scope
return "", false
}
return "if block ends with a return statement, so drop this else and outdent its block", true
2017-08-28 01:57:16 +02:00
}