You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Quick fix 1002 suggests use of tagged switch statement inappropriately #1613
Consider the following code, which I admit could use strings.Cut instead:
// Variable "c" is of type string.// We wish to split it on an embedded space character, if any.switchi:=strings.IndexByte(c, ' '); {
casei==-1:
returnc, "", nilcasei==0:
return"", "", errors.New("value starts with a space character")
default:
returnc[:i], c[i+1:], nil
}
My LSP implementation—gopls—reports the following:
could use tagged switch on i [default]
It then offers a quick fix: "Replace with tagged switch". If I accept that suggestion, though, the resulting code is invalid, since I lose access to the "i" variable used in the return statement in the default case.
// Note the extra "i" here.switchii:=strings.IndexByte(c, ' '); {
case-1:
returnc, "", nilcase0:
return"", "", errors.New("value starts with a space character")
default:
// We can no longer use "i" here.returnc[:i], c[i+1:], nil
}
I reckon the intended rewrite is switch i := strings.IndexByte(c, ' '); i { which AFAICT would be correct. I'll have to check tomorrow if we emit a broken quickfix, or if this is an instance of golang/go#63930.
Activity
dominikh commentedon Oct 24, 2024
I reckon the intended rewrite is
switch i := strings.IndexByte(c, ' '); i {which AFAICT would be correct. I'll have to check tomorrow if we emit a broken quickfix, or if this is an instance of golang/go#63930.dominikh commentedon Oct 24, 2024
2739fd0 fixes this. It will take a release of Staticcheck and a release of gopls for this to reach users, however.
seh commentedon Oct 24, 2024
Thank you for the fast diagnosis and correction!
I'm going to rewrite the original function to use
strings.Cut, but I'm glad that its current state allowed us to find this burr to sand off.quickfix/qf1002: emit valid fix for switches with initialization stat…