dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.12k stars 371 forks source link

SA4020 could be slightly more aggressive #1477

Open seebs opened 9 months ago

seebs commented 9 months ago
type badRequest struct { error }
type notFound error

func checkError(e error) int {
    switch e.(type) {
    case badRequest: return http.StatusBadRequest
    case notFound: return http.StatusNotFound
    default: return http.InternalServerError
    }
    return http.StatusOK
}

this is simplified from an actual case where we got bitten by this. this is equivalent to:

case notFound: return http.StatusNotFound
case error: return http.InternalServerError

which I think SA4020 would catch.

meteorgan commented 5 months ago

@seebs why is the origin code equivalent to

case notFound: return http.StatusNotFound
case error: return http.InternalServerError

the below test cases all passed

func TestCheckRequestError(t *testing.T) {
    badRequestErr := badRequest{errors.New("xxx")}
    assert.Equal(t, 400, checkRequestError(badRequestErr))

    var notFoundErr notFound = errors.New("not found")
    assert.Equal(t, 404, checkRequestError(notFoundErr))

    err := errors.New("error")
    assert.Equal(t, 404, checkRequestError(err))
}