Go: cmd/vet: %w in Printf call should be flagged

Created on 8 Nov 2019  ·  9Comments  ·  Source: golang/go

package main

import (
    "errors"
    "fmt"
)

func main() {
    err := errors.New("ABC")
    fmt.Printf("error: %w\n", err)
}

https://play.golang.org/p/-kQ6HtIIMG7

vet should complain about the %w verb. Only in Errorf format strings does it have meaning.

FrozenDueToAge NeedsInvestigation

Most helpful comment

This also seems like a pretty simple issue for anyone wanting to get started contributing to vet.

All 9 comments

CC @ianthehat @matloob @dominikh

This also seems like a pretty simple issue for anyone wanting to get started contributing to vet.

I would love to take a shot on this. Any pointers to get started on this issue?

The file in question is this one - https://github.com/golang/tools/blob/master/go/analysis/passes/printf/printf.go. Look at how other verbs are handled and take it from there.

Ah I'm on the right track then. Thank you! I'm working on this 😃

vet already complains about the %w verb in printf. I ran the above code and this is what I got:
./main.go:10:2: Printf format %w has unknown verb w.

You are right. This was fixed by https://github.com/golang/tools/commit/1d1727260058c6d58ebfdeb982c4657342282355. The original issue was reported by @josharian himself which was about passing strings to a %w parameter. But the commit reported usages of %w with non-Errrof functions too.

The error message is Printf call has error-wrapping directive %w though, not the one you mentioned.

I will go ahead and close this one. Thanks for looking into it @Inconnu08.

Exactly. But the end error message is the above one reported by vet. Anytime!

Oops, sorry. :( I thought I tested with tip but apparently only did 1.13. Thanks, @Inconnu08.

Was this page helpful?
0 / 5 - 0 ratings