golang linter always complains

Issue

Q: How do I resolve this madness between the ireturn and nolintlint linters?

Details:

I have a Golang function with this signature

func NewClientCredentialsTokenSource(
    issuer string,
    clientId string,
    clientSecret string,
    scope []string,
) (oauth2.TokenSource, error) {

When I run golangci-lint v1.43.0 it reports

golangci-lint run
oidc/token_utils.go:19:1: NewClientCredentialsTokenSource returns interface (golang.org/x/oauth2.TokenSource) (ireturn)
func NewClientCredentialsTokenSource(

Since the function has only two return params it is easy to deduce it is complaining about oauth2.TokenSource and not error.

The downstream function called by NewClientCredentialsTokenSource returns an instance of oauth2.TokenSource so I don’t have a concrete type to return. I have no choice but to return the oauth2.TokenSource interface.

So I add a lint exception to the function like this:

//nolint:ireturn
func NewClientCredentialsTokenSource(
    issuer string,
    clientId string,
    clientSecret string,
    scope []string,
) (oauth2.TokenSource, error) {

You’d think that should fix it but no! Now there’s a new lint issue is reported:

golangci-lint run
oidc/token_utils.go:19:1: directive `//nolint:ireturn` is unused for linter "ireturn" (nolintlint)
//nolint:ireturn

So now I’m chasing my tail. ireturn complains I am returning an interface. I add an exception for that function only to have nolintlint complain I have an exception that doesn’t apply.

What’s a guy to do?

Solution

I tried adding the allow rule as suggested by Nico Huysamen but that opened a can of worms. In the end I still want to believe in the ireturn linter and don’t want to disable it yet. I figure the cleanest way to resolve this issue is to add an exception for both linters, ireturn and nolintlint as shown here:

//nolint:nolintlint,ireturn
func NewClientCredentialsTokenSource(
    issuer string,
    clientId string,
    clientSecret string,
    scope []string,
) (oauth2.TokenSource, error) {

Update 5/25/2022:

Perhaps a better solution is shown below. For some reason the ireturn exception has to go inside the function signature.

func NewPasswordGrantTokenSource( //nolint:ireturn
    issuer string,
    clientId string,
    clientSecret string,
    username string,
    password string,
    scope []string,
) (oauth2.TokenSource, error) {

Answered By – HairOfTheDog

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.