-
Notifications
You must be signed in to change notification settings - Fork 18k
tools/gopls/internal/analysis/modernize: stringscutprefix offers invalid fix #73547
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
Comments
Change https://go.dev/cl/669055 mentions this issue: |
cc: @xieyuschen |
I saw there is an ongoing CL, and left some comments already. My idea is just to skip offering a fix for this scenario rather than moving init declaration to the if stmt. |
Ignoring the variable initialization scenario seems to be achievable with minimal changes based on the existing code. However, if I move I'm not sure if this situation should also be modified in the same way. I would like to get more opinions from two contributors on this. cc @xieyuschen @adonovan Perhaps I can first make the changes to the code and temporarily ignore this scenario. |
Oh, Thanks. I saw your comment regarding the example above where auth might be used later. I conducted a test using the current code of CL669055 with significant changes (though it still needs test fixes), and it won't make any modifications in that case. package main
import (
"fmt"
"strings"
)
func main() {
var tokenStr string
demoMap := make(map[string]string)
demoMap["Authorization"] = "Bearer 123456789abc"
if auth := demoMap["Authorization"]; strings.HasPrefix(auth, "Bearer ") {
fmt.Println("auth is:", auth)
tokenStr = strings.TrimPrefix(auth, "Bearer ")
}
fmt.Println("tokenStr is:", tokenStr)
} (Running modernize -fix -test ./... will not make any changes) However, if So we may have to make a choice between functionality and the complexity of code refactoring. I also tend to prefer ignoring this scenario for now. |
I prefer to separate the fix CL and another CL to support more patterns. So the first CL could get a quick win.
Looks like you want to inline |
Yes, this can get complicated. Maybe we should keep it simple and just ignore this case. I'll update the code later, thanks |
Go version
go1.24.2
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
After executing
modernize -fix -test ./...
, it will become.This will result in a compilation failure.
What did you expect to see?
In fact, what we expect is something like this:
The text was updated successfully, but these errors were encountered: