New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go/printer: format x&1<<8 as x&1 << 8? #13038
Comments
& and << are both multiplicative operators and have the same binding strength. The suggestion implies a different binding for & than for <<. Where do we stop? /cc: @rsc who wrote the code for the spacing around operators in go/printer. |
The expression formatting logic is basically unchanged from day 1 of the public release, and I think we should leave it that way. There are any number of arguments for and against minor changes, but widespread agreement is more important than any single change. As Rob said recently, gofmt particular syntax choices are no one's favorite but gofmt is everyone's favorite. For this specific case, a goal for the expression formatting was on a particular line, operators with spaces have looser precedence than operators without. If we write this expression as x&1 << 8 that implies something about & vs << that simply isn't true. By itself, the expression formats as x & 1 << 8. When used inside a larger expression with another precedence, it gets compressed to make clear that the outer expression has different precedence:
Note in the last case especially that a special case for & vs << would produce a confusing result with respect to addition-level operators like | or +. No matter what the formatting is, people will still make the occasional mistake. It's actually working very well. Let's leave it as it is. |
@minux What about making cmd/vet warn that "a&b<<c == 0" and "a&b<<c != 0" are probably missing parentheses? They seem very unlikely to occur legitimately. |
Yeah, I think cmd/vet check would be nice.
|
A recent bug on sqrt (#13013) is caused by missing parenthesis
in condition for for loop:
I suspect it might be a common error.
What if we make gofmt formats that to
instead?
/cc: @griesemer
The text was updated successfully, but these errors were encountered: