2019-04-17 22:55:52 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BareReturnRule lints given else constructs.
|
|
|
|
type BareReturnRule struct{}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2019-04-17 22:55:52 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
onFailure := func(failure lint.Failure) {
|
|
|
|
failures = append(failures, failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := lintBareReturnRule{onFailure: onFailure}
|
|
|
|
ast.Walk(w, file.AST)
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BareReturnRule) Name() string {
|
2019-04-17 22:55:52 +02:00
|
|
|
return "bare-return"
|
|
|
|
}
|
|
|
|
|
|
|
|
type lintBareReturnRule struct {
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w lintBareReturnRule) Visit(node ast.Node) ast.Visitor {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
w.checkFunc(n.Type.Results, n.Body)
|
|
|
|
case *ast.FuncLit: // to cope with deferred functions and go-routines
|
|
|
|
w.checkFunc(n.Type.Results, n.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkFunc will verify if the given function has named result and bare returns
|
|
|
|
func (w lintBareReturnRule) checkFunc(results *ast.FieldList, body *ast.BlockStmt) {
|
|
|
|
hasNamedResults := results != nil && len(results.List) > 0 && results.List[0].Names != nil
|
|
|
|
if !hasNamedResults || body == nil {
|
|
|
|
return // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
brf := bareReturnFinder{w.onFailure}
|
|
|
|
ast.Walk(brf, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
type bareReturnFinder struct {
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w bareReturnFinder) Visit(node ast.Node) ast.Visitor {
|
2019-12-21 22:17:51 +02:00
|
|
|
_, ok := node.(*ast.FuncLit)
|
|
|
|
if ok {
|
|
|
|
// skip analysing function literals
|
2021-10-14 20:56:29 +02:00
|
|
|
// they will be analysed by the lintBareReturnRule.Visit method
|
2019-12-21 22:17:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 22:55:52 +02:00
|
|
|
rs, ok := node.(*ast.ReturnStmt)
|
|
|
|
if !ok {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rs.Results) > 0 {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Confidence: 1,
|
|
|
|
Node: rs,
|
|
|
|
Failure: "avoid using bare returns, please add return expressions",
|
|
|
|
})
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|