Antonboom / testifylint

The Golang linter that checks usage of github.com/stretchr/testify.
https://github.com/stretchr/testify
MIT License
101 stars 8 forks source link

error-nil: mixed struct "type" can lead to false-positive (v1.3.0) #95

Closed ldez closed 6 months ago

ldez commented 6 months ago
$ golangci-lint run -E testifylint
main_test.go:32:2: error-nil: use assert.NoError (testifylint)
        assert.Nil(t, resp)
        ^

The example is a fake API client.

package main

import (
    "testing"

    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

// APIResponse can be an error or not.
type APIResponse struct {
    Status   int
    Data     string
    ErrorMsg string
}

func (a APIResponse) Error() string {
    return a.ErrorMsg
}

func Update(a string) (*APIResponse, error) {
    if a == "a" {
        return &APIResponse{Status: 200, Data: "fake"}, nil
    }

    return nil, &APIResponse{Status: 500, ErrorMsg: "Oops"}
}

func TestName(t *testing.T) {
    resp, err := Update("b")
    require.Error(t, err, new(*APIResponse))
    assert.Nil(t, resp)
}
Antonboom commented 6 months ago

@ldez hi!

thank you for request.

the checker intentionally reacts on everything “that satisfies the error interface”, but I think it needs to be changed to the strict “has type error” 👌