Closed rski closed 9 months ago
Thanks, where did you spot this ? (which open source project ?)
it's not from an open source codebase :-)
the above is basically a case I found in multiple places that do something like
err := foo()
errStr := fmt.Sprint(err)
if !strings.Contains(errStr, "some expected thingy") { t.Fatal() }
which would panic if it were replaced with this and the error were nil
err := foo()
if !strings.Contains(err.Error(), "some expected thingy") { t.Fatal() }
Solved by making this an optional off by default feature
I suggest that you change your code into
err := foo()
if err != nil {
if !strings.Contains(fmt.Sprint(err), "some expected thingy") { t.Fatal() }
}
Then, the optimization will work
this will panic on .Error. The warning printed could explain that this is only recommended for non-nil errors