Reorganize and get tests working

This commit is contained in:
indigo 2022-08-02 22:09:15 +00:00
parent a065a68449
commit 02f24a7ea1
6 changed files with 41 additions and 25 deletions

4
.golangci.yml Normal file
View File

@ -0,0 +1,4 @@
linters:
disable-all: true
enable:
- explicitreturn

View File

@ -2,7 +2,7 @@ package main
import (
"golang.org/x/tools/go/analysis/singlechecker"
"tildegit.org/indigo/explicitreturn/analyzer"
"tildegit.org/indigo/explicitreturn/pkg/analyzer"
)
func main() {

View File

@ -1,22 +0,0 @@
package main
func DoConfusingStuff() (x int, y string) {
return
}
func DoProceduralStuff() {
return
}
func DoSensibleStuff() (x int, y string) {
return x, y
}
func DoSillyStuff() (x int, y string) {
if true {
return x, y
}
return
}
var x = func() (x int, y string) { return }

View File

@ -35,7 +35,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
for _, statement := range funcBody.List {
returnStmt, ok := statement.(*ast.ReturnStmt)
if ok && returnStmt.Results == nil && function.Type.Results != nil {
pass.Reportf(returnStmt.Pos(), "implicit return found for anonymous function")
pass.ReportRangef(returnStmt, "implicit return found in anonymous function")
}
}
case *ast.FuncDecl:
@ -43,7 +43,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
for _, statement := range funcBody.List {
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)
pass.ReportRangef(returnStmt, "implicit return found in function %s", function.Name)
}
}
default:

View File

@ -0,0 +1,11 @@
package analyzer
import (
"testing"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestAll(t *testing.T) {
analysistest.Run(t, analysistest.TestData(), Analyzer)
}

23
pkg/analyzer/testdata/example.go vendored Normal file
View File

@ -0,0 +1,23 @@
package main
func SimpleNakedReturn() (x int, y string) {
return // want `implicit return found in function SimpleNakedReturn`
}
func IgnorableProcedure() {
return // No return values expected, all good here!
}
func SimpleFunction() (x int, y string) {
return x, y // Return values are as foretold in the function signature
}
func MultipleReturnPaths() (x int, y string) {
if true {
return x, y // All good here, but...
}
return // want `implicit return found in function MultipleReturnPaths`
}
// Also works on anonymous functions!
var x = func() (x int, y string) { return } // want `implicit return found in anonymous function`