-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: reevaluate printf
check
#49350
Comments
I believe we should include it in the beta and see. |
IMO here are the 3 relevant cases of
[edited: the examples which previously used variables. I was wrong that these were involved.] Thanks to @dmitshur's detective work it is clear case 1 is as old as govet. 2 should also have previously reported. #49322 was caused by changing 3 to being reported. Going through vet's inclusion criteria. IIUC the reason for flagging any of these is that ending with "\n" in a "ln" function is considered to be a correctness problem as this is likely not what the programmer intended to output. So correctness box ticked. As yesterday indicates the Frequency box is clearly ticked. So we are really going to be debating vet's Precision criteria. Is each case getting the false positive to false negative ratio right for the programmer's intention of including the extra "\n" within Printlns? Given the age of cases 1 and 2, those are probably are getting it right. (Right?) Evidence for case 3 would be helpful. I am also inclined to wait and see in the beta. |
Can you please clarify what has changed in Go 1.18? It seems like case (1) should have been reported before. Why was it not? Cases (2) and (3) seem like maybe an overreach: What about |
@rsc My examples were bad. We are still not going through variables. Just "const x =". I've edited them. The only case I believe has changed in 1.18 was case 3. |
It sounds like you are confirming that the only thing that changed in vet is handling of +'ed string constants. |
I tested the vet change on all golang.org/x repos and it found issues in x/tools and x/exp, the rest were clean:
(Source: https://storage.googleapis.com/go-build-log/624eb6f7/linux-amd64_f52d45a1.log and https://storage.googleapis.com/go-build-log/624eb6f7/linux-amd64_de3063d6.log from CL 361094 slowbots.) Looking through the findings, they're almost all instances of CLI commands that print a usage string that happens to end with a newline, and rely on fmt.Fprintln to add a second newline. (Like cmd/cover and cmd/trace from #49322.) The one "fmt.Fprint call has possible formatting directive %s" finding from x/tools/cmd/eg happens because its -help output implementation uses const Help = `
This tool implements example-based refactoring of expressions.
[...]
The expression statement form is useful when the expression has no
result, for example:
func before(msg string) { log.Fatalf("%s", msg) }
func after(msg string) { log.Fatal(msg) }
[...]
` If we vendor the latest x/tools into gotip now so this is included in beta 1, it'll create red rows for x/tools and x/exp on build.golang.org, unless they're updated first. Please let me know how you'd like to proceed after considering this new information. |
That would mask any other regressions in those repos in the interim — it doesn't seem viable to me unless the fix CLs are already ready to merge, and at that point we may as well just merge them before the |
I agree. If vet owners suggest to include this change in beta 1, then the findings in x/tools and x/vet need to be resolved first. |
Change https://golang.org/cl/361716 mentions this issue: |
I can send the changes to the detected issues. The |
Regarding "fmt.Fprint call has possible formatting directive %s" finding from x/tools/cmd/eg, the tool is actually doing what it is supposed to do AFAIK. I can see several options for mitigating this:
My hunch is telling me 3). Thoughts? |
I would also lean toward (3). We could also do (1) for the short term to get the |
We need to fix our own code. Let's do that. I've commented twice in favor of keeping the change and explained why. |
My suggestion was not reverting the whole change, but only the part for formatting directive case. I see nothing wrong with the help string in the eg package, so reporting that looks like an FP to me (which is in fact blocking things). The reverting would happen until we figure out a better solution. |
Looking back at #30436, the So, let's keep it and go with just option (1). (Honestly, for these uses, |
I like this since, instead of changing the string, we would just change the printing mechanism. |
FWIW, I would rather use s/const/var/ to make clear that it's a good workaround for a spurious message. |
For non-os.File's, io.WriteString can be used instead. Is there a reason modifying a const into var would be better, given it'd allow other code to unintentionally modify something that was meant to be a constant? |
No one is going to unintentionally modify main.Help. |
Another possibility is help := Help // hide %s from vet |
Change https://golang.org/cl/361854 mentions this issue: |
For golang/go#49350 Change-Id: I6d3c50f56a61e666235e6d2e0ba3231768575218 Reviewed-on: https://go-review.googlesource.com/c/exp/+/361854 Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Zvonimir Pavlinovic <zpavlinovic@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org>
Also quiet neat. If there aren't any objections, I will send this fix then. |
Bit late to the party, but here are my 2 cents. I don't think we should be trying to adjust the checker to "fix forward" on features like false positives suppression at the moment. I'd prefer a revert of the original change to printf over this option. These 2 cents might not matter as we seem to be either fixing the help strings or |
For golang/go#49350 Change-Id: Ic566112ff8a8707998b461d2b99fca852f2972f8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/361716 Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Zvonimir Pavlinovic <zpavlinovic@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
I am yet to be convinced that
If the string is long and dynamically constructed, and is to be displayed in multiple lines, then using One case is that if the string already contains "\n" in the middle, then it is very likely that it is a multi-line string, and the vet checker should suppress the finding:
|
Change https://golang.org/cl/361094 mentions this issue: |
The Go 1.18 code freeze has recently started. This is a time to update all golang.org/x/... module versions that contribute packages to the std and cmd modules in the standard library to latest master versions. This CL updates only the tools module, keeping mod unchanged because its lastest commit isn't ready to be vendored yet. For #36905. Updates #49350. Change-Id: Ib39713d28a55fc9ec79058aab9919eba912def5f Reviewed-on: https://go-review.googlesource.com/c/go/+/361094 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Go Bot <gobot@golang.org>
The existing |
Right. I meant something like:
Such cases should be rare though. |
I think we decided to keep the more precise check, and we updated our code. The release notes alert people to the possible new errors. |
Go vet in 1.18 is more picky about fmt.*rintln and additional newlines at the end of output, thus producing errors like: ./cmd_info_test.go:1197:4: fmt.Fprintln arg list ends with redundant newline ./cmd_info_test.go:1201:4: fmt.Fprintln arg list ends with redundant newline ./cmd_routine_portal_info_test.go:101:4: fmt.Fprintln arg list ends with redundant newline ./cmd_routine_portal_info_test.go:161:4: fmt.Fprintln arg list ends with redundant newline ./cmd_run_test.go:1755:5: fmt.Fprintln arg list ends with redundant newline See golang/go#49350 and https://go.dev/doc/go1.18#vet Although I believe we should not be hitting this case, still it does not make sense to output so many newlines, so it's worth fixing. Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Recent work on #30436 improved the accuracy of the printf check greatly, and it started to uncover style issues previously undetected. For example, it detected problems like #49322,
fmt.Println arg list ends with redundant new line
.Even though I agree that this is a useful information in general, I want us to think if this type of check is severe enough to justify breaking existing tests when users upgrade
go
to 1.18.From @dmitshur's comment #49322 (comment)
Unlike in 2010, now
vet
is part ofgo test
. Improved accuracy is great, but it looks to me some of the checks included in the initial commit may be more suitable for linters orgopls
.The text was updated successfully, but these errors were encountered: