gofumpt icon indicating copy to clipboard operation
gofumpt copied to clipboard

Unexpected splitting of function arguments

Open twpayne opened this issue 2 years ago • 3 comments

Given the following code (constraints, slices, and maps are the golang.org/x/exp packages of the same name):

func SortedKeys[K interface {
	comparable
	constraints.Ordered
}, V any](m map[K]V) []K {
	keys := maps.Keys(m)
	slices.Sort(keys)
	return keys
}

gofumpt@latest (900c61a4cb83bedde751dd2aedf2fc1c73de5e40) reformats this to:

func SortedKeys[K interface {
	comparable
	constraints.Ordered
}, V any](m map[K]V,
) []K {
	keys := maps.Keys(m)
	slices.Sort(keys)
	return keys
}

which has this diff:

--- a   2022-06-19 16:24:24.237245879 +0000
+++ b   2022-06-19 16:25:48.403318940 +0000
@@ -1,7 +1,8 @@
 func SortedKeys[K interface {
        comparable
        constraints.Ordered
-}, V any](m map[K]V) []K {
+}, V any](m map[K]V,
+) []K {
        keys := maps.Keys(m)
        slices.Sort(keys)
        return keys

I did not expect the function arguments to be split over two lines. I'm only starting to learn generics, so this might however be idiomatic. Please do close this issue if it is invalid.

twpayne avatar Jun 19 '22 16:06 twpayne

This rule was tested before the introduction of generics, so my best guess is that this is unintended :) Have you tried the latest master version as well, to see if the same problem occurs?

cc @Oiyoo

mvdan avatar Jun 19 '22 16:06 mvdan

Yes, I've tested with the latest master (900c61a4cb83bedde751dd2aedf2fc1c73de5e40) and the same problem occurs.

twpayne avatar Jun 19 '22 17:06 twpayne

I can confirm that this is an issue on v0.3.2-0.20220627183521-8dda8068d9f3, which is the latest commit in master.

ainar-g avatar Jul 15 '22 13:07 ainar-g

Here is a possible another case:

package  fact
func

Factorial(x    int)     int {

	return x * Factorial(x   -  1,
)
    }

converted to:

package fact

func Factorial(x int,
) int {
	return x * Factorial(x-1)
}

meain avatar Dec 06 '22 04:12 meain