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

46 lines
1.2 KiB
Go
Raw 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
)
2018-05-27 01:14:36 +02:00
// IndentErrorFlowRule lints given else constructs.
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, 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"
}
// CheckIfElse evaluates the rule against an ifelse.Chain.
func (*IndentErrorFlowRule) CheckIfElse(chain ifelse.Chain, args ifelse.Args) (failMsg string) {
if !chain.If.Deviates() {
// this rule only applies if the if-block deviates control flow
return
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
2017-11-20 05:38:28 +02:00
}
if !chain.If.Returns() {
// avoid overlapping with superfluous-else
return
2017-08-28 01:57:16 +02:00
}
if args.PreserveScope && !chain.AtBlockEnd && (chain.HasInitializer || chain.Else.HasDecls) {
// avoid increasing variable scope
return
}
return "if block ends with a return statement, so drop this else and outdent its block"
2017-08-28 01:57:16 +02:00
}