-
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/gofmt: inconsistent indentation with comments in switch #38495
Comments
CC @griesemer |
Hm, indeed. Looks like a bug at first glance. Thanks for reporting. |
Why is this a bug? The gofmt formatting of If the code in the expected section is formatted, it remains the same (this can be tried here). The issue is how gofmt can know which of the two cases the author wants (one would likely be a I note there is some discussion on this in gophers #tools that would be relevant to bring in here. |
The issue from the slack discussion was when you manually moved the comments into alignment with the case (leading |
I'm familiar with this gofmt behavior, and I've spent some time thinking whether it's a bug or not (if it is, it's very minor). I agree with @kortschak's characterization of this issue. Given there are two valid gofmt-ed outcomes, I think gofmt should be making the edit which results in a smaller diff. So, given this non-gofmted block: for {
switch n {
case 1:
foo()
// Comment that applies to case 1 or case 2 depending on its indentation.
case 2:
bar()
}
} After formatting, it should become: for {
switch n {
case 1:
foo()
// Comment that applies to case 1 or case 2 depending on its indentation.
case 2:
bar()
}
} This is the current behavior. I think it's good. However, the other side doesn't work as expected. Starting with this non-gofmted block: for {
switch n {
case 1:
foo()
// Comment that applies to case 1 or case 2 depending on its indentation.
case 2:
bar()
}
} gofmt currently inserts two tabs instead of one tab. Given both one tab and two tabs would make the program gofmted, I think it should be making the smaller edit rather than the bigger. |
Seems like this and similar cases have come up a couple times now, see #30327 and its linked issues. I guess the issue here is that there is no agreed upon definition on how comments in switch-statements should be inlined, but I agree with the above proposal. However, a question that would come up is still the comment at the end of the whole package main
func main() {
val := 1
switch val {
case 1:
_ = 1
// case 5: // gets aligned to the case <- this would be left as-is, which makes sense.
// _ = 5
case 2:
_ = 2
// case 3: // gets aligned to the code block inside the case <- this would be left-as-is?
// _ = 3
}
} While the This would be an enhancement to what has been said by @dmitshur, for completeness. |
This is a bug (or quirk) that has existed for many years, and it's very minor. It seems too late to work on a fix this late in the 1.15 cycle, so I'll move this to backlog and we can revisit in the future. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes, also in the playground
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
write inconsistently indented code/comments in a switch
https://play.golang.org/p/QWIBPjclreK
What did you expect to see?
consistently indented comments
What did you see instead?
observations
this does not happen when the comment and the case below it are indented by the same number of whitespace characters, so the below formats properly
The text was updated successfully, but these errors were encountered: