mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

Using rewrite rules with gofumpt #275

Closed silverwind closed 12 months ago

silverwind commented 1 year ago

I would like to rewrite interface{} to any, but the docs seem unclear about rewrite rules:

Finally, note that the -r rewrite flag is removed in favor of gofmt -r, and the -s flag is hidden as it is always enabled.

What does this statement mean? Does one need to run gofmt before gofumpt to be able to use rewrite rules? A docs clarification would be nice.

Also, I wonder how a tool that is advertized as a "drop-in replacement" does not support all features of the tool it's meant to replace.

mvdan commented 1 year ago

Correct, there isn't a gofumpt -r because you should use gofmt -r. There are three reasons for this:

1) gofmt -r works, and gofumpt is not in the business of pattern rewrites. 2) gofmt -r has always been quite limited by design; https://github.com/rsc/rf is their new attempt and it's far more powerful. I don't see a point in investing more time into gofmt -r, particularly since neither approach has anything to do with canonical formatting. 3) It is a surprisingly complex chunk of code that we don't need to maintain on our end.

Also note that our README says:

It can be used as a drop-in replacement to format your Go code

I think that still holds true; at least in my head, rewrite rules have little to do with canonical formatting like gofmt or gofumpt.

In any case, I'm happy to get input on better ways to document both of these aspects :)

silverwind commented 1 year ago

I guess in the end such "rewrites" are the job of a linter's autofix, e.g. ruff --fix or eslint --fix serve the purpose very will in those respective languages, but golang's linter's are rudimentary compared to those two, golangci's --fix is borderline useless.

I still hope a better go linter arises, golangci really pales in comparision to what other linters can do.

mvdan commented 12 months ago

I'm going to close this then, as it seems like the misunderstanding is solved. Still happy to receive suggestions in terms of better wording - in that case please send a PR.