Reorganize and get tests working
This commit is contained in:
parent
a065a68449
commit
02f24a7ea1
|
@ -0,0 +1,4 @@
|
||||||
|
linters:
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- explicitreturn
|
|
@ -2,7 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.org/x/tools/go/analysis/singlechecker"
|
"golang.org/x/tools/go/analysis/singlechecker"
|
||||||
"tildegit.org/indigo/explicitreturn/analyzer"
|
"tildegit.org/indigo/explicitreturn/pkg/analyzer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
22
example.go
22
example.go
|
@ -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 }
|
|
|
@ -35,7 +35,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
for _, statement := range funcBody.List {
|
for _, statement := range funcBody.List {
|
||||||
returnStmt, ok := statement.(*ast.ReturnStmt)
|
returnStmt, ok := statement.(*ast.ReturnStmt)
|
||||||
if ok && returnStmt.Results == nil && function.Type.Results != nil {
|
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:
|
case *ast.FuncDecl:
|
||||||
|
@ -43,7 +43,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
for _, statement := range funcBody.List {
|
for _, statement := range funcBody.List {
|
||||||
returnStmt, ok := statement.(*ast.ReturnStmt)
|
returnStmt, ok := statement.(*ast.ReturnStmt)
|
||||||
if ok && returnStmt.Results == nil && function.Type.Results != nil {
|
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:
|
default:
|
|
@ -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)
|
||||||
|
}
|
|
@ -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`
|
Loading…
Reference in New Issue