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

x/lint: warn about unnecessary detailed verbs in format strings #27073

Closed
dvyukov opened this issue Aug 18, 2018 · 3 comments
Closed

x/lint: warn about unnecessary detailed verbs in format strings #27073

dvyukov opened this issue Aug 18, 2018 · 3 comments

Comments

@dvyukov
Copy link
Member

dvyukov commented Aug 18, 2018

Context:
People with C/C++ background tend to do unnecessary detailed verbs in format strings, i.e.:

fmt.Sprintf("%s %d %f", person.Name, person.Age, person.Salary)

instead of:

fmt.Sprintf("%v %v %v", person.Name, person.Age, person.Salary)

which leads to additional iterations and manual comments on pull requests. I was asked if this can be checked with a linter instead (google/syzkaller#678 (comment)).
Using %v unless something special is needed feels more idiomatic and looks better to me because:

  • no need to remember types of all fields
  • more robust on var type changes
  • also prints arrays and structs

But now I can't actually find any documentation anywhere that %v is indeed more idiomatic and the recommended way. So is it really the recommended way? Are there any advantages in using %d/f/s in common case? Does it make sense to make lint to warn when %v would give the same format?

@dominikh

@dominikh
Copy link
Member

Personally I've always preferred using exact verbs. Vet complains when the types of arguments don't match the verbs, which acts as a kind of soft static checking of Printf calls. For example, it can protect against incorrect order of arguments.

@mvdan
Copy link
Member

mvdan commented Aug 18, 2018

I too prefer exact verbs sometimes, in particular when I know I want that type of data to be printed. For example, if a tool is supposed to print a number, I don't want it to happily print a string or a slice if I somehow mess up the code.

@dvyukov
Copy link
Member Author

dvyukov commented Aug 18, 2018

Okay, then I guess we can close this because it does not seem that we will come to unanimous consensus here.

@golang golang locked and limited conversation to collaborators Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants