Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: false positive about unrecognized printf flag '0' #23598

Closed
octomarat opened this issue Jan 29, 2018 · 8 comments

Comments

Projects
None yet
7 participants
@octomarat
Copy link

commented Jan 29, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

o1.9.3 darwin/amd64

What did you do?

Run go vet on the following file:

package main

import "fmt"

func main() {
	fmt.Printf("%08t", false)
}

What did you expect to see?

No errors are reported as '0' flag is valid for 't' verb. Program prints 000false, zeros are used for padding instead of spaces.

What did you see instead?

vet reports unrecognized printf flag for verb 't': '0'.

@robpike

This comment has been minimized.

Copy link
Contributor

commented Jan 29, 2018

I see a couple of problems in there.

@mvdan

This comment has been minimized.

Copy link
Member

commented Jan 29, 2018

@robpike could you elaborate?

@octomarat

This comment has been minimized.

Copy link
Author

commented Jan 29, 2018

Hm... fmt documentation states that:
Flags are ignored by verbs that do not expect them. For example there is no alternate decimal format, so %#d and %d behave identically.
But go vet reports unrecognized flags as error. It this even correct? I understand that removing such redundancies from a source code is a good thing. But what about go vet executed before tests in Go 1.10?

@octomarat

This comment has been minimized.

Copy link
Author

commented Jan 29, 2018

Speaking about other problems of '0' flag processing by go vet.

fmt.Printf("%020p \n", &[]int{})
fmt.Printf("%020T \n", false)
fmt.Printf("%020U \n", 1234)

In all these cases '0' affects print result, but is reported by go vet as unrecognized.

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Jan 29, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jan 29, 2018

This does not seem like unreasonable go vet behavior to me. The code is almost certainly mistaken even if fmt carries on as best it can.

@dolmen

This comment has been minimized.

Copy link

commented Jan 29, 2018

Here is an additional test case using fmt.Formatter to let you decide what should be the bahaviour of "go vet":

package main

import (
	"fmt"
)

type t struct{}

func (_ t) Format(f fmt.State, c rune) {
	if f.Flag('0') {
		f.Write([]byte("GA"))
	} else {
		f.Write([]byte("BU"))
	}
}

func main() {
	fmt.Printf("%0s\n", t{})
}
@robpike

This comment has been minimized.

Copy link
Contributor

commented Jan 30, 2018

https://go-review.googlesource.com/c/go/+/90695 was supposed to mention this issue but got the issue number wrong.

@spf13 spf13 added the NeedsFix label Mar 26, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 26, 2018

@golang golang locked and limited conversation to collaborators Mar 26, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.