mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
fix(1323): false positive in get-return rule for HTTP Get handlers (#1325)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"go/ast"
|
||||
"strings"
|
||||
|
||||
"github.com/mgechev/revive/internal/astutils"
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
@@ -29,6 +30,10 @@ func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||
continue
|
||||
}
|
||||
|
||||
if isHTTPHandler(fd.Type.Params) {
|
||||
continue // the Get prefix in the function name refers to HTTP GET
|
||||
}
|
||||
|
||||
failures = append(failures, lint.Failure{
|
||||
Confidence: 0.8,
|
||||
Node: fd,
|
||||
@@ -69,3 +74,15 @@ func isGetter(name string) bool {
|
||||
func hasResults(rs *ast.FieldList) bool {
|
||||
return rs != nil && len(rs.List) > 0
|
||||
}
|
||||
|
||||
// isHTTPHandler returns true if the given params match with the signature of an HTTP handler, false otherwise
|
||||
// A params list is considered to be an HTTP handler if the first two parameters are
|
||||
// http.ResponseWriter, *http.Request in that order.
|
||||
func isHTTPHandler(params *ast.FieldList) bool {
|
||||
typeNames := astutils.GetTypeNames(params)
|
||||
if len(typeNames) < 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
return typeNames[0] == "http.ResponseWriter" && typeNames[1] == "*http.Request"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user