-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/gofmt: simplify nil checks followed by len check, e.g. x != nil && len(x) ... #14733
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
Comments
Unfortunately, w/o type information, this simplification cannot be done in general; see: http://play.golang.org/p/PMfEn4oT_i That is, this not an option for |
I disagree with both the arguments you make for putting this into
and
The fact that |
interesting @griesemer I didn't take account the behavior of pointers and len. Should I move this the golint issues? |
@client9 Yes, please: https://github.com/golang/lint/issues . I'm going to close this. |
@ALTree I'm convinced. golint it is. |
This a followup from golang-nuts that I posted a few months back.
It would be nice if
gofmt -s
simplified the following redundant expressions:can be simplified into just
How common is this?
Not uncommon: While this code search on GitHub isn't perfect, it does show up. I'd prefer not to download github to correctly search until I understand the acceptance requirements (defects per 10k? at what level? number of repos affected?)
To quote the original topic:
I reran this, a few of them have been fixed up and some new ones added.
Great for learning
Even if not particularly common, people new to Go from ruby, C/C++, java are normally terrified by
nil
ornull
and are use to putting in guards. Having this check in gofmt, make learning that thelen
operator understands the native type and makes code simpler. Plus it definitely makes the code easier to read.gofmt -st
vs.golint
len
understands pointers and types) vs a style issues. (more likegofmt
, less likegolint
)gofmt
, less likegolint
)-s
flag ingofmt
is less known thangolint
, andgolint
is decoupled from release cycles.gofmt
. For instance, what should happen with inverted forms such asx == nil || 0 == len(x)
, or broken code such asif len(x) == 0 || x = nil
(which I bet happens).gofmt -s
simplification is very simple, whilegolint
is more like "hey take a look at this.."Next Steps
If this is better in
golint
Great, let me know and I'll move the ticket over there.Likewise, if this needs a formal proposal I'm happy to that as well.
And finally, I'm also happy to fill out the copyright assignment and do the actual work. You win!
ps. While writing this, "golint" got autocorrected as "goofy"
The text was updated successfully, but these errors were encountered: