diff options
author | Teddy Wing | 2023-05-25 20:40:08 +0200 |
---|---|---|
committer | Teddy Wing | 2023-05-25 20:40:08 +0200 |
commit | 8bcef611f794272203e05e207259ac8d45d558fb (patch) | |
tree | ec633067878ab77f8b2c10ca438fd3fcb4582fc6 /testdata/signature.go | |
parent | c7ea2c3d8fd7d4536f04d060cc57307d15a340df (diff) | |
download | godefererr-8bcef611f794272203e05e207259ac8d45d558fb.tar.bz2 |
Report missing signature declaration with multiple return values
The previous message which reported when a function's return signature
didn't declare an error variable didn't work for functions with multiple
return values.
I thought about extending it and coming up with generated variable names
for the non-error types to be able to support automatic fixing, but this
idea ended up being too complicated. Also, I didn't like the idea of an
automated fixer coming up with generated variable names, because you'd
need to manually rename them anyway. That kind of defeats the purpose of
it being automated.
Instead, change the diagnostic message to only refer to the error
variable and recommend what the declared variable name should be based
on the identifier the error was assigned to in the defer.
Modify our multiple return value test functions according to this new
approach.
Diffstat (limited to 'testdata/signature.go')
-rw-r--r-- | testdata/signature.go | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/testdata/signature.go b/testdata/signature.go index 0da2bb0..cc9d72b 100644 --- a/testdata/signature.go +++ b/testdata/signature.go @@ -2,7 +2,7 @@ package main import "errors" -func shouldDeclareErrInSignature() error { // want "return signature should be '\\(err error\\)'" +func shouldDeclareErrInSignature() error { // want "return signature should use named error parameter err" var err error // Should use variable declared in signature err = nil @@ -45,7 +45,7 @@ func returnedErrorMustMatchDeferErrorName() (err error) { return err2 // want "does not return 'err'" } -func deferUsesUnconventionalErrName() error { // want "return signature should be '\\(anErr error\\)'" +func deferUsesUnconventionalErrName() error { // want "return signature should use named error parameter anErr" var anErr error anErr = nil @@ -60,9 +60,8 @@ func deferUsesUnconventionalErrName() error { // want "return signature should b return anErr } -// TODO: This is starting to look like needless complexity. Maybe we just -// report that the error variable must be declared in the signature instead. -func multipleReturnValuesString() (string, error) { // want "return signature should be '\\(string1 string, err error\\)'" + +func multipleReturnValuesString() (string, error) { // want "return signature should use named error parameter err" var err error = nil if err != nil { return "", err @@ -77,17 +76,17 @@ func multipleReturnValuesString() (string, error) { // want "return signature sh type AStruct struct {} -func multipleReturnValuesStruct() (*AStruct, error) { // want "return signature should be '\\(aStruct1 *AStruct, err error\\)'" +func multipleReturnValuesStructBool() (*AStruct, bool, error) { // want "return signature should use named error parameter err" var err error = nil if err != nil { - return nil, err + return nil, false, err } defer func() { err = errors.New("defer error") }() - return &AStruct{}, err + return &AStruct{}, true, err } func good() (err error) { |