Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can a gofmt rewrite rule remove redundant argument types?

Tags:

go

If you have code like: func MyFunc(a int, b int)

Can a gofmt rewrite rule change it to: func MyFunc(a, b int)

I tried: gofmt -r "f(x t, y t) -> f(x, y t)" myfile.go

But I get: parsing pattern f(x t, y t) at 1:5: expected ')', found 'IDENT' t

I also tried: gofmt -r "f(x int, y int) -> f(x, y int)" myfile.go

But it gives a similar error for int instead of t

I have read the gofmt documentation. A web search didn't turn up anything helpful.

I am deliberately using single character identifiers to match expressions.

I suspect the problem may be in trying to match the type since it may not be regarded as an "expression"

Is it possible to do this with gofmt?

like image 401
Andrew McKinlay Avatar asked Oct 21 '22 10:10

Andrew McKinlay


1 Answers

No, its not possible - because go fmt treat patter as "Expression", look at the http://golang.org/src/cmd/gofmt/rewrite.go parseExpr() function.

Go specification(http://golang.org/ref/spec#Expressions) clearly says what "An expression specifies the computation of a value by applying operators and functions to operands." so go fmt try to parse your pattern "f(x t, y t)" as function call, so instead of "t" it expects comma or parentheses.

you can not write pattern which will much "func MyFunc(a int, b int)" - because its function definition, not a valid go expression

like image 164
varyous Avatar answered Dec 01 '22 17:12

varyous