Support procedural (nil return) functions

This commit is contained in:
indigo 2022-07-30 03:07:25 +00:00
parent 6298880b06
commit a065a68449
2 changed files with 13 additions and 9 deletions

View File

@ -29,21 +29,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
var funcBody *ast.BlockStmt
switch n := node.(type) {
switch function := node.(type) {
case *ast.FuncLit:
funcBody = n.Body
funcBody = function.Body
for _, statement := range funcBody.List {
s, ok := statement.(*ast.ReturnStmt)
if ok && s.Results == nil {
pass.Reportf(s.Pos(), "implicit return found for anonymous function")
returnStmt, ok := statement.(*ast.ReturnStmt)
if ok && returnStmt.Results == nil && function.Type.Results != nil {
pass.Reportf(returnStmt.Pos(), "implicit return found for anonymous function")
}
}
case *ast.FuncDecl:
funcBody = n.Body
funcBody = function.Body
for _, statement := range funcBody.List {
s, ok := statement.(*ast.ReturnStmt)
if ok && s.Results == nil {
pass.Reportf(s.Return, "implicit return found in function %s", n.Name)
returnStmt, ok := statement.(*ast.ReturnStmt)
if ok && returnStmt.Results == nil && function.Type.Results != nil {
pass.Reportf(returnStmt.Return, "implicit return found in function %s", function.Name)
}
}
default:

View File

@ -4,6 +4,10 @@ func DoConfusingStuff() (x int, y string) {
return
}
func DoProceduralStuff() {
return
}
func DoSensibleStuff() (x int, y string) {
return x, y
}