Skip to content
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: comments under case statements indented inconsistently #20681

Closed
stjj89 opened this issue Jun 15, 2017 · 5 comments
Closed

cmd/gofmt: comments under case statements indented inconsistently #20681

stjj89 opened this issue Jun 15, 2017 · 5 comments
Assignees
Milestone

Comments

@stjj89
Copy link
Contributor

@stjj89 stjj89 commented Jun 15, 2017

go version go1.8.1.typealias linux/amd64

gofmt indents comments under case statements differently, depending on whether or not there is a subsequent default case.

https://play.golang.org/p/04RrASTyjn

Press "Format" and notice that "// CommentBar" is properly indented, whereas "// CommentFoo" is missing an indent.

@bradfitz bradfitz changed the title gofmt: comments under case statements indented inconsistently cmd/gofmt: comments under case statements indented inconsistently Jun 15, 2017
@bradfitz bradfitz added this to the Go1.10 milestone Jun 15, 2017
@griesemer
Copy link
Contributor

@griesemer griesemer commented Jun 15, 2017

That's because it's not obvious whether //CommentFoo belongs to the (missing) code for case "a" or whether it's a comment for the next (default) case. There's some heuristics one could employ but there's no clear-cut approach. In fact, if you indent manually, gofmt accepts that as well.

Leaving open for future consideration.

@mattn
Copy link
Member

@mattn mattn commented Jun 15, 2017

It works well for me.

package test

func foo(a string) {
	switch {
	// if a is "a"
	case a == "a":
		// then call doSomething()
	// not all
	default:
		// then call doOtherThing()
	}
}
@stjj89
Copy link
Contributor Author

@stjj89 stjj89 commented Jun 15, 2017

That's because it's not obvious whether //CommentFoo belongs to the (missing) code for case "a" or whether it's a comment for the next (default) case. There's some heuristics one could employ but there's no clear-cut approach. In fact, if you indent manually, gofmt accepts that as well.

Thanks for the explanation, that makes sense.

@josharian
Copy link
Contributor

@josharian josharian commented Jun 15, 2017

See also #9910, of which this is perhaps a dup.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Jun 15, 2017

@josharian Yes, indeed. Closing as duplicate of #9910.

@griesemer griesemer closed this Jun 15, 2017
@golang golang locked and limited conversation to collaborators Jun 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.