-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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/tools/gopls: yet more modernizer bugs #71847
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Change https://go.dev/cl/650815 mentions this issue: |
info.Defs[v] is nil if the loop variable is not declared (for i = 0 instead of for i := 0). + test Updates golang/go#71847 Change-Id: I28f82188e813f2d4f1ddc9335f0c13bd90c31ec1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650815 Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
Putting this in the v0.18.0 milestone until we make a decision. It seems premature to expose a tool that blanket-applies these modernizers. |
Change https://go.dev/cl/651095 mentions this issue: |
…ngeint bug info.Defs[v] is nil if the loop variable is not declared (for i = 0 instead of for i := 0). + test Updates golang/go#71847 Change-Id: I28f82188e813f2d4f1ddc9335f0c13bd90c31ec1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/650815 Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> (cherry picked from commit 300465c) Reviewed-on: https://go-review.googlesource.com/c/tools/+/651095 Auto-Submit: Robert Findley <rfindley@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> Commit-Queue: Alan Donovan <adonovan@google.com>
Here is another buggy transformation it did on my code: - start := strings.Index(origin, "//")
- if start < 0 {
- start = 0
- } else {
- start += 2
- }
+ start := max(strings.Index(origin, "//"), 0) |
Many thanks for the bug report. It appears that minmax failed to check that IfStmt.Else is nil in its matcher for pattern 2. Fix pending. |
Change https://go.dev/cl/651375 mentions this issue: |
The matcher for pattern 2 forgot to check that the IfStmt.Else subtree was nil, leading to unsound fixes. Updates golang/go#71847 Change-Id: I0919076c1af38012cedf3072ef5d1117e96a64b9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651375 Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
I added #71878 but it seems to be the same bug that @erikdubbelboer reported (I checked the OP here and not the comments first. Sorry) |
Don't apologize, filing a separate issue is much appreciated. Let's make that issue the canonical one for this bug. |
Change https://go.dev/cl/651237 mentions this issue: |
…nmax bug The matcher for pattern 2 forgot to check that the IfStmt.Else subtree was nil, leading to unsound fixes. Updates golang/go#71847 Change-Id: I0919076c1af38012cedf3072ef5d1117e96a64b9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651375 Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> (cherry picked from commit 96bfb60) Reviewed-on: https://go-review.googlesource.com/c/tools/+/651237 Reviewed-by: Robert Findley <rfindley@google.com>
Here is a buggy transformation by "modernize":
Before the change, |
Thanks; this is the same bug as above (and thus #71878), which is now fixed at head. We'll release the patch today. |
First, thanks a lot for the tool! I'm slowly applying on our codebase and it works super well. I found one additional bug in the transformation of |
Good point, thanks for reporting it. I've split this out as #71952. Glad it (mostly) works for you. |
Reminder: use this command to batch-apply fixes:
(Unfortunately the vital -fix flag was missing from the original release notes.) |
Yes, that's what I'm using. I'm doing it package by package, to get smaller diff and avoid merge conflict in our fast changing monorepo. My complete process is actually, for each packages:
|
That's a good idea. Others have pointed out the |
Found after running modernize on std + cmd:
maps
transformations tomaps
package itself or its in-package tests.slices
(or almost any other package) fromruntime
or its dependencies.for i := 0; i < 1e3; i++
->for range 1e3
requires an int conversion or change of literal.for i := 0; i < n; i++
->for range n
is invalid if the loop body contains i++ (I thought we already checked this?). Example: path/filepath.scanChunk (Fixed by https://go.dev/cl/650815 )var i int; for i = 0; i < n; i++
->for range n
removes uses of i that might make local var i unreferenced (example)Refactoring is hard. :( We may need to be selective about which categories we apply without review until all these bugs are fixed. Perhaps we also need a flag to select categories too.
Mistakes that break the build are of low severity, because the mistake is impossible to miss and the remedy is obvious. Loss of commentary is of medium severity as it may go undetected due to reviewer fatigue in a large CL. The really severe problems are latent behavior changes such as the "loop body contains i++" case above (now fixed).
The text was updated successfully, but these errors were encountered: