diff --git a/ql/src/InconsistentCode/WrappedErrorAlwaysNil.qhelp b/ql/src/InconsistentCode/WrappedErrorAlwaysNil.qhelp new file mode 100644 index 00000000000..8a027db4be2 --- /dev/null +++ b/ql/src/InconsistentCode/WrappedErrorAlwaysNil.qhelp @@ -0,0 +1,54 @@ + + + + +

+ The pkg.errors package provides the errors.Wrap + function for annotating an error with a stack trace. When passed + nil, this function returns nil. + + When the first parameter to errors.Wrap is always + nil, the function call has no effect and likely indicates a + programming mistake. + + A common example of this is when an errors.Wrap(err, "message") + cqll is copied from an earlier error-handling block in the same function and + used in a subsequent error-handling block that does not check + err in its guard. In this case the return of a nil + value to the caller indicates by convention that the operation succeeded, and + so the failure is masked. +

+
+ + +

+ Usually an err value is being referenced outside its intended + scope. The problem can be fixed by removing that reference, for example by + changing a call of the form errors.Wrap(err, "message") to + errors.New("message"). +

+
+ + +

+ The example below shows an example where the err value returned + from the call to f1 is reused in a later call, when it is known + to be nil: +

+ + + +

+ One way of fixing this is to create a new error value with + errors.New: +

+ + +
+ + +
  • errors package - github.com/pkg/errors - pkg.go.dev: errors.Wrap
  • +
    +
    diff --git a/ql/src/InconsistentCode/WrappedErrorAlwaysNilGood.go b/ql/src/InconsistentCode/WrappedErrorAlwaysNilGood.go new file mode 100644 index 00000000000..9ce421868e7 --- /dev/null +++ b/ql/src/InconsistentCode/WrappedErrorAlwaysNilGood.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/pkg/errors" +) + +func f1(input string) error { + if input == "1" { + return errors.Errorf("error in f1") + } + return nil +} + +func f2(input string) (bool, error) { + if input == "2" { + return false, errors.Errorf("error in f2") + } + return true, nil +} + +func test1(input string) error { + err := f1(input) + if err != nil { + return errors.Wrap(err, "input is the first non-negative integer") + } + if ok2, _ := f2(input); !ok2 { + return errors.New("input is the second non-negative integer") + } + return nil +}