-
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: puts else if
comments in wrong place
#20562
Comments
It's not clear to me that this is wrong. The B and C comments are within the "{" and "}" of the containing if block, so it makes sense to associate the comment at that indentation level. \cc @griesemer |
elseif
comments in wrong placeelse if
comments in wrong place
@dsnet If the comments didn't have a space after the previous if statements contents, and if they weren't right against the following line, I'd agree. Where else would you put the comment that should precede the else if? I've amazingly found an edge case which just wasn't considered previously. Thanks for looking into it. |
I agree with dsnet.
If a comment is about branch B, I'd definitely put it inside the branch it refers to. Like this:
IMHO putting a comment about B before its |
I think this is effectively a dup of #9910. As Russ says there:
|
@josharian Nice find! I'm okay with the fix in #9910 about leaving them untouched. It would be better than them always getting moved incorrectly in my case! Thanks for the comment! |
Cool. Closing this as a dup, then. |
@josharian Sorry if I wasn't clear, but this issue isn't fixed, the other one took the right approach which I think we should too! |
OK, reopening, but I'm confused. I clicked on your original playground link, hit format, and nothing moved, which seems like exactly the proposed resolution--when there's ambiguity, don't move things. What am I missing? |
@josharian Click format on this link: https://play.golang.org/p/Odchgkj1Tg I would expect either nothing to move, but unfortunately it does. On the earlier link, I would expect things to move to look like this link, or if we agree with the solution in #9910 for nothing to move. |
Found a workaround https://play.golang.org/p/mcx57OdNvy It's a bit awkward, but at least it's gofmt legal, and the comments are at the right indentation level. |
Full code for those who hate link following:
|
I disagree completely. The bug in gofmt is that it this awkward workaround is legal. I would hate to this type of style in production, especially since gofmt doesn't fix it. Removing the strategically-placed comments fixes it, but it requires the comments to be removed/replaced. It would be nice if gofmt was less lenient with this. |
On Sat, Jun 24, 2017 at 4:55 AM, as ***@***.***> wrote:
It would be nice if gofmt was less lenient with this.
The fix to "leave comments as they were" when nested above an else if would
be great!
|
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?play.golang.org
What operating system and processor architecture are you using (
go env
)?play.golang.org
What did you do?
Ran this: https://play.golang.org/p/xJ0-Cq8ZYi and clicked
format
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
here it is: https://play.golang.org/p/xJ0-Cq8ZYi
What did you expect to see?
I expected the B and C branch comments to be aligned the same way the A branch comment is..
What did you see instead?
Instead they were indented as if they were part of the previous block.
I get how this is a tricky issue, but if there is a blank line and the comment is right before the next block, I think it should be indented for that block.
Thanks!
The text was updated successfully, but these errors were encountered: