2018-06-22 13:21:09 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetReturnRule lints given else constructs.
|
|
|
|
type GetReturnRule struct{}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2018-06-22 13:21:09 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
onFailure := func(failure lint.Failure) {
|
|
|
|
failures = append(failures, failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := lintReturnRule{onFailure}
|
|
|
|
ast.Walk(w, file.AST)
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*GetReturnRule) Name() string {
|
2018-06-22 13:21:09 +02:00
|
|
|
return "get-return"
|
|
|
|
}
|
|
|
|
|
|
|
|
type lintReturnRule struct {
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGetter(name string) bool {
|
|
|
|
if strings.HasPrefix(strings.ToUpper(name), "GET") {
|
|
|
|
if len(name) > 3 {
|
|
|
|
c := name[3]
|
|
|
|
return !(c >= 'a' && c <= 'z')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasResults(rs *ast.FieldList) bool {
|
|
|
|
return rs != nil && len(rs.List) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w lintReturnRule) Visit(node ast.Node) ast.Visitor {
|
|
|
|
fd, ok := node.(*ast.FuncDecl)
|
|
|
|
if !ok {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isGetter(fd.Name.Name) {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
if !hasResults(fd.Type.Results) {
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Confidence: 0.8,
|
|
|
|
Node: fd,
|
|
|
|
Category: "logic",
|
|
|
|
Failure: fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|