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: unfinished else statement followed by if statement should maybe have indentation #26701

Open
mholt opened this issue Jul 30, 2018 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mholt
Copy link

mholt commented Jul 30, 2018

What version of Go are you using (go version)?

go version go1.10.2 darwin/amd64

Does this issue reproduce with the latest release?

Yep

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/matt/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/matt/Dev"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d4/3d8k5bgd62v9h2c5f05xkyx80000gn/T/go-build921281437=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Started writing an else statement but got distracted fixing an issue elsewhere in the program and didn't finish the statement. Definitely my bad! However, gofmt made this bug a little harder to find:

https://play.golang.org/p/AdIDE1dT9Sd

Notice that the next statement is an if, with a comment between them. Because of the comment. go fmt didn't move the if up to the line with the else. That seems correct, however, since it is technically a continuation of the else, indenting the if would have been helpful for catching it.

What did you expect to see?

...
	} else

		// unfinished statement above
		if b == 2 {
			fmt.Println("b is 2")
		}
...

What did you see instead?

...
	} else

	// unfinished statement above
	if b == 2 {
		fmt.Println("b is 2")
	}
...

This is just a proposed enhancement -- not urgent, and definitely still my fault for not finishing a line I started -- but if gofmt could figure this out and cue me in by indenting, that would be helpful. :)

Edit: Seems similar to #20562 -- at least one of the comments there mentions the same issue, it looks like. Feel free to close if you think this is a duplicate. I can't quite tell if that's a separate formatting issue originally...

@robpike
Copy link
Contributor

robpike commented Jul 31, 2018

The comment is necessary to trigger this behavior. Without a comment between the 'else' and the if, gofmt behaves as requested.

@ALTree ALTree changed the title cmd/gofmt: Unfinished else statement followed by if statement should maybe have indentation cmd/gofmt: unfinished else statement followed by if statement should maybe have indentation Jul 31, 2018
@ALTree ALTree added this to the Go1.12 milestone Jul 31, 2018
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 1, 2018
@griesemer griesemer modified the milestones: Go1.12, Unplanned Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants