sqs / goreturns

A gofmt/goimports-like tool for Go programmers that fills in Go return statements with zero values to match the func return types
Other
530 stars 55 forks source link

Formatting inconsistent with gofmt #17

Closed vanackere closed 8 years ago

vanackere commented 8 years ago

Hi,

The following code in g() seems to be indented differently depending if I use goimports (same formatting as gofmt) or goreturns :

package test

func g() {
        f().
                Scan(a,
                b)
        return
}

func h() {
        Scan(a,
                b)
}

Note that there is no return value involved here... (I built this minimal sample from a much larger file where some coworkers and me were wondering why the formatting was always changing between our commits... turns out is was caused by us using different formatting tools !).

dmitshur commented 8 years ago

Cannot reproduce. Latest versions of both commands produce the same output for me. Can you post what your output is (from goreturns and goimports)?

Both binaries use the same Go code to do gofmt-level formatting, so it's unlikely they would be able to format differently.

I think what's likely happening here is one of your goreturns or goimports binaries is stale, built a long time ago. You can use binstale and gostatus tools to check.

I think if you do this, it'll fix your problem:

go get -u golang.org/x/tools/cmd/goimports
go get -u sourcegraph.com/sqs/goreturns
vanackere commented 8 years ago

You are indeed right, I have a previous version of goreturns that do not have the same behaviour:

marvin:/tmp$ export GOPATH=$PWD
marvin:/tmp$ go get -u golang.org/x/tools/cmd/goimports
marvin:/tmp$ go get -u sourcegraph.com/sqs/goreturns
marvin:/tmp$ $HOME/bin/goreturns -d bug.go
diff bug.go gofmt/bug.go
--- /tmp/gofmt186851162 2016-01-25 09:02:02.766400770 +0100
+++ /tmp/gofmt056593649 2016-01-25 09:02:02.766400770 +0100
@@ -3,7 +3,7 @@
 func g() {
    f().
        Scan(a,
-       b)
+           b)
    return
 }

marvin:/tmp$ ./bin/goreturns -d bug.go    
marvin:/tmp$ ls -l /home/vince/bin/goreturns 
-rwxrwxr-x 1 vince vince 6984600 janv. 14 15:54 /home/vince/bin/goreturns

It turns out I had compiled this particular version of goreturns with go 1.6 (and goimports with go1.6 indeed exhibits the same formatting behaviour). I guess new the 1.6 behaviour results from a bug fix (seeing the different formatting in g() and h() in my sample using go 1.5). I'm closing this bug now. Thanks !

dmitshur commented 8 years ago

You're welcome!