goreturns icon indicating copy to clipboard operation
goreturns copied to clipboard

Incorrect rewrite of bare return at end of block when comment follows

Open philoserf opened this issue 6 years ago • 2 comments

Here is a diff example of bare return rewrite when using goreturns -b -i -l -w .:

 	if err != nil {
-		return
+		return output,
+
+			// Processing output
+			err
 	}

-	// Processing output
 	if updateCache {

expected result:

 	if err != nil {
-		return
+		return output, err
 	}

	// Processing output
 	if updateCache {

It is interesting that the rewrite escapes the {...} block.

philoserf avatar Dec 07 '18 00:12 philoserf

Heh, just came across this myself.

Preserving the comment structure is hard... https://github.com/golang/go/issues/18593

Edit:

and another useful reference, seems it is being considered ongoing: https://github.com/golang/go/issues/20744

Someone made another package representing the comments in another way which looks useful: https://github.com/dave/dst

pwaller avatar Dec 13 '18 14:12 pwaller

Hey there! Author of dst here... It should fix this problem. If you give it a go, let me know how it works...

dave avatar Jan 29 '19 15:01 dave