diff --git a/nakedret.go b/nakedret.go index 667a14e..7787b0b 100644 --- a/nakedret.go +++ b/nakedret.go @@ -64,7 +64,7 @@ func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error files, err := parseInput(args, fset) if err != nil { - return fmt.Errorf("could not parse input %v", err) + return fmt.Errorf("could not parse input: %v", err) } if maxLength == nil { diff --git a/nakedret_test.go b/nakedret_test.go index 1533e76..5d0908a 100644 --- a/nakedret_test.go +++ b/nakedret_test.go @@ -7,6 +7,37 @@ import ( "testing" ) +type testParams struct { + filename string + maxLength uint +} + +var testcases = []struct { + name string + expected string + params testParams +}{ + {"return in block", `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function +`, testParams{ + filename: "testdata/ret-in-block.go", + maxLength: 0, + }}, + {"ignore short functions", ``, testParams{ + filename: "testdata/ret-in-block.go", + maxLength: 10, + }}, + {"nested function literals", `testdata/nested.go:16: Bad naked returns on 6 line function +testdata/nested.go:21: naked returns on 2 line function +testdata/nested.go:28: naked returns on 2 line function +testdata/nested.go:32: naked returns on 2 line function +testdata/nested.go:36: naked returns on 2 line function +testdata/nested.go:40: naked returns on 2 line function +`, testParams{ + filename: "testdata/nested.go", + maxLength: 0, + }}, +} + func runNakedret(t *testing.T, filename string, maxLength uint, expected string) { t.Helper() defer func() { @@ -21,25 +52,16 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string) if err := checkNakedReturns([]string{filename}, &maxLength, false); err != nil { t.Fatal(err) } - actual := string(logBuf.Bytes()) + actual := logBuf.String() if expected != actual { - t.Errorf("Unexpected output:\n-----\n%s\n-----\n", actual) + t.Errorf("Unexpected output:\n-----\ngot: \n%s\nexpected: \n%v\n-----\n", actual, expected) } } -func TestReturnInBlock(t *testing.T) { - expected := `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function -` - runNakedret(t, "testdata/ret-in-block.go", 0, expected) -} - -func TestIgnoreShortFunctions(t *testing.T) { - runNakedret(t, "testdata/ret-in-block.go", 10, "") -} - -func TestNestedFunctionLiterals(t *testing.T) { - expected := `testdata/nested.go:16: Bad naked returns on 6 line function -testdata/nested.go:21: naked returns on 2 line function -` - runNakedret(t, "testdata/nested.go", 0, expected) +func TestCheckNakedReturns(t *testing.T) { + for _, tt := range testcases { + t.Run(tt.name, func(t *testing.T) { + runNakedret(t, tt.params.filename, tt.params.maxLength, tt.expected) + }) + } } diff --git a/testdata/nested.go b/testdata/nested.go index 71b9b88..fac4778 100644 --- a/testdata/nested.go +++ b/testdata/nested.go @@ -22,3 +22,21 @@ func BadNested() { } return } + +func MoreBad() { + var _ = func() (err error) { + return + } + + func() (err error) { + return + }() + + defer func() (err error) { + return + }() + + go func() (err error) { + return + }() +}