Skip to content

x/tools/go/analysis/passes/printf: say what the right type is in the analyzer #59875

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

Closed
b0ri5 opened this issue Apr 27, 2023 · 5 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@b0ri5
Copy link

b0ri5 commented Apr 27, 2023

The analyzer output at https://github.com/golang/tools/blob/master/go/analysis/passes/printf/printf.go#L889 knows that you're using the wrong type, but does not specify what the right type should be and it seems like it could.

What did you expect to see?

The correct format string to use. Something like

format %s has arg <redacted code> of wrong type bool, should be %t. See also https://pkg.go.dev/fmt#hdr-Printing

What did you see instead?

format %s has arg <redacted code> of wrong type bool see also https://pkg.go.dev/fmt#hdr-Printing

Would it be feasible to include the correct type in the output?

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Apr 27, 2023
@gopherbot gopherbot added this to the Unreleased milestone Apr 27, 2023
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 28, 2023
@dmitshur dmitshur changed the title x/tools/go/analysis: Say what the right type is for the printf analyzer x/tools/go/analysis/passes/printf: say what the right type is in the analyzer Apr 28, 2023
@dmitshur
Copy link
Contributor

CC @timothy-king.

@timothy-king
Copy link
Contributor

  1. There is always some tension between more informative and being pithy and precise in vet warnings. IMO the current message is already a bit long, but no part of it is redundant. The end is directing users to a helpful place to self service format %s has arg <redacted code> of wrong type bool see also https://pkg.go.dev/fmt#hdr-Printing. We have URLs fields coming soon so perhaps this verbosity will soon no longer required as we can redirect to a landing page that then has the equivalent of see also https://pkg.go.dev/fmt#hdr-Printing. So vet may have more space to play with soon.

  2. So the message is already pretty helpful. We are mostly discussing whether the message could be more helpful.

  3. I am somewhat hesitant to think we know user intent here. The checker does know something is wrong so the current message is fine from that regard. Adding a recommendation ("should") kinda requires having an opinion about user intent. And there are also multiple choices valid. Why not recommend %v? We can soften the language do be (Did you mean %t?).

  4. I am not sure we want to add a recommendation just for bool. What do we think about other cases? Is it all of the types that have a default format for %v? Do we really want to recommend %d over %x or %v for int?

Thoughts from others?

@AndrewHarrisSPU
Copy link

Thoughts from others?

At a glance, is incompatible type any better than wrong type?

Occasionally it's relevant if some wrong type implements fmt.Formatter, or has a GoString, String, or Error methods, if there's an inclination to provide a guess about what verbs are compatible.

@adonovan
Copy link
Member

adonovan commented May 3, 2023

I agree with @timothy-king. The message tells you the offending expression, type, and format verb. Usually there are multiple possible fixes among which the tool has no basis to choose---was the problem with the format or the argument?---yet the appropriate fix is obvious to the user.

@adonovan adonovan closed this as completed May 3, 2023
@b0ri5
Copy link
Author

b0ri5 commented May 3, 2023

Great, thank you for taking a look at this!

@golang golang locked and limited conversation to collaborators May 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants